Retrieving ads metrics per campaign


#1

Hi,
I’m using the twitter-ads python library and trying to fetch some ads metrics with it (within a date range), without success.

I’m trying first with this entrypoint/ params:

resource = '/4/stats/accounts/{account_id}/'.format(account_id=account.id)
params = {'entity': 'CAMPAIGN',
          'entity_ids': 'xxxx,xxxx',
          'start_time': '2018-10-03',
          'end_time': '2018-10-10',
          'granularity': 'DAY',
          'metric_groups': 'ENGAGEMENT,MEDIA,WEB_CONVERSION',
          'placement': 'PUBLISHER_NETWORK'}

However I’m not getting data at the ads level, just campaign details.
What I need is to retrieve a list of ads by campaign with the stats, conversion, engagement, etc, as we can see in the web interface.

If I want to retrieve line_items data it forces me to pass the line_items_ids as a parameter which I don’t have.

What’s the right way to approach this?

Thanks in advance


#2

Thanks for the question, @Outflink. What is the account ID? What campaign IDs are you requesting data for? Please see our Guideline for Reporting Issues.


#3

@juanshishido Thanks for your answer.

Actually, the problem is not with a specific account_id or campaign_id. What we need is to retrieve data for any of the accounts / campaigns we are managing.

Let me explain:

We are trying to fetch metrics / stats per date for each one of the promoted tweets on the accounts and campaigns we’re running.

This is how we are trying to do it:

  1. We fetch all the promoted tweets per account
  2. Then we fetch metrics for every one of them (20 per call as per the docs)

Even though it seems to work we found this process very expensive.

So the rephrased question would be: Is there a way to retrieve the metrics / stats per day for all the ads / promoted tweets on a campaign or account in one REST call? Or the way we’ve found is the right one?

Thanks


#4

I need to look into the specifics of campaign you’re fetching data for to answer the following.


In terms of your new / rephrased question, there is not a way to fetch what you’re looking for in a single request. In addition, fetching data for every promoted_tweets entity may not be the best approach. It’s likely that not every ad is in flight for the days you’re fetching data for. That means there are likely wasted calls—i.e., calls that did not need to be made since there is no data for the specified time period.


You also said you don’t have line item IDs. Why not? You can page through the GET accounts/:account_id/line_items endpoint.


#5

It’s likely that not every ad is in flight for the days you’re fetching data for. That means there are likely wasted calls—i.e., calls that did not need to be made since there is no data for the specified time period.

Exactly, that’s my concern, I wonder if there is a better approach for requesting the data I need.
And basically what I need is all stats/ metrics for all the ads (promoted tweets) between two dates.

About the line_items you’re right, the same way I get the promoted tweets I can get the line items ids however I’d be facing the same issue about the unnecessary calls.

So It seems I have no chance but to retrieve all the promoted tweets ids and then query for metrics over all of them even though there might be some of them without data?

Btw, could you please explain what’s the exact difference between a line_item and a promoted_tweet? I’ve seen the same tweet ID in more than a line_item and yet I don’t understand what’s the line item use case.

Appreciate your help, thank you!


#6

@juanshishido
If I want to get data at the ads level, each add with its metrics per day, what’s the entity I should ask for? line_item or promoted_tweet?

Thanks


#7

@juanshishido
I’ve have an specific example of a promoted tweet who has some impressions as I can see in the ads manager, but when retrieving its metrics through the API I get everything None/ null.

promoted_tweet_id: 2a6f9x
tweet id: 1048974475667025925
line_item id: clsfq
between 2018-10-08 and 2018-10-09

In the ads manager I see 160 impressions for the 3 days, (2, 147, 11).
However in the data coming from the API this is the result:

[{'id': '2a6f9x',
  'id_data': [{'metrics': {'app_clicks': None,
                           'auto_created_conversion_add_to_cart': None,
                           'auto_created_conversion_add_to_wish_list': None,
                           'auto_created_conversion_added_payment_info': None,
                           'auto_created_conversion_checkout_initiated': None,
                           'auto_created_conversion_complete_registration': None,
                           'auto_created_conversion_content_view': None,
                           'auto_created_conversion_download': None,
                           'auto_created_conversion_page_view': None,
                           'auto_created_conversion_purchase': None,
                           'auto_created_conversion_search': None,
                           'auto_created_conversion_sign_up_initiated': None,
                           'card_engagements': None,
                           'carousel_swipes': None,
                           'clicks': None,
                           'conversion_custom': {'metric': None,
                                                 'order_quantity': None,
                                                 'order_quantity_engagement': None,
                                                 'order_quantity_view': None,
                                                 'post_engagement': None,
                                                 'post_view': None,
                                                 'sale_amount': None,
                                                 'sale_amount_engagement': None,
                                                 'sale_amount_view': None},
                           'conversion_downloads': {'metric': None,
                                                    'order_quantity': None,
                                                    'order_quantity_engagement': None,
                                                    'order_quantity_view': None,
                                                    'post_engagement': None,
                                                    'post_view': None,
                                                    'sale_amount': None,
                                                    'sale_amount_engagement': None,
                                                    'sale_amount_view': None},
                           'conversion_purchases': {'assisted': None,
                                                    'metric': None,
                                                    'order_quantity': None,
                                                    'order_quantity_engagement': None,
                                                    'order_quantity_view': None,
                                                    'post_engagement': None,
                                                    'post_view': None,
                                                    'sale_amount': None,
                                                    'sale_amount_engagement': None,
                                                    'sale_amount_view': None},
                           'conversion_sign_ups': {'assisted': None,
                                                   'metric': None,
                                                   'order_quantity': None,
                                                   'order_quantity_engagement': None,
                                                   'order_quantity_view': None,
                                                   'post_engagement': None,
                                                   'post_view': None,
                                                   'sale_amount': None,
                                                   'sale_amount_engagement': None,
                                                   'sale_amount_view': None},
                           'conversion_site_visits': {'metric': None,
                                                      'order_quantity': None,
                                                      'order_quantity_engagement': None,
                                                      'order_quantity_view': None,
                                                      'post_engagement': None,
                                                      'post_view': None,
                                                      'sale_amount': None,
                                                      'sale_amount_engagement': None,
                                                      'sale_amount_view': None},
                           'engagements': None,
                           'follows': None,
                           'impressions': None,
                           'likes': None,
                           'media_engagements': None,
                           'media_views': None,
                           'poll_card_vote': None,
                           'qualified_impressions': None,
                           'replies': None,
                           'retweets': None,
                           'tweets_send': None,
                           'unfollows': None,
                           'url_clicks': None},
               'segment': None}]}]

