Campaign cost(total spend) from ads-api


#1

I am trying to pull campaign costs/total spend from the API
I am 100% new to the API and I am not able to crack it through

I want to :
1- get cost per campaign
2- get campaign ids that showup on the GUI not just the kind of weird codes ie 8cdfn vs 1448530943

here is my python code so far

from twitter_ads.client import Client
from twitter_ads.cursor import Cursor
from twitter_ads.http import Request
from twitter_ads.error import Error
import time

from twitter_ads.campaign import LineItem
from twitter_ads.enum import METRIC_GROUP
from twitter_ads.enum import GRANULARITY

client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
account = client.accounts(ACCOUNT_ID)
cids = map(lambda x: x.id, account.campaigns())
resource = ‘/2/stats/accounts/{account_id}/’.format(account_id=account.id)
params = { ‘entity’:‘CAMPAIGN’,
‘entity_ids’:cids,
‘start_time’: ‘2017-12-10’,
‘end_time’:‘2017-12-16’,
‘granularity’:‘TOTAL’,
‘metric_groups’: ‘BILLING’,
‘placement’: ‘PUBLISHER_NETWORK’}

try, build and execute the request with error handling

try:
response = Request(client, ‘get’, resource, params=params).perform()
print(response.body[‘data’])
except Error as e:
# see twitter_ads.error for more details
print e.details
raise


#2

Hi @HuNegm,

In order to determine the underlying cause, can you setup Twurl and make the same request? Then copy / paste request and response here.

Ex -->

twurl -H ads-api.twitter.com "/2/stats/accounts/18ce54d4x5t?entity=LINE_ITEM&entity_ids=8u94t&start_time=2017-05-19&end_time=2017-05-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=ENGAGEMENT" | jq

Regarding the weird codes mentioned, the API uses the base 36 version of the number vs UI is base 10. Here is a link I use sometimes to convert the numbers.


#3

Hello @goforbrent
thanks for your reply . So I installed Twurl and here are the responses I get for 3 different calls I think are relevant

twurl -H ads-api.twitter.com “/2/stats/accounts/18ce53xn1yg?entity=LINE_ITEM&entity_ids=8qqsr&start_time=2017-05-19&end_time=2017-05-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=ENGAGEMENT”
{“data_type”:“stats”,“time_series_length”:1,“data”:[{“id”:“8qqsr”,“id_data”:[{“segment”:null,“metrics”:{“impressions”:[197062],“tweets_send”:null,“qualified_impressions”:null,“follows”:[100],“app_clicks”:[1728],“retweets”:[6],“likes”:[106],“engagements”:[4056],“clicks”:[2227],“card_engagements”:[1711],“poll_card_vote”:null,“replies”:[8],“url_clicks”:[17],“carousel_swipes”:null}}]}],“request”:{“params”:{“start_time”:“2017-05-19T07:00:00Z”,“segmentation_type”:null,“entity_ids”:[“8qqsr”],“end_time”:“2017-05-26T07:00:00Z”,“country”:null,“placement”:“ALL_ON_TWITTER”,“granularity”:“TOTAL”,“entity”:“LINE_ITEM”,“platform”:null,“metric_groups”:[“ENGAGEMENT”]}}}

twurl -H ads-api.twitter.com “/2/stats/accounts/18ce53xn1yg?entity=CAMPAIGN&entity_ids=9wggk&start_time=2017-12-19&end_time=2017-12-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=BILLING”

{“data_type”:“stats”,“time_series_length”:1,“data”:[{“id”:“9wggk”,“id_data”:[{“segment”:null,“metrics”:{“billed_charge_local_micro”:[100000000],“billed_engagements”:[34427]}}]}],“request”:{“params”:{“start_time”:“2017-12-19T08:00:00Z”,“segmentation_type”:null,“entity_ids”:[“9wggk”],“end_time”:“2017-12-26T08:00:00Z”,“country”:null,“placement”:“ALL_ON_TWITTER”,“granularity”:“TOTAL”,“entity”:“CAMPAIGN”,“platform”:null,“metric_groups”:[“BILLING”]}}}


#4

Hi @HuNegm,

From the looks of your responses, you were able to get cost per campaign and if you’d like to pull campaign IDs that match the Ads UI. You just need to build in base 10 to base 36 converter in your code. Please let me know if you need anything else!

Thanks!


#5

@goforbrent Thanks again. I know the following will probably define ignorance for you :grin: but how do I get cost from these responses? Do I multiply billed_charge_local_micro*billed_engagements?


#6

Hi @HuNegm,

When you say cost, do you mean money spent on serving ads? Which would be billed_charge_local_micro (total spend in micros), other wise I’d checkout our derived metrics page for other metrics available. Thanks!