Skip to content

Commit 17ffd57

Browse files
authored
v9 update for TA->CA (#278)
* Update audience.py * Update test_targeted_audiences.py * Update __init__.py * Create custom_audience.py * Update account.py * Update campaign.py * Update custom_audience.py * Create custom_audience_load.json * Rename custom_audience_load.json to custom_audiences_load.json * Rename tailored_audiences_all.json to custom_audiences_all.json * Update and rename tailored_audiences_permissions_all.json to custom_audiences_permissions_all.json
1 parent ebb8202 commit 17ffd57

9 files changed

+146
-78
lines changed

examples/custom_audience.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import hashlib
2+
from twitter_ads.client import Client
3+
from twitter_ads.audience import CustomAudience
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 custom audience
18+
audience = CustomAudience.create(account, 'test CA')
19+
20+
# sample user
21+
# all values musth be sha256 hashed
22+
email_hash = hashlib.sha256("[email protected]").hexdigest()
23+
24+
# create payload
25+
user = [{
26+
"operation_type": "Update",
27+
"params": {
28+
"users": [{
29+
"email": [
30+
email_hash
31+
]
32+
}]
33+
}
34+
}]
35+
36+
# update the custom audience
37+
success_count, total_count = audience.users(user)
38+
if success_count == total_count:
39+
print(("Successfully added {total_count} users").format(total_count=total_count))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"data_type": "custom_audience",
3+
"data": {
4+
"targetable": false,
5+
"name": "TA #2",
6+
"targetable_types": [
7+
"WEB",
8+
"EXCLUDED_WEB"
9+
],
10+
"audience_type": "WEB",
11+
"id": "abc2",
12+
"reasons_not_targetable": [
13+
"TOO_SMALL"
14+
],
15+
"list_type": null,
16+
"created_at": "2014-03-09T20:35:41Z",
17+
"updated_at": "2014-06-11T09:38:06Z",
18+
"partner_source": "OTHER",
19+
"deleted": false,
20+
"audience_size": null
21+
},
22+
"request": {
23+
"params": {
24+
"account_id": "2iqph",
25+
"name": "TA #2",
26+
"list_type": "EMAIL"
27+
}
28+
}
29+
}

tests/fixtures/tailored_audiences_permissions_all.json renamed to tests/fixtures/custom_audiences_permissions_all.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"request": {
33
"params": {
44
"account_id": "2iqph",
5-
"tailored_audience_id": "abc2"
5+
"custom_audience_id": "abc2"
66
}
77
},
88
"data": [
99
{
10-
"tailored_audience_id": "abc2",
10+
"custom_audience_id": "abc2",
1111
"permission_level": "READ_ONLY",
1212
"id": "k",
1313
"created_at": "2016-04-09T18:16:32Z",
@@ -16,7 +16,7 @@
1616
"deleted": false
1717
},
1818
{
19-
"tailored_audience_id": "abc2",
19+
"custom_audience_id": "abc2",
2020
"permission_level": "READ_ONLY",
2121
"id": "l",
2222
"created_at": "2016-04-09T18:22:33Z",
@@ -25,7 +25,7 @@
2525
"deleted": false
2626
}
2727
],
28-
"data_type": "tailored_audience_permission",
28+
"data_type": "custom_audience_permission",
2929
"total_count": 2,
3030
"next_cursor": null
3131
}

tests/test_targeted_audiences.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from twitter_ads.account import Account
77
from twitter_ads.client import Client
8-
from twitter_ads.audience import TailoredAudience
8+
from twitter_ads.audience import CustomAudience
99
from twitter_ads.cursor import Cursor
1010
from twitter_ads import API_VERSION
1111

@@ -17,11 +17,11 @@ def test_targeted_audiences():
1717
body=with_fixture('accounts_load'))
1818

1919
responses.add(responses.GET,
20-
with_resource('/' + API_VERSION + '/accounts/2iqph/tailored_audiences/2906h'),
21-
body=with_fixture('tailored_audiences_load'))
20+
with_resource('/' + API_VERSION + '/accounts/2iqph/custom_audiences/2906h'),
21+
body=with_fixture('custom_audiences_load'))
2222

2323
responses.add(responses.GET,
24-
with_resource('/' + API_VERSION + '/accounts/2iqph/tailored_audiences/abc2/targeted?with_active=True'),
24+
with_resource('/' + API_VERSION + '/accounts/2iqph/custom_audiences/abc2/targeted?with_active=True'),
2525
body=with_fixture('targeted_audiences'))
2626

2727
client = Client(
@@ -33,7 +33,7 @@ def test_targeted_audiences():
3333

3434
account = Account.load(client, '2iqph')
3535

36-
audience = TailoredAudience.load(account, '2906h')
36+
audience = CustomAudience.load(account, '2906h')
3737
targeted_audiences = audience.targeted(
3838
with_active=True
3939
)
@@ -44,4 +44,4 @@ def test_targeted_audiences():
4444
assert targeted_audiences.first.line_items[0]['id'] == '5gzog'
4545
assert targeted_audiences.first.line_items[0]['name'] == 'test-line-item'
4646
assert targeted_audiences.first.line_items[0]['servable'] == True
47-
assert len(responses.calls) == 3
47+
assert len(responses.calls) == 3

twitter_ads/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (C) 2015 Twitter, Inc.
22

3-
VERSION = (8, 0, 0)
4-
API_VERSION = '8'
3+
VERSION = (9, 0, 0)
4+
API_VERSION = '9'
55

66
from twitter_ads.utils import get_version
77

twitter_ads/account.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from twitter_ads.resource import resource_property, Resource
1212
from twitter_ads.creative import (AccountMedia, MediaCreative, ScheduledTweet,
1313
Card, PromotedTweet)
14-
from twitter_ads.audience import TailoredAudience
14+
from twitter_ads.audience import CustomAudience
1515
from twitter_ads.campaign import (AppList, Campaign, FundingInstrument, LineItem,
1616
PromotableUser, ScheduledPromotedTweet)
1717

@@ -111,12 +111,12 @@ def app_lists(self, id=None, **kwargs):
111111
"""
112112
return self._load_resource(AppList, id, **kwargs)
113113

114-
def tailored_audiences(self, id=None, **kwargs):
114+
def custom_audiences(self, id=None, **kwargs):
115115
"""
116-
Returns a collection of tailored audiences available to the
116+
Returns a collection of custom audiences available to the
117117
current account.
118118
"""
119-
return self._load_resource(TailoredAudience, id, **kwargs)
119+
return self._load_resource(CustomAudience, id, **kwargs)
120120

121121
def account_media(self, id=None, **kwargs):
122122
"""

0 commit comments

Comments
 (0)