Skip to content

Commit 3639f28

Browse files
authored
Update example (#210)
* Update analytics exapmle script - explicitly convert map object to list (Python 3 compatible) - remove misuse of "count" parameter (it's just a page size adjuster and Cursor class will read all cursor until it's exhausted anyway.) * consistent use of map() with join() method
1 parent b24465f commit 3639f28

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

examples/analytics.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# https://dev.twitter.com/ads/analytics/metrics-and-segmentation
88
# https://dev.twitter.com/ads/analytics/metrics-derived
99

10+
import sys
1011
import time
1112

1213
from twitter_ads.client import Client
@@ -25,8 +26,8 @@
2526
# load the advertiser account instance
2627
account = client.accounts(ACCOUNT_ID)
2728

28-
# limit request count and grab the first 10 line items from Cursor
29-
line_items = list(account.line_items(None, count=10))[:10]
29+
# grab the first 10 line items from Cursor
30+
line_items = list(account.line_items(None))[:10]
3031

3132
# the list of metrics we want to fetch, for a full list of possible metrics
3233
# see: https://dev.twitter.com/ads/analytics/metrics-and-segmentation
@@ -36,7 +37,11 @@
3637
line_items[0].stats(metric_groups)
3738

3839
# fetching stats for multiple line items
39-
ids = map(lambda x: x.id, line_items)
40+
ids = list(map(lambda x: x.id, line_items))
41+
if not ids:
42+
print('Error: A minimum of 1 items must be provided for entity_ids')
43+
sys.exit()
44+
4045
LineItem.all_stats(account, ids, metric_groups)
4146

4247
# fetching async stats on the instance
@@ -52,3 +57,5 @@
5257
async_stats_job_result = LineItem.async_stats_job_result(account, [job_id]).first
5358

5459
async_data = LineItem.async_stats_job_data(account, async_stats_job_result.url)
60+
61+
print(async_data)

twitter_ads/campaign.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TargetingCriteria(Resource, Persistence, Batch):
2222
@classmethod
2323
def all(klass, account, line_item_ids, **kwargs):
2424
"""Returns a Cursor instance for a given resource."""
25-
params = {'line_item_ids': ','.join(line_item_ids)}
25+
params = {'line_item_ids': ','.join(map(str, line_item_ids))}
2626
params.update(kwargs)
2727

2828
resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)

twitter_ads/creative.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def load(klass, account, card_uris=None, card_id=None, with_deleted=None):
513513
params['with_deleted'] = 'true'
514514

515515
if card_uris:
516-
params['card_uris'] = ','.join(card_uris)
516+
params['card_uris'] = ','.join(map(str, card_uris))
517517
resource = klass.FETCH_URI.format(account_id=account.id)
518518
request = Request(account.client, 'get', resource, params=params)
519519
return Cursor(klass, request, init_with=[account])

twitter_ads/resource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,15 @@ def _standard_params(klass, ids, metric_groups, **kwargs):
254254
placement = kwargs.get('placement', PLACEMENT.ALL_ON_TWITTER)
255255

256256
params = {
257-
'metric_groups': ','.join(metric_groups),
257+
'metric_groups': ','.join(map(str, metric_groups)),
258258
'start_time': to_time(start_time, granularity),
259259
'end_time': to_time(end_time, granularity),
260260
'granularity': granularity.upper(),
261261
'entity': klass.ANALYTICS_MAP[klass.__name__],
262262
'placement': placement
263263
}
264264

265-
params['entity_ids'] = ','.join(ids)
265+
params['entity_ids'] = ','.join(map(str, ids))
266266

267267
return params
268268

0 commit comments

Comments
 (0)