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

Digital Marketing Facebook Marketing API 3 years ago

4.33K 1 0 0 0

_x000D_ _x000D_ 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

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago
_x000D_ 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
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

What are the best SEO tools for a personal website? What website or tools should I use to build and...

What are the best SEO tools for a personal website? What website or tools should I use to build and...
Q

Why is keyword research important?

Why is keyword research important?
Q

What is SEO ?

What is SEO ?