Skip to content

Instantly share code, notes, and snippets.

@alecbw
Last active September 29, 2020 19:15
Show Gist options
  • Save alecbw/d06d12eb5a7d0b3dcb2c938f0f329737 to your computer and use it in GitHub Desktop.
Save alecbw/d06d12eb5a7d0b3dcb2c938f0f329737 to your computer and use it in GitHub Desktop.
Returns the ads that were in paused campaigns between time_start and time_end
import requests
time_start = '2020-09-01'
time_end = '2020-09-01'
fields = ['website_ctr','reach','adset_name','frequency','action_values','campaign_name','unique_actions','unique_clicks','video_avg_percent_watched_actions','video_p75_watched_actions','spend','cpc','video_p25_watched_actions','canvas_avg_view_time','canvas_avg_view_percent','campaign_id','video_p50_watched_actions','ctr','cpm','cpp','unique_ctr','video_avg_time_watched_actions','ad_name','impressions','labels','video_p95_watched_actions','cost_per_10_sec_video_view','ad_id','adset_id','clicks','website_purchase_roas','location','actions','cost_per_unique_click']
fields = "['" + "','".join(fields) + "']"
api_url = 'https://graph.facebook.com/v8.0/' + os.environ['FB_ACCOUNT_ID'] + "/insights?"
api_url += "level=ad"
api_url += '&limit=5'
api_url += "&breakdowns=hourly_stats_aggregated_by_advertiser_time_zone"
api_url += "&action_attribution_windows=['7d_click','1d_view']"
api_url += f"&time_range[since]={time_start}&time_range[until]={time_end}"
api_url += f"&fields={fields}"
api_url += f"&filtering=[{'field':'ad.effective_status','operator':'IN','value':['CAMPAIGN_PAUSED']}]"
resp = requests.get(api_url, headers={"Authorization": "Bearer " + os.environ["FB_TOKEN"])
print(resp.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment