I am trying to extract ad wise spend data including metrics like impression,clicks and fields like campaign id, campaign name, ad group id, ad group name, tweet id in a single report every day. Reference below
As per the documentation i don’t find there is a straight forward solution to fetch these data unlike other ad platforms (Adwords, FB, Quora).
I am using python twitter_ads library to achieve this. below are the sample code which generates required metrics. however i couldn’t find the way to fetch other fields like campaign id/name, adgroup id/name through PromotedTweet ids.
from twitter_ads.client import Client
from twitter_ads.enum import METRIC_GROUP, GRANULARITY, PLACEMENT
from twitter_ads.creative import PromotedTweet
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
account = client.accounts(ACCOUNT_ID)
metric_groups = [METRIC_GROUP.BILLING, METRIC_GROUP.ENGAGEMENT, METRIC_GROUP.WEB_CONVERSION]
granularity = GRANULARITY.DAY
start = datetime.strptime('2018-01-24', '%Y-%m-%d')
end = datetime.strptime('2018-01-25', '%Y-%m-%d')
active_entities = PromotedTweet.active_entities(account, start, end)
for i in range(0, len(active_entities), 20):
ids = []
for itm in active_entities[i:i + 20]:
ids.append(itm['entity_id'])
queued_job = PromotedTweet.queue_async_stats_job(
account, ids, metric_groups, granularity=granularity, start_time=start, end_time=end)
job_id = queued_job['id']
time.sleep(15)
async_stats_job_result = PromotedTweet.async_stats_job_result(account, [job_id])
async_data = PromotedTweet.async_stats_job_data(account, async_stats_job_result['url'])
for itm in async_data['data']:
print('tweet_id: ', itm['id'])
for x in itm['id_data']:
print('impressions:', x['metrics']['impressions'])
print('spend:', x['metrics']['billed_charge_local_micro'])
print('clicks:', x['metrics']['clicks'])
I guess if i take top down approach campaign->adgroups->promotedtweet->cost data this can be achievable. However how can i get other data like campaign name, ad group name form this call?
Moreover multiple request for this simple report will definitely hits the API limitation for large account so what’s the ideal way to fetch this report?