How can I get more than 25 results from Facebook's insights api using Facebook-business-python-sdk?

三世轮回 提交于 2019-12-23 04:06:15

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/56865103/how-can-i-get-more-than-25-results-from-facebooks-insights-api-using-facebook-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!