Search code examples
phpfacebookfacebook-php-sdkfacebook-marketing-api

Facebook Marketing API - Accessing Ad Impressions


I have recently been looking at the Facebook Marketing API and I'm looking to retrieve the impressions via the ads. I know that you used to be able to use getStats( *fields*, *params* ) however that was deprecated in 2.4.

So I'm looking for an alternative method. So far I have managed to obtain the ad object:

<?php
/* Require autoload */

$_CONFIG = array(
    'accountID' => '****',   
    'campaignID' => '****',   
    'accessToken' => '****',   
    'appSecret' => '****',   
    'appID' => '****',  
);

$appsecret_proof = hash_hmac('sha256', $_CONFIG['accessToken'], $_CONFIG['appSecret']);  

use FacebookAds\Api;
Api::init($_CONFIG['appID'], $_CONFIG['appSecret'], $_CONFIG['accessToken'], $appsecret_proof);

use FacebookAds\Object\AdUser;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdCampaignFields;
use FacebookAds\Object\Fields\AdGroupFields;
use FacebookAds\Object\AdGroup;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\AdSet;

$me = new AdUser('me');
$accounts = $me->getAdAccounts();

// Output account ID
foreach ($accounts as $account) {
  echo $account_id = $account->id;
   echo "----";
}

$account = new AdAccount($account_id);

$fields = array(
  AdCampaignFields::NAME,
  AdCampaignFields::OBJECTIVE,
  AdCampaignFields::STATUS,
);

$campaigns = $account->getAdCampaigns($fields);
?>

<table border="1" bordercolor="#6699CC" style="width:100%">
<tr>
<th bgcolor="#336699" style="color:#FFF">Campaign_id</th>
<th bgcolor="#336699" style="color:#FFF">Campaign Name</th>
<th bgcolor="#336699" style="color:#FFF">Status</th>
<th bgcolor="#336699" style="color:#FFF">Impressions</th>
<th bgcolor="#336699" style="color:#FFF">Clicks</th>
<th bgcolor="#336699" style="color:#FFF">Spent</th>
</tr>

<?PHP
foreach ($campaigns as $campaign) {
//---------------------------------
?>

<tr>
<td><?php echo $campaign->id; ?></td>
<td><?php echo $campaign->name; ?></td>
<td><?php echo $campaign->campaign_group_status ?></td>

<?php

$fields = array (
    'impressions',
    'clicks',
    'spent',
);

$params = array (
    'date_preset'=> 'yesterday',
    'data_columns'=>  "['adgroup_id','actions','spend']",
);

$adsets = $campaign->getAdSets(array(
  AdSetFields::NAME,
  AdSetFields::CAMPAIGN_STATUS,
));

// Output name of ad sets.
foreach ($adsets as $adset) {
  echo $adset->{AdSetFields::NAME}.PHP_EOL . ' -- ADSET --</br>';
    $adset = new AdSet($adset->id);
    $adgroups = $adset->getAdGroups(array(
      AdGroupFields::NAME,
    ));

    // Outputs names of Ad Groups.
    foreach ($adgroups as $adgroup) {
      //echo $adgroup->{AdGroupFields::NAME} . ' -- AD -- </BR>';
        echo $adgroup->id;
    }
}

}
?>

However, I cannot seem to find any trace of impressions through their updated API. Could somebody please help me?

Many thanks in advance!


Solution

  • In general, since v2.4 all reports are accessed off the /insights edge of the appropriate ad object (ad, ad set, campaign, ad account) - https://developers.facebook.com/docs/marketing-api/insights/v2.4

    For just the lifetime impressions of a specific ad, this API call will return it (ID ommitted):

    /v2.4/602687013XXXX/insights?fields=impressions

    Response:

    {
      "data": [
        {
          "impressions": "2535",
          "date_start": "2015-09-09",
          "date_stop": "2015-09-16"
        }
    

    You can set your own date ranges, breakdowns, other fields, etc too

    If you're using that Ads-specific PHP SDK and not the main SDK, there's sample code in the documentation too - the equivalent call to my API call above would be

    $params = array ( 'fields' => 'insights' );
    $insights = $ad->getInsights(null, $params);
    print_r ($insights);