2

I'm trying to get basic ad level data from Facebook's insights API using facebook-python-business-sdk with python 3.7. The problem is I'm only getting 25 results back, even in accounts that have more than 25 active ads.

I'm using the get_insights method on each account, passing a 'level':'ad' param and filtering on a specific date. I've also checked whether I've reached facebook's limit (using the explanation provided here) and I'm not even close to the limit. The get_insights method doesn't have a 'limit' param and either way I don't want to limit it at all as some accounts may have hundreds or even thousands of ads.

This is the code I'm using

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adaccountuser import AdAccountUser
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adsinsights import AdsInsights

access_token = '******'
app_secret = '******'
app_id = '******'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = AdAccountUser(fbid='me')
my_accounts = list(me.get_ad_accounts())

params={'time_range': {'since': '2019-06-29', 'until': '2019-06-29'},'level': 'ad'}
fields = [AdsInsights.Field.account_id,
           AdsInsights.Field.account_name,
           AdsInsights.Field.ad_id,
           AdsInsights.Field.ad_name,
           AdsInsights.Field.adset_id,
           AdsInsights.Field.adset_name,
           AdsInsights.Field.campaign_id,
           AdsInsights.Field.campaign_name,
           AdsInsights.Field.spend,
           AdsInsights.Field.impressions,
           AdsInsights.Field.clicks,
           AdsInsights.Field.outbound_clicks,
        ]

for account in my_accounts:
    ads = account.get_insights(params=params, fields=fields)
    print(ads)
    print(len(ads))

I expected to get all ads in each account but I'm only getting a maximum of 25 ads per account. Any help will be greatly appreciated!

Thanks

idoka
  • 81
  • 4

1 Answers1

6

The solution is just to add a 'limit' param, see below:

from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adaccountuser import AdAccountUser
from facebook_business.adobjects.campaign import Campaign as AdCampaign
from facebook_business.adobjects.adsinsights import AdsInsights

access_token = '******'
app_secret = '******'
app_id = '******'

FacebookAdsApi.init(app_id, app_secret, access_token)

me = AdAccountUser(fbid='me')
my_accounts = list(me.get_ad_accounts())

params={'time_range': {'since': '2019-06-29', 'until': '2019-06-29'},'level': 'ad', 'limit': '20000'}
fields = [AdsInsights.Field.account_id,
           AdsInsights.Field.account_name,
           AdsInsights.Field.ad_id,
           AdsInsights.Field.ad_name,
           AdsInsights.Field.adset_id,
           AdsInsights.Field.adset_name,
           AdsInsights.Field.campaign_id,
           AdsInsights.Field.campaign_name,
           AdsInsights.Field.spend,
           AdsInsights.Field.impressions,
           AdsInsights.Field.clicks,
           AdsInsights.Field.outbound_clicks,
        ]

for account in my_accounts:
    ads = account.get_insights(params=params, fields=fields)
    print(ads)
    print(len(ads))

Thanks to @Matteo

idoka
  • 81
  • 4