Skip to content

Commit 39120d7

Browse files
paulbainEvan Chen
authored and
Evan Chen
committed
Adding python example for Custom Audience targeting in Docsmith
Summary: https://phabricator.fb.com/P19851381 Test Plan: Run docsmith, profit
1 parent c45e4f9 commit 39120d7

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

examples/docs/custom_audiences.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright 2014 Facebook, Inc.
2+
3+
# You are hereby granted a non-exclusive, worldwide, royalty-free license to
4+
# use, copy, modify, and distribute this software in source code or binary
5+
# form for use in connection with the web services and APIs provided by
6+
# Facebook.
7+
8+
# As with any software that integrates with the Facebook platform, your use
9+
# of this software is subject to the Facebook Developer Principles and
10+
# Policies [http://developers.facebook.com/policy/]. This copyright notice
11+
# shall be included in all copies or substantial portions of the software.
12+
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
# DEALINGS IN THE SOFTWARE.
20+
21+
from facebookads.objects import *
22+
from facebookads.api import *
23+
from facebookads.exceptions import *
24+
25+
config_file = open('./examples/docs/config.json')
26+
config = json.load(config_file)
27+
config_file.close()
28+
29+
account_id = config['account_id']
30+
access_token = config['access_token']
31+
app_id = config['app_id']
32+
app_secret = config['app_secret']
33+
34+
FacebookAdsApi.init(app_id, app_secret, access_token)
35+
36+
# _DOC open [CUSTOM_AUDIENCE_CREATE]
37+
# _DOC vars [account_id:s]
38+
# from facebookads.objects import CustomAudience
39+
audience = CustomAudience(parent_id=account_id)
40+
41+
audience[CustomAudience.Field.name] = 'My new CA'
42+
audience[CustomAudience.Field.description] = 'People who bought on my website'
43+
44+
audience.remote_create()
45+
# _DOC close [CUSTOM_AUDIENCE_CREATE]
46+
47+
custom_audience_id = audience.get_id()
48+
49+
# _DOC open [CUSTOM_AUDIENCE_USERS_ADD_EMAILS]
50+
# _DOC vars [custom_audience_id:s]
51+
52+
# from facebookads.objects import CustomAudience
53+
audience = CustomAudience(custom_audience_id)
54+
users = ['test1@example.com', 'test2@example.com', 'test3@example.com']
55+
56+
audience.add_users(CustomAudience.Schema.email_hash, users)
57+
# _DOC close [CUSTOM_AUDIENCE_USERS_ADD_EMAILS]
58+
59+
# _DOC open [CUSTOM_AUDIENCE_USERS_REMOVE_EMAILS]
60+
# _DOC vars [custom_audience_id:s]
61+
# from facebookads.objects import CustomAudience
62+
audience = CustomAudience(custom_audience_id)
63+
users = ['test1@example.com', 'test2@example.com', 'test3@example.com']
64+
65+
audience.remove_users(CustomAudience.Schema.email_hash, users)
66+
# _DOC close [CUSTOM_AUDIENCE_USERS_REMOVE_EMAILS]
67+
68+
# _DOC open [CUSTOM_AUDIENCE_UPDATE_NAME]
69+
# _DOC vars [custom_audience_id:s]
70+
# from facebookads.objects import CustomAudience
71+
audience = CustomAudience(custom_audience_id)
72+
audience[CustomAudience.Field.name] = 'Updated name for CA'
73+
audience.remote_update()
74+
# _DOC close [CUSTOM_AUDIENCE_UPDATE_NAME]
75+
76+
# _DOC open [CUSTOM_AUDIENCE_UPDATE_OPTOUT]
77+
# _DOC vars [custom_audience_id:s]
78+
# from facebookads.objects import CustomAudience
79+
audience = CustomAudience(custom_audience_id)
80+
audience[CustomAudience.Field.opt_out_link] = 'http://www.yourdomain.com/optout'
81+
audience.remote_update()
82+
# _DOC close [CUSTOM_AUDIENCE_UPDATE_OPTOUT]
83+
84+
audience.remote_delete()
85+
86+
audience = CustomAudience(parent_id=account_id)
87+
audience[CustomAudience.Field.name] = 'My new CA'
88+
audience[CustomAudience.Field.description] = 'Docsmith Example CA'
89+
audience.remote_create()
90+
custom_audience_id = audience.get_id()
91+
user_id_1 = 1234
92+
user_id_2 = 12345
93+
94+
# _DOC open [CUSTOM_AUDIENCE_USERS_ADD_ID]
95+
# _DOC vars [custom_audience_id:s, app_id:s, user_id_1:s, user_id_2:s]
96+
# from facebookads.objects import CustomAudience
97+
audience = CustomAudience(custom_audience_id)
98+
users = [user_id_1, user_id_2]
99+
apps = [app_id]
100+
audience.add_users(CustomAudience.Schema.uid, users, apps)
101+
# _DOC close [CUSTOM_AUDIENCE_USERS_ADD_ID]
102+
103+
audience.remote_delete()

0 commit comments

Comments
 (0)