Skip to content

Commit ea4d33c

Browse files
tushdantejuanshishido
authored andcommitted
Adds support for Audience Intelligence (#160)
* Added support for Audience Intelligence * removes untitled * replaces hardcoded version with `API_VERSION`
1 parent 3c6900f commit ea4d33c

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

examples/audience_intelligence.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from twitter_ads.client import Client
2+
from twitter_ads.audience import AudienceIntelligence
3+
from twitter_ads.enum import CONVERSATION_TYPE, AUDIENCE_DEFINITION
4+
5+
CONSUMER_KEY = 'your consumer key'
6+
CONSUMER_SECRET = 'your consumer secret'
7+
ACCESS_TOKEN = 'access token'
8+
ACCESS_TOKEN_SECRET = 'access token secret'
9+
ACCOUNT_ID = 'account id'
10+
11+
# initialize the client
12+
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
13+
14+
# load the advertiser account instance
15+
account = client.accounts(ACCOUNT_ID)
16+
17+
# create a new instance of AI to get conversations
18+
ai = AudienceIntelligence(account)
19+
ai.targeting_inputs = [{
20+
"targeting_type": 'GENDER',
21+
"targeting_value": '2'
22+
}, {
23+
"targeting_type": 'AGEBUCKET',
24+
"targeting_value": 'AGE_OVER_50'
25+
}]
26+
ai.conversation_type = CONVERSATION_TYPE.HASHTAG
27+
ai.audience_definition = AUDIENCE_DEFINITION.TARGETING_CRITERIA
28+
response = ai.conversations()
29+
for i in range(0, response.count):
30+
ai = response.next()
31+
print ai.localized['targeting_type']
32+
print ai.localized['targeting_value']
33+
print "\n"
34+
35+
# create a new instance of AI to get demographics
36+
ai = AudienceIntelligence(account)
37+
ai.targeting_inputs = [{
38+
"targeting_type": 'BROAD_MATCH_KEYWORD',
39+
"targeting_value": 'womensmarch2018',
40+
"start_time": '2017-12-31'
41+
}]
42+
ai.audience_definition = AUDIENCE_DEFINITION.KEYWORD_AUDIENCE
43+
response = ai.demographics()
44+
for key in response.keys():
45+
print key
46+
print response[key]
47+
print "\n"

twitter_ads/audience.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from twitter_ads.cursor import Cursor
1010
from twitter_ads import API_VERSION
1111

12+
import json
13+
1214

1315
class TailoredAudience(Resource):
1416

@@ -183,3 +185,67 @@ def delete(self):
183185
resource_property(TailoredAudiencePermission, 'tailored_audience_id')
184186
resource_property(TailoredAudiencePermission, 'granted_account_id')
185187
resource_property(TailoredAudiencePermission, 'permission_level')
188+
189+
190+
class AudienceIntelligence(Resource):
191+
192+
PROPERTIES = {}
193+
194+
RESOURCE_BASE = '/' + API_VERSION + '/accounts/{account_id}/audience_intelligence/'
195+
RESOURCE_CONVERSATIONS = RESOURCE_BASE + 'conversations'
196+
RESOURCE_DEMOGRAPHICS = RESOURCE_BASE + 'demographics'
197+
METHOD = 'post'
198+
HEADERS = {'Content-Type': 'application/json'}
199+
200+
@classmethod
201+
def __get(klass, account, client, params):
202+
"""
203+
Helper function to get the conversation data
204+
Returns a Cursor instance
205+
"""
206+
resource = klass.RESOURCE_CONVERSATIONS.format(account_id=account.id)
207+
208+
request = Request(
209+
account.client, klass.METHOD,
210+
resource, headers=klass.HEADERS, body=params)
211+
return Cursor(klass, request, init_with=[account])
212+
213+
def conversations(self):
214+
"""
215+
Get the conversation topics for an input targeting criteria
216+
"""
217+
body = {
218+
"conversation_type": self.conversation_type,
219+
"audience_definition": self.audience_definition,
220+
"targeting_inputs": self.targeting_inputs
221+
}
222+
return self.__get(account=self.account, client=self.account.client, params=json.dumps(body))
223+
224+
def demographics(self):
225+
"""
226+
Get the demographic breakdown for an input targeting criteria
227+
"""
228+
body = {
229+
"audience_definition": self.audience_definition,
230+
"targeting_inputs": self.targeting_inputs
231+
}
232+
resource = self.RESOURCE_DEMOGRAPHICS.format(account_id=self.account.id)
233+
response = Request(
234+
self.account.client, self.METHOD,
235+
resource, headers=self.HEADERS, body=json.dumps(body)).perform()
236+
return response.body['data']
237+
238+
239+
# tailored audience permission properties
240+
# read-only
241+
resource_property(AudienceIntelligence, 'operator_type', readonly=True)
242+
resource_property(AudienceIntelligence, 'targeting_type', readonly=True)
243+
resource_property(AudienceIntelligence, 'targeting_value', readonly=True)
244+
resource_property(AudienceIntelligence, 'localized', readonly=True)
245+
# writable
246+
resource_property(AudienceIntelligence, 'conversation_type')
247+
resource_property(AudienceIntelligence, 'targeting_inputs')
248+
resource_property(AudienceIntelligence, 'audience_definition')
249+
# demographics only
250+
resource_property(AudienceIntelligence, 'start_time', transform=TRANSFORM.TIME)
251+
resource_property(AudienceIntelligence, 'end_time', transform=TRANSFORM.TIME)

twitter_ads/enum.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,14 @@ def enum(**enums):
171171
REMOVE='REMOVE',
172172
REPLACE='REPLACE'
173173
)
174+
175+
AUDIENCE_DEFINITION = enum(
176+
KEYWORD_AUDIENCE='KEYWORD_AUDIENCE',
177+
TARGETING_CRITERIA='TARGETING_CRITERIA'
178+
)
179+
180+
CONVERSATION_TYPE = enum(
181+
HASHTAG='HASHTAG',
182+
HANDLE='TARGETING_CRITERIA',
183+
EVENT='EVENT'
184+
)

0 commit comments

Comments
 (0)