O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Metrics

Problem

I want to retrieve my app’s daily metrics for yesterday.

Solution

The Admin.getMetrics() method will return metrics for daily, weekly, or monthly periods:

$targetMetrics = array('active_users');
$monthAgo = strtotime('yesterday') - 24*60*60*29;
$yesterday = strtotime('yesterday');

$oneDay = $facebook->api_client->admin_getMetrics($monthAgo,
 $yesterday, 86400, $targetMetrics);
$sevenDays = $facebook->api_client->admin_getMetrics($monthAgo,
 $yesterday, 604800, $targetMetrics);
$thirtyDays = $facebook->api_client->admin_getMetrics($monthAgo,
 $yesterday, 2592000, $targetMetrics);

Discussion

Admin.getMetrics() accepts start and end dates, expressed in epoch time, a period, and an array containing a list of the specific metrics you’re looking for (you have to use an array, even if you only want one back, or you’ll get a long error message about needing an array). The period parameter, also expressed in epoch seconds, defines the period you want to receive metrics for: one day (86400 seconds), seven days (604800 seconds), or 30 days (2592000 seconds). See Formatting Relative Time for more information about epoch time. Your return value will always be a multidimensional array (which makes sense, since you’re requesting an array), with each item containing a date and the specified metric. If you requested a one-day period of active_users and did a PHP print_r on that array, you might see:

Array
(
    [0] => Array
        (
            [active_users] => 1234340
        )
)

There are quite a few metrics you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required