Facebook ads api - get stats by day - facebook

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
);

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"),
),
),

Related

GraphMethodException - Unsupported get request at

I have two Debian-systems. With both I access the Facebook-API via Perl(Facebook::Graph).
On the first system this works perfectly, on the other system I ran into following problem.
Could not execute request (https://graph.facebook.com): GraphMethodException - Unsupported get request at /usr/local/share/perl/5.10.1/Facebook/Graph/Response.pm line 40
Response.pm:
[...]
has response => (
is => 'rw',
isa => 'HTTP::Response',
required=> 1,
);
[...]
ouch $response->code, 'Could not execute request ('.$response->request->uri->as_string.'): '.$message, $response->request->uri->as_string;
[...]
I know it has something to do with my ssl-libs, but I cannot figure out where the problem is. I reinstalled multiple times the IO:Socket:SSL and SSLeay.
Every help is welcome,
enzo
Don't know if this helps, but came on here looking for help with 'graphMethodException' when trying to access this page: http://graph.facebook.com/doodleembroidery (which works fine now).
Have just tracked it down to the page's country permissions. She didn't realise she had set restrictions when she created the page, now she's removed them the link works fine!

How to schedule the posts

I am currently developing a page application which will allow users to schedule their page posts. As given in facebook documentation, I have to use unix time stamp ,but since the application is stored in a server it will always create a unix time stamp using mktime() respective of the server time.
Now suppose different people from different places need to schedule posts, how can i schedule their post for their given time??
You have to set a timezone for creating time-stamp.
$timezone= ''Americas/New_York';
$date = new DateTime($dateStr, new DateTimeZone($timezone));
or
date_default_timezone_set('Americas/New_York');
$date = strtotime($dateStr);
$attachment = array(
'message' => $data['facebook_text'],
'name' => $data['name'],
'link' => $this->getLinkToLatestNews(),
'description' => '',
'scheduled_publish_time' => $date
);
$facebook->api('/PAGE ID/feed/', 'post', $attachment);
Note: Time when the page post should go live, this should be between 10 mins and 6 months from the time of publishing the post.
See:
http://www.php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/datetime.construct.php
Facebook Docs

Why does Facebook Credits callback payments_get_items occasionally not pass user ID?

Our facebook canvas app is having this problem where now, several times a day, the credits callback is passing a signed_request with the following contents:
Array
(
[algorithm] => HMAC-SHA256
[credits] => Array
(
[order_id] => 9005967273834
[order_info] => "item104"
)
[issued_at] => 1319329443
[user] => Array
(
[country] => do
[locale] => es_LA
[age] => Array
(
[min] => 0
[max] => 12
)
)
)
Notice anything missing? That's right! No user_id, buyer, or receiver is given!
We can't tell facebook what the price or description of an item is without knowing who is receiving the item.
This seems like a bad bug! This problem started happening on Oct 11, 2011
This is definitely an intermittent bug, so the only work-around is to pass the user ID from your own code into the order_info field of the item. Then use that instead of the credits receiver if the user information is missing.
I don't like that I have to do this, but it seems necessary.
I noticed this as well on my app and from what I can see the missing user IDs happen when Facebook indicates an age bracket of min 0 and max 12 years for the user. I assume that this might be the result of under 13 year olds being prevented from making purchases with Facebook credits.
Which makes me wonder what they are doing on Facebook to start with...

Errors with: https://graph.facebook.com/app_id/insights/share_views/day?access_token=

I am trying to get the number of users who have seen pages shared from my site using: https://graph.facebook.com/app_id/insights/share_views/day?access_token=
I am filling in my app_id and access_token and I am getting an error response of:
Array ( [error] => Array ( [type] => OAuthException [message] => (#604) Invalid metric name: share_views ) )
Has anyone run into this error and determined the cause?
I believe you need to actually specify time period you are trying to access. For an example of an application that is working and can access Insight data, check here https://github.com/facebook/insights-api-sample.
Notice that they specify a since and until post parameter in this file:
https://github.com/facebook/insights-api-sample/blob/master/php/results.php

Are data acquired from Facebook GRAPH API real-time?

I am creating a Facebook application that will download all the photos in an album. The application is created for my personal use and at the same time, learn the Facebook API and JSON.
I can already retrieve the URL of the photos inside an album by calling this url:
http://graph.facebook.com/[album id]/photos?fields=source
The album that I'm trying to download contains 5400+ photos so I tried increasing the limit by adding the limit parameter:
http://graph.facebook.com/[album id]/photos?fields=source&limit=1000
Here's the problem:
The results being returned are only until 2010-07-30T11:20:11+0000. When I tried to modify the query by using the until parameter like so:
http://graph.facebook.com/[album id]/photos?fields=source,link&limit=1000&until=2010-06-01
the data responded correctly. However, if I changed the date to something like 2010-08-05, the latest photo returned will have a created_date of 2010-07-30T11:20:11+0000.
The last photo returned is photo #5000 out of 5695.
Here's my question:
Is the data acquired from Facebook GRAPH Api real-time (or a Monthly update, 2010-07-30)? Or there's just a limit on the number of photos returned on album (like 5000)?
Thanks!
EDIT
There is a 5000 object limit in Facebook. If you know how to break the limit, go here:
Breaking the 5000 object limit in Facebook API
Thanks!
There is indeed 5000 limit on returned objects. You would need to run multiple FQL queries on photo table ordering and limiting the results (<5000) to get all the data (check photo table page for examples). (doesn't work)
When I look at the photos in the graph query you linked
https://graph.facebook.com/119403264763178/photos
I see paging information at the bottom. So, hacking together a quick test
$request = 'http://graph.facebook.com/119403264763178/photos?fields=source&limit=1000';
$response = json_decode( file_get_contents( $request ), true );
$totalCount = 0;
while ( count( $response['data'] ) )
{
echo 'Fetching ' . urldecode( $response['paging']['next'] ) . '<br>';
$totalCount += count( $response['data'] );
$response = json_decode( file_get_contents( $response['paging']['next'] ), true );
}
echo $totalCount;
It would seem that even following the paging data, you can still only retrieve 5000 records.
I'd suggest hitting up the FB forums or opening a bug ticket.