Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get insights for all campaigns in single query + Facebook marketing api

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?

like image 913
Mitul Shah Avatar asked Jan 05 '16 08:01

Mitul Shah


People also ask

How do I get Facebook Page insights on graph API?

You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.

How do I find my campaign data on Facebook?

Navigate to your Ads Manager and find the Reports button in the top-right of your reporting table. To export data as a CSV or Excel file, click Reports and then select Export Table Data, choose the type of file you want and then click Export.


2 Answers

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

like image 51
maleta Avatar answered Sep 20 '22 10:09

maleta


To get all ads link in a single request, I've solved my issues with below code.

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)
like image 31
Mitul Shah Avatar answered Sep 20 '22 10:09

Mitul Shah