I want to write query in Python, I want all campaigns performance details in single request.
how to convert below graph api request in Python Query?
/<version>/act_<ACT_ID>/campaigns?fields=insights.fields(actions_results)
I'd tried using below queries, but it is wrong idea to send multiple times to send request to Facebook, and also Facebook blocks 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)
Bad thing is I'm sending requests by calling "get_insights()" for each campaign.
UPDATE
I also tried to fetch directly insights, Below code returns only 1 campaign detail while I've 1 active campaigns and 87 Not Delivering campaign, also update level=campaign
in params
for insight in self.my_account.get_insights(fields=fields, params=params):
print insight
Query: By using my updated code, How can I get all delivered and non-delivered campaigns using single query?
Take a look at https://developers.facebook.com/docs/graph-api/using-graph-api section "Making Nested Requests".
You can get insights for all campaigns in single query for account using field expansions by calling following api:
https://graph.facebook.com/v2.10/YOUR_ACCOUNT_ID/campaigns?fields=name,status,insights{reach,impressions,clicks}&access_token=YOUR_TOKEN
Field expansions allows you to set field requests to node. In example above I am getting insights for all campaigns in account grouped by campaign and then with 'insights{reach,impressions,clicks}' you can select fields for node level.
EDIT: Removed level=campaign from URL because data is already grouped by campaign because of endpoint /campaigns