how to get insights for all campaigns in single query + Facebook marketing api

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 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? //act_/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?

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

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