Facebook Marketing API - 访问广告展示次数

时间:2015-09-13 03:00:25

标签: php facebook facebook-php-sdk facebook-marketing-api

我最近一直在关注Facebook营销API,并希望通过广告检索展示次数。我知道您曾经能够使用getStats( *fields*, *params* ),但在2.4中已弃用。

所以我正在寻找另一种方法。到目前为止,我已设法获取广告对象:

<?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;
    }
}

}
?>

但是,我似乎无法通过更新的API找到任何印象的痕迹。有人可以帮帮我吗?

非常感谢提前!

1 个答案:

答案 0 :(得分:2)

一般情况下,自v2.4以来,所有报告均在相应广告对象{广告,广告集,广告系列,广告帐户}的/insights边缘访问 - https://developers.facebook.com/docs/marketing-api/insights/v2.4

对于特定广告的生命周期展示次数,此API调用将返回该值(ID已省略):

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

响应:

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

您也可以设置自己的日期范围,细分,其他字段等

如果您使用的是特定于广告的PHP SDK而不是主SDK,那么文档中的示例代码也是如此 - 上面对我的API调用的等效调用将是

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