Reputation: 193
Following the doc here
https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group/insights/
I'm querying insights on ad performance etc. However, I can't make the time_frame work. A query like this
just returns lifetime stats. I've tried to modify the query in many ways, but it's always an error or lifetime.
Any help would be highly appreciated!
Thanks
Upvotes: 12
Views: 15317
Reputation: 487
If you are doing this in the Graph API Explorer, it should be like below:
I get #2 syntax from Facebook's document itself :|
Upvotes: 4
Reputation: 1902
I kept getting an HTML error page as a response until I finally realized that a space after the comma was making this API unhappy.
"time_range={since:'2020-06-12',until:'2020-06-14'}" works fine.
"time_range={since:'2020-06-12', until:'2020-06-14'}" causes much suffering.
Upvotes: 0
Reputation: 145880
This one gets me every time. Here's a URL that worked in the Graph API.
act_2222222/insights?fields=unique_actions,actions&level=account&time_ranges=[{since:'2020-06-12',until:'2020-06-12'}]
2222222 is your ad account
Upvotes: 0
Reputation: 1
As none of the solutions mentioned above worked for me, although it is a very very late response. How I tried is below:
dates = 'since='+start_date+'&until='+end_date
requests.get("https://graph.facebook.com/" + BusinessManagerConfig.version + key + \
"/insights?metric=" + ','.join(BusinessManagerConfig.metrics) + \
"&period=day&" + dates + \
"&access_token="+page_token + '&limit=5')
I hope it helps others
Upvotes: 0
Reputation: 149
yes Facebook didn't provide good document for this. I also struggled a lot. we can use time range to fetch the data for specific date or date range.
Inside Graph API you can provide like below me?fields=id,name,adaccounts{campaigns{adsets{ads{insights.time_range({'since':'2020-05-01','until':'2020-05-01'}){adset_id,campaign_id,ad_id,clicks}}}}}
note the syntax from insights.time_range({'since':'2020-05-01','until':'2020-05-01'}).
once you provide above code and submit, fb provides the codes for different languages such as JavaScript, ios , android etc.
curl will looks like below
curl -i -X GET \ "https://graph.facebook.com/v6.0/me?fields=id%2Cname%2Cadaccounts%7Bcampaigns%7Badsets%7Bads%7Binsights.time_range(%7B'since'%3A'2020-05-01'%2C'until'%3A'2020-05-01'%7D)%7Badset_id%2Ccampaign_id%2Cad_id%2Cclicks%7D%7D%7D%7D%7D&access_token="
Upvotes: 3
Reputation: 136
Another alternative is to pass the time_range
object as below if you don't want to encode the URL
and want to keep the URL clean.
Facebook is smart enough to parse the URL and set the correct time_range
.
https://httpbin.org/get?time_range[since]=2019-08-10&time_range[until]=2019-09-09
Upvotes: 4
Reputation: 166
I imagine you've found the answer by now but this could help someone else as from experience the documentation isn't always the most friendly to jump into.
You'll need to format it as a time_range object, the example object below is from that page of the documentation you included. It's essentially just an object with 2 properties- since and until.
{'since'=>YYYY-MM-DD,'until'=>YYYY-MM-DD}
So from the example URL you're using, rather than setting the since and until values using individual parameters, the time range would be set like this (I've URL encoded the object):
time_range=%7B%22since%22%3A%22YYYY-MM-DD%22%2C%22until%22%3A%22YYYY-MM-DD%22%7D
Upvotes: 15