How to get information for all campaigns in one request + marketing Facebook api

I want to write a query in Python, I want all the campaign performance data in one query.

how to convert below request api chart in python query?

/<version>/act_<ACT_ID>/campaigns?fields=insights.fields(actions_results) 

I tried using the below requests, but it’s the wrong idea to send several times to send a request to Facebook, and Facebook blocks the user for 30 minutes.

 fields = [Insights.Field.cpm, Insights.Field.cpp] class Fb_insights(object): def __init__(self, app_id, app_secret, access_token): FacebookAdsApi.init(app_id, app_secret, access_token) # Add after FacebookAdsApi.init me = AdUser(fbid='me') self.my_account = me.get_ad_accounts()[0] def campaign_reports(self, since, until): params = { 'level': Insights.Level.campaign, 'time_range': { 'since': since, 'until': until, }, } for campaign in self.my_account.get_campaigns(): for stat in campaign.get_insights(fields=fields, params=params): print(stat) 

It’s bad that I send requests by calling get_insights () for each campaign.

UPDATE

I also tried to get direct information. Below the code, only one detail of the campaign is returned, while I have 1 active campaign and 87 Not delivering campaigns, also update level=campaign in the parameters

 for insight in self.my_account.get_insights(fields=fields, params=params): print insight 

Request: Using my updated code, how can I get all delivered and not delivered campaigns using a single request?

+6
source share
5 answers

To get a link to all the ads in one request, I solved my problems using the code below.

 class FB(object): def __init__(self): me = AdUser(fbid='me') self.my_account = me.get_ad_accounts()[0] def fb_creativies(self, since, until): """ This function is used to get destination url for each creative object """ fields = [AdCreative.Field.object_story_spec] params = { 'time_range': { 'since': since, 'until': until, }, } return self.my_account.get_ad_creatives(fields=fields, params=params) 
+3
source

Take a look at the https://developers.facebook.com/docs/graph-api/using-graph-api section "Creating Subqueries".

You can get information about all campaigns in one request for an account using field extensions by calling the following api:

 https://graph.facebook.com/v2.10/YOUR_ACCOUNT_ID/campaigns?fields=name,status,insights{reach,impressions,clicks}&access_token=YOUR_TOKEN 

Field extensions let you specify field requests on a node. In the above example, I get information for all the campaigns in my account, grouped by campaign, and then with the help of "insights (reach, impressions, clicks)" you can select the fields for the node level.

EDIT: Deleted level = campaign from URL because data is already grouped by campaign due to endpoint / campaigns

+3
source

I did something like that. I set the level to show, for example, an example code: http://www.samk3nny.com/getting-ad-insights-from-facebook-and-avoiding-error-17/

And then create ads for each ad:

 ad = Ad(fbid=ad_id) creatives = ad.get_ad_creatives(fields=[AdCreative.Field.id, AdCreative.Field.object_story_spec, AdCreative.Field.object_story_id]) 
+1
source

Some facebook coding API that I discovered while delving into Business Manager.

Add Facebook Graphics Prefix

 https://graph.facebook.com/v2.8/ 

Following the ad ID (note the ad id and ad id)

The following is this code:

 ?fields=%5B%22insights.date_preset(lifetime).action_attribution_windows(%5B%5C%22default%5C%22%5D).default_summary(false).fields(%5B%5C%22impressions%5C%22%2C%5C%22reach%5C%22%2C%5C%22frequency%5C%22%2C%5C%22actions%5C%22%5D).limit(5000).summary_action_breakdowns(%5B%5C%22action_type%5C%22%5D)%22%5D 

Then add &access_token=WDVW##$Y#% G@ $f

It will display impressions, reach, frequency, and a list of action values ​​separated by type of action.

 "insights": {"data": [ { "impressions": "963986", "reach": "335872", "frequency": "2.870099", "actions": [ {"action_type": "comment", "value": "133"}, {"action_type": "like","value": "60"}, {"action_type": "link_click","value": "4652"}, {"action_type": "post","value": "76"}, {"action_type": "post_reaction","value": "516"}, {"action_type": "unlike","value": "1"}, {"action_type": "page_engagement","value": "5437"}, {"action_type": "post_engagement","value": "5377"} ], "date_start": "2016-05-31", "date_stop": "2016-06-30" 

BUT, you can also use the Ad Acct ID , and then use filtering to filter using the Ad ID to show multiple ads at once under the same Acct ID.

For example: https://graph.facebook.com/v2.8/YOURADACCOUNTID?fields....etc above, and then the code below.

Replace _ENTERADIDHERE_ with your ad ID to pull a few at a time.

 &filtering=%5B%7B%22field%22%3A%22ad.id%22%2C%22operator%22%3A%22IN%22%2C%22value%22%3A%5B%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%2C%22_ENTERADIDHERE_%22%5D%7D%5D 

Hope this helps someone along the way.

0
source

I don’t know if he needs it.

The main solution is to raise ideas at the ad level. This limits the number of API calls. Just add a line to your code:

 class Fb_insights(object): def __init__(self, app_id, app_secret, access_token): FacebookAdsApi.init(app_id, app_secret, access_token) # Add after FacebookAdsApi.init me = AdUser(fbid='me') self.my_account = me.get_ad_accounts()[0] def campaign_reports(self, since, until): params = { 'level': 'ad', 'date_preset': 'lifetime', 'fields': ['account_id', 'account_name', 'actions', 'ad_id', 'ad_name', 'adset_id', 'adset_name', ] } insights = account.get_insights(params=params) 
0
source

All Articles