This is my request:

URI: '/4/stats/accounts/18ce54lpp19
params = {
        'entity': 'PROMOTED_TWEET',
        'entity_ids': '2a6f9x',
        'start_time': '2018-10-07',
        'end_time': '2018-10-09',
        'granularity': 'DAY',
        'metric_groups': 'ENGAGEMENT,MEDIA,WEB_CONVERSION',
        'placement': 'PUBLISHER_NETWORK'}

Any idea of where could be the issue?


#8

+1. I have the same ask.

From my understanding, line item refers to the ad groups so promoted tweets would be a part of an ad group.

I could define account, campaign and line item but did not get the method to define the promoted tweet or how to get a list of promoted tweets from a campaign or line item

account = client.accounts(ACCOUNT_ID)
campaign = account.campaigns().next()
line_item = account.line_items(None, campaign_ids=campaign.id).next()
promoted_tweet = ???

What would be the code in this case to do the following:

  • Get the list of promoted tweets in a line item or campaign or account

  • Make a call to get metrics for a list of promoted tweets


#9

Could you please share the code for how you are getting the id for promoted tweets. I am able to get the metrics by campaign and line item level but not get the list of promoted tweets.

I used a nested loop to drill down to the list of line items.

for campaign in account.campaigns():
    if 'XXXXXX' in campaign.name: #or whatever classification method you use in your campaigns
        cidlist.extend([campaign.id]) #creates a list of campaign ids
        for line_item in account.line_items(campaign_ids=campaign.id):
            lidlist.extend([line_item.id]) #creates a list of line item ids
mynewlist = list(set(lidlist))

#10

This would be the promoted tweets. Line items in our case refer to audience segments


#11

Here is you code. Thanks to @juanshishido for the help.

client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
account = client.accounts(ACCOUNT_ID)
campaign = account.campaigns().next()
line_item = account.line_items(None, campaign_ids=campaign.id).next()
promoted_tweet = PromotedTweet.all(account, line_item_ids=line_item.id)

ptidlist = []

#perhaps not the most computing optimal method but suggestions on how to rework this are welcome
for campaign in account.campaigns():
    if 'XXXXXX' in campaign.name:
        for line_item in account.line_items(campaign_ids=campaign.id):
            for promoted_tweet in PromotedTweet.all(account, line_item_ids=line_item.id):
                ptidlist.extend([promoted_tweet.id])

for i in ptidlist:          
    resource = '/4/stats/accounts/{account_id}/'.format(account_id=account.id)
    params = { 'entity':'PROMOTED_TWEET', #this is where promoted tweet ids will go
    'entity_ids': i,#this is where promoted tweet ids will go
    'start_time': '2018-09-30',
    'end_time':'2018-10-03',
    'granularity':'TOTAL',
    'metric_groups': ['BILLING'],
    'placement': 'ALL_ON_TWITTER'}
    response = Request(client, 'get', resource, params=params).perform()
    print(response.body['data'])

Good luck!


#12

Does promoted tweet stats give the stats for both organic tweets that were promoted after publishing and for promoted only tweets (dark posts) ?


#13

Hey thanks for your help
I agree with you, it’s not the most optimal way but I haven’t found any other way.

In any case as I said before, I see null data coming for ids that have some numbers in the ads manager interface,… @juanshishido could you please have a look at those ids?

Appreciate your help guys,
Thanks!


#14

Try changing the granularity to Day and then comparing it to the report for day to day performance in ads manager


#15

@WestDigi That’s exactly how I do the request, by DAY granularity.
Btw you don’t need to make a call for every tweet, you can pass a list of 20 promoted tweets ids per request.

@juanshishido another issue I see is that I query analytics data between ‘2018-10-07’ and ‘2018-10-08’ for a specific campaign, and one of the tweets I get metrics for (which is promoted_tweet_id: “29mf0z”. However if I go to the twitter ads manager in the UI I filter by those dates, same campaign I don’t see its corresponding tweet_id in the list.

@juanshishido I’d really appreciate your help

Thanks a lot,
Alejandro


#16

Also in the results ,when metric value comes as an object, it has this fields:
post_view, order_quantity, assisted, post_engagement, sale_amount…

Could you explain what’s the difference between them?

Thanks in advance