Reputation: 545
I'm building an analytics tool for some Facebook pages where I can view page information and statistics.
The problem is that it takes a long time to load the data using FQL, almost 50 seconds to load the page fully. The problem is that it needs to request a query for each day when I want to know all the new likes within a month and then count them all up.
These are the functions I'm using:
function getmetrics($objectid, $metrics, $end_time, $period){
global $facebook;
$fql = "SELECT value FROM insights WHERE object_id=$objectid AND metric='$metrics' AND end_time=end_time_date('$end_time') AND period=$period";
$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));
if (!empty($response)){
return $response[0]['value'];
}
else{
return "no results found";
}
}
function getdailymetrics($objectid, $metrics, $end_time , $days ){
global $facebook;
$total = 0;
do {
$amount = getmetrics($objectid, $metrics, $end_time, '86400');
$total += $amount;
$pieces = explode("-", $end_time);
$end_time = date("Y-m-d", mktime(0, 0, 0, date($pieces[1]),date($pieces[2])-1,date($pieces[0])));
$days--;
} while($days > 1);
return $total;
}
On the front end:
<table>
<tr>
<td>Total likes</td>
<?php
$like1 = getmetrics($samsung, 'page_fans', '2012-01-29', '0');
$like2 = getmetrics($samsung, 'page_fans', '2012-02-26', '0');
$likep = round((($like2/$like1)-1)*100, 2);
?>
<td><?php echo $like1; ?></td>
<td><?php echo $likep; ?>%</td>
<td><?php echo $like2; ?></td>
</tr>
<tr>
<td>New Likes</td>
<?php
$newlikes1 = getdailymetrics($samsung, 'page_fan_adds', '2012-01-29', '28');
$newlikes2 = getdailymetrics($samsung, 'page_fan_adds', '2012-02-26', '28');
$newlikep = round((($newlikes2/$newlikes1)-1)*100, 2);
?>
<td><?php echo $newlikes1; ?></td>
<td><?php echo $newlikep; ?>%</td>
<td><?php echo $newlikes2; ?></td>
</tr>
<tr>
<td>Daily New Likes</td>
<td><?php echo round($newlikes1/28); ?></td>
<td><?php echo $newlikep; ?>%</td>
<td><?php echo round($newlikes2/28); ?></td>
</tr>
</table>
Is there a way to speed this up or is there a more efficient way?
Upvotes: 0
Views: 198
Reputation: 545
I resolved the problem with a different method of fetching the data, using this function:
function getfromuntil($objectid, $metric, $start_time, $end_time){
global $facebook;
try {
$start_time = $timestamp = strtotime($start_time);
$end_time = $timestamp = strtotime($end_time);
$response = $facebook->api('/'.$objectid.'/insights/'.$metric.'?since='.$start_time.'&until='.$end_time);
} catch (FacebookApiException $e){
error_log($e);
}
$total = 0;
foreach ($response['data'][0]['values'] as $value) {
$total += $value['value'];
}
return $total;
}
Upvotes: 0
Reputation: 3305
You can try to reduce the amount of requests in two ways. First, you can use batch requests to send up to 50 queries to FB in one request. Also you can try to change your query, so that it would request insights for several days at once, like end_time in (values_list)
.
Also you could do some caching of the results.
And, though it depends on your certain case, maybe you could consider pulling insights with daemon (if you have the objects to pull data for), and storing it in the database.
Upvotes: 1