Skip to content

Instantly share code, notes, and snippets.

@moacirmoda
Last active August 1, 2023 19:38
Show Gist options
  • Save moacirmoda/ebdc1310935e0ca67605b3f20f5e3e4b to your computer and use it in GitHub Desktop.
Save moacirmoda/ebdc1310935e0ca67605b3f20f5e3e4b to your computer and use it in GitHub Desktop.
PoC to get Ad Insights from Facebook
from time import sleep
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from django.conf import settings
my_app_id = settings.SOCIAL_AUTH_FACEBOOK_KEY
my_app_secret = settings.SOCIAL_AUTH_FACEBOOK_SECRET
user = User.objects.get(email="[email protected]")
my_access_token = user.facebook_access_token
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
# conta do Tintim
ad_account = AdAccount("act_613083973947880")
fields = ['account_currency', 'account_id', 'account_name', 'action_values', 'actions', 'ad_click_actions', 'ad_id', 'ad_impression_actions', 'ad_name', 'adset_id', 'adset_name', 'age_targeting', 'attribution_setting', 'auction_bid', 'auction_competitiveness', 'auction_max_competitor_bid', 'buying_type', 'campaign_id', 'campaign_name', 'canvas_avg_view_percent', 'canvas_avg_view_time', 'catalog_segment_actions', 'catalog_segment_value', 'catalog_segment_value_mobile_purchase_roas', 'catalog_segment_value_omni_purchase_roas', 'catalog_segment_value_website_purchase_roas', 'clicks', 'total_postbacks_detailed_v4', 'conversion_values', 'conversions', 'converted_product_quantity', 'converted_product_value', 'cost_per_15_sec_video_view', 'cost_per_2_sec_continuous_video_view', 'cost_per_action_type', 'cost_per_ad_click', 'cost_per_conversion', 'cost_per_dda_countby_convs', 'cost_per_inline_link_click', 'cost_per_inline_post_engagement', 'cost_per_one_thousand_ad_impression', 'cost_per_outbound_click', 'cost_per_thruplay', 'cost_per_unique_action_type', 'cost_per_unique_click', 'cost_per_unique_inline_link_click', 'cost_per_unique_outbound_click', 'cpc', 'cpm', 'cpp', 'created_time', 'ctr', 'date_start', 'date_stop', 'dda_countby_convs', 'dda_results', 'estimated_ad_recall_rate_lower_bound', 'estimated_ad_recall_rate_upper_bound', 'estimated_ad_recallers_lower_bound', 'estimated_ad_recallers_upper_bound', 'total_postbacks_detailed_v4', 'frequency', 'full_view_impressions', 'full_view_reach', 'gender_targeting', 'total_postbacks_detailed_v4', 'impressions', 'inline_link_click_ctr', 'inline_link_clicks', 'inline_post_engagement', 'instagram_upcoming_event_reminders_set', 'instant_experience_clicks_to_open', 'instant_experience_clicks_to_start', 'instant_experience_outbound_clicks', 'interactive_component_tap', 'labels', 'location', 'mobile_app_purchase_roas', 'objective', 'optimization_goal', 'outbound_clicks', 'outbound_clicks_ctr', 'place_page_name', 'total_postbacks_detailed_v4', 'purchase_roas', 'qualifying_question_qualify_answer_rate', 'reach', 'total_postbacks_detailed_v4', 'social_spend', 'spend', 'updated_time', 'video_30_sec_watched_actions', 'video_avg_time_watched_actions', 'video_continuous_2_sec_watched_actions', 'video_p100_watched_actions', 'video_p25_watched_actions', 'video_p50_watched_actions', 'video_p75_watched_actions', 'video_p95_watched_actions', 'video_play_actions', 'video_play_curve_actions', 'video_play_retention_0_to_15s_actions', 'video_play_retention_20_to_60s_actions', 'video_play_retention_graph_actions', 'video_time_watched_actions', 'website_ctr', 'website_purchase_roas', 'wish_bid']
params = {
"action_report_time": "impression",
"time_increment": 1,
"time_range": {
'since': '2023-07-01',
'until': '2023-08-01',
},
"level": "ad",
"date_preset": "last_30d",
"effective_status": ['ACTIVE','PAUSED','PENDING_REVIEW','DISAPPROVED','PREAPPROVED','PENDING_BILLING_INFO','CAMPAIGN_PAUSED','ARCHIVED']
}
insights = ad_account.get_insights(fields=fields, params=params)
print(insights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment