user2760338
user2760338

Reputation: 235

Facebook ads api - get stats by day

I can successfully get campaign stats between particular dates via the API however I need that broken down day by date rather than a total.

I can see it works for the page insights API but there doesn't seem to be documentation on the ad insights API.

I'm using the following call to get the total between dates.

https://graph.facebook.com/v2.2/act_/stats?access_token=&start_time=&end_time=

However I cannot find the documentation to break it down into day by day for one query.

This answer had a solution but didn't work.

Downloading Facebook ads statistics in background (no web browser)

Thanks!

===================================================

New info:

So I can use time_increment=1 to get a break down by day however this only works with 'date_preset' however I want to set a date range. I am using the latest API

https://graph.facebook.com/v2.2/act_{$this->accountID}/reportstats

Using 'time_ranges' will merge the data regardless of using time_increment=1

Using 'date_preset' eg last_28_days does work with time_increment.

Using 'time_interval' with midnight timestamps of my timezone (as the documentation suggested) throws the following error:

[error] => stdClass Object ( [message] => (#100) The "time_start" and "time_stop" must be integer. [type] => OAuthException [code] => 100 )

They are integers! Here's my complete post data

$postData = array(

         'async'=>'true',

         'data_columns'=>$data_columns,
       /* 
         'time_ranges'=>array(
             array(
                 'day_start'=>array(
                     'day'=>$startDate->format("d"),
                     'month'=>$startDate->format("m"),
                     'year'=>$startDate->format("Y"),
                 ),
                 'day_stop'=>array(
                     'day'=>$endDate->format("d"),
                     'month'=>$endDate->format("m"),
                     'year'=>$endDate->format("Y"),
                 ),
             ),
         ),
    */
         'actions_group_by'=>array('action_type'),




         'time_interval'=>array(
                'time_start'=>$startDate->getTimestamp() ,
                'time_stop'=>$endDate->getTimestamp(),
             ),

         //'date_preset' => 'last_28_days',

         'time_increment'=>'1',

         'filters'=>$filters,

         'access_token'=>$this->access_token


        );

Upvotes: 1

Views: 3698

Answers (1)

user2760338
user2760338

Reputation: 235

Ok I had to change the format time_interval to the time_range format and it worked! The documentation says timestamps will work but it doesn't, this worked:

 'time_interval'=>array(
                 'day_start'=>array(
                     'day'=>$startDate->format("d"),
                     'month'=>$startDate->format("m"),
                     'year'=>$startDate->format("Y"),
                 ),
                 'day_stop'=>array(
                     'day'=>$endDate->format("d"),
                     'month'=>$endDate->format("m"),
                     'year'=>$endDate->format("Y"),
                 ),
                ),

Upvotes: 1

Related Questions