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

Digital Marketing Facebook Marketing API 3 years ago

8.24K 1 0 0 0

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

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