Skip to content

Instantly share code, notes, and snippets.

@zachmiri
Created September 19, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachmiri/7bdbdf8bbb0bce0989dec07f6e6e9897 to your computer and use it in GitHub Desktop.
Save zachmiri/7bdbdf8bbb0bce0989dec07f6e6e9897 to your computer and use it in GitHub Desktop.
# Expanding Lookalikes above 10%
import requests
import json
import time
# Update with a list of all audince IDs (lookalike seeds)
audience_ids = [""]
# Update with a list of all countries (lookalike geos)
countries = [""]
# Update with a list of ratos (lookalike percentage brackets, up to 0.20)
ratios = [[0.01, 0.20]]
# Update with your Facebook Ad Account ID and Access Token
adaccount_id = ''
access_token = ''
for ratio in ratios:
for audience_id in audience_ids:
for country in countries:
time.sleep(0.5)
if ratio[0] != ratio[1]:
data_dump = {
'subtype': 'LOOKALIKE',
'access_token': access_token,
'origin_audience_id': audience_id,
'lookalike_spec': json.dumps({
'starting_ratio': ratio[0],
'ratio': ratio[1],
'country': country
})
}
else:
data_dump = {
'subtype': 'LOOKALIKE',
'access_token': access_token,
'origin_audience_id': audience_id,
'lookalike_spec': json.dumps({
'ratio': ratio[0],
'country': country
})
}
r = requests.post('https://graph.facebook.com/v4.0/act_' + adaccount_id + '/customaudiences', data=data_dump)
result = r.json()
if 'id' in result:
print('Adset ID: ' + adset_id + ', Country: ' + country + ', Ratio: ' + str(ratio[0]) + ':' + str(ratio[1]) + ', ' + ' Audience ID: ' + result['id'])
else:
print('Adset ID: ' + adset_id + ', Country: ' + country + ', Error: ' + result['error']['message'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment