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

Digital Marketing Facebook Marketing API 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_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

Posted on 16 Aug 2022, this text provides information on Facebook Marketing API related to Digital Marketing. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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

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.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community