9

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?

Mitul Shah
  • 1,551
  • 1
  • 12
  • 33
  • 2
    Why not just ask for the `/insights` on the account object, and specify ?level=campaign to get the results broken down by campaign? – Igy Jan 06 '16 at 00:03
  • @Igy and same for ads, I used level=ad and now I want destination url also, how can I get using /insight? – Mitul Shah Jan 06 '16 at 09:45
  • @Igy thanks for suggestion, it works, now only have issue with destination url (object url), I can't able to use insight with ad_creatives. can you please also help me here? – Mitul Shah Jan 06 '16 at 10:17
  • For that you'll still need to make a separate request to get the ad details, it's not part of the output of /insights – Igy Jan 06 '16 at 17:50
  • @Igy how can I merge separate response to ads? I want each URL with its Ads. If possible can you write sample code? – Mitul Shah Jan 07 '16 at 02:46
  • @Igy there is same issue happening, user request limit reached. First I'm trying to get creative ID using `get_ad_creatives` and then for each id again sending `remote_read(fields=fields)` and just because of multiple Ads are there, my user request limit reached. – Mitul Shah Jan 07 '16 at 02:58
  • @Igy Is it possible to get creative_id using `get_insights` functions for all ads? I'm using `account.get_insights()` to get performance report for all ads. – Mitul Shah Jan 11 '16 at 15:24
  • no, the creative ID is not part of the insights API - for fields about the ads which aren't part of the insights API you'd need to make additional calls to fetch those fields – Igy Jan 11 '16 at 18:44

5 Answers5

6

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

ssoler
  • 3,886
  • 4
  • 27
  • 31
maleta
  • 445
  • 4
  • 15
3

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)
Mitul Shah
  • 1,551
  • 1
  • 12
  • 33
  • Hey this looking like what I need. How would I implement this when getting information for multiple ad accounts. How do I pass it the ad account id? – LucyTurtle Jun 25 '18 at 21:10
1

I have been doing something similar. I set the level to be ad - see example code here: http://www.samk3nny.com/getting-ad-insights-from-facebook-and-avoiding-error-17/

And then for each ad get the creatives:

ad = Ad(fbid=ad_id)
creatives = ad.get_ad_creatives(fields=[AdCreative.Field.id,
                                        AdCreative.Field.object_story_spec,              
                                        AdCreative.Field.object_story_id])
Sam Kenny
  • 365
  • 4
  • 9
  • your first answer is correct which is same answered by @igy in comment, your second solution is not satisfied, as we have more than 50 ads and your answer will send multiple request to Facebook, we can't send multiple requests as u did – Mitul Shah Jan 15 '16 at 07:29
  • BTW +1 for good answer. I solved it earlier already, but it will definitely helps to others :) – Mitul Shah Jan 15 '16 at 07:40
0

Some facebook graph API coding I found while digging around Business Manager.

Add the Facebook graph prefix

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

Followed by an Ad ID (note Ad ID and Ad Creative ID are different)

Followed by 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 your &access_token=WDVW##$Y#%G@$f

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

"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 your Ad Acct ID and then use filtering to filter by Ad ID to show several Ads at once under the one Acct Id.

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

Replace _ENTERADIDHERE_ with your Ad ID to pull multiple 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 get somebody on their way.

Neil
  • 13,042
  • 2
  • 26
  • 48
0

Don't know if you still need it.

The main solution lies in calling the insights at ad level. That restricts the number of API calls. Just added 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)
Rafael
  • 1,568
  • 3
  • 16
  • 25