Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Marketing API - time_range

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

https://graph.facebook.com/v2.5/_CAMPAIGN_ID_/insights?access_token=AT&time_range&since=2015-11-08&until=2015-11-12

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

like image 800
Thomas Lindegaard Jensen Avatar asked Dec 14 '15 10:12

Thomas Lindegaard Jensen


3 Answers

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

like image 137
biscuitdan Avatar answered Sep 18 '22 15:09

biscuitdan


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
like image 41
being_j Avatar answered Sep 16 '22 15:09

being_j


If you are doing this in the Graph API Explorer, it should be like below:

  1. Work: time_range={since:'2021-05-01',until:'2021-05-05'}
  2. NOT Work: time_range={'since':2021-05-01,'until':2021-05-05}

I get #2 syntax from Facebook's document itself :|

like image 20
Toàn Đoàn Avatar answered Sep 16 '22 15:09

Toàn Đoàn