Created
September 19, 2019 10:41
-
-
Save zachmiri/50b56760efc785038f311852fcadbb46 to your computer and use it in GitHub Desktop.
Rollout a New Ad - https://mirigrowth.com/blog/automation-facebook-marketing-api-scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rollout a New Ad | |
import requests | |
import json | |
import time | |
# Update with your Facebook Ad Account ID and Access Token | |
adaccount_id = '' | |
access_token = '' | |
# Update with a list of ad IDs you want to roll out to all active adsets | |
to_rollout = [""] | |
r = requests.get('https://graph.facebook.com/v4.0/act_' + adaccount_id + '/adsets?fields=name,id,effective_status&effective_status=["ACTIVE"]&limit=50&access_token=' + access_token) | |
result = r.json() | |
adsets = result['data'] | |
while 'next' in result['paging']: | |
r = requests.get(result['paging']['next']) | |
result = r.json() | |
adsets.append(result['data']) | |
for ad in to_rollout: | |
for adset in adsets: | |
data_dump = { | |
'adset_id': adset['id'], | |
'rename_options': json.dumps({ | |
'rename_strategy':'NO_RENAME', | |
'rename_suffix':'' | |
}), | |
'status_option': 'ACTIVE', | |
'access_token': access_token | |
} | |
r = requests.post('https://graph.facebook.com/v4.0/' + ad + '/copies', data=data_dump) | |
result = r.json() | |
if 'copied_ad_id' in result: | |
print('Added ad: ' + ad + ' to Adset: ' + adset['id']) | |
else: | |
print('Error Added ad: ' + ad + ' to Adset: ' + adset['id'] + ', Error: ' + result['error']['message']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment