Trying to make API call to get campaign spend broken by country


#1

I’m trying to make an API call to pull campaign spend, broken out by country. Efforts so far have only reported spend aggregated for the whole campaign, and values of country are reporting null. Example of my call below (using Twitter’s python packages Client and enum).

from datetime import datetime, timedelta
from twitter_ads.client import Client
from twitter_ads.http import Request
from twitter_ads.enum import METRIC_GROUP

dateRequestStart = datetime.strptime((datetime.now()).strftime(’%Y-%m-%d’) + ‘T00:00:00Z’, “%Y-%m-%dT%H:%M:%SZ”)
dateRequestEnd = datetime.strptime((datetime.now()).strftime(’%Y-%m-%d’) + ‘T00:00:00Z’, “%Y-%m-%dT%H:%M:%SZ”)

ConsumerKey = ‘XXXXXXXXXXXXXXXXXX’
ConsumerSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessToken= ‘XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessTokenSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccountID = ‘XXXXXXXX’

client = Client(ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret)
account = client.accounts(AccountID)
campaign_list = list(account.campaigns())

metric_groups = [METRIC_GROUP.BILLING]

for i in campaign_list:
response = i.stats(metric_groups, segment = ‘LOCATION.COUNTRY’, granularity=‘DAY’, start_time = dateRequestStart, end_time = dateRequestEnd)
for i in response:
print(i)


#2

Hi @eaglennsworld ,

From your code snippet,

response = i.stats(metric_groups, segment = ‘LOCATION.COUNTRY’, granularity=‘DAY’, start_time = dateRequestStart, end_time = dateRequestEnd)

this doesn’t look like correct, specifically for segment = ‘LOCATION.COUNTRY' param (both of its name and value). You meant segmentation_type, perhaps? You can refer possible values from here (at the bottom of the page, Segmentation section):
https://developer.twitter.com/en/docs/ads/analytics/overview/metrics-and-segmentation#segmentation

In this case, you may want to use just LOCATIONS as a value. See below post for details.

Best,
Shohei


#3

Thanks @jrsyo , I’ve changed the request to use the asynchronous analytics endpoints. I think the request is correct based on the segments information you gave me, however the value for country returns null, and it aggregates just at the daily level. Does this mean the campaigns are not broken out by country?

import requests
from datetime import datetime, timedelta
import time
from twitter_ads.http import Request
import json
from requests_oauthlib import OAuth1

reportStart = ‘2018-12-01’
reportEnd = ‘2018-12-02’

ConsumerKey = ‘XXXXXXXXXXXXXXXXXX’
ConsumerSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessToken= ‘XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccessTokenSecret = ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’
AccountID = ‘XXXXXXXX’
client = OAuth1(ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret)

url = ‘https://ads-api.twitter.com/4/stats/jobs/accounts/{0}?entity=CAMPAIGN&entity_ids=btyeo&segmentation_type=LOCATIONS&start_time={1}&end_time={2}&granularity=DAY&placement=ALL_ON_TWITTER&metric_groups=BILLING

response = requests.post(url.format(AccountID, reportStart, reportEnd), auth = client)
data = response.content
data = data.decode(‘utf-8’)
dictdump = json.loads(data)
print(dictdump)
jobId = dictdump[‘data’][‘id’]

url = ‘https://ads-api.twitter.com/4/stats/jobs/accounts/{0}?job_ids={1}’.format(AccountID, jobId)
time.sleep(15)
response = requests.get(url, auth = client)
data = response.content
data = data.decode(‘utf-8’)
dictdump = json.loads(data)
for i in dictdump[‘data’]:
url = i[‘url’]
print(url)


#4

I tried the same but get the error:
MISSING_PARAMETER: "country" is a required parameter.


#5

Some segmentation types, CITIES , POSTAL_CODES , or REGIONS, require the Country field as documented. Could you create a new forum post that includes the Twurl request?