使用facebook marketing api调用成员函数getData()

时间:2015-12-07 13:41:45

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

我目前正在测试facebook营销API,以便使用它从自定义信息中心的广告系列中获取数据。

我可以从广告帐户中获取数据,但对于某些人来说,不明原因我在获取数据时获得了Call to a member function getData() on a non-object。它将在退出之前输出一些数据。

我正在测试

if($_SESSION["facebook_access_token"]) {

    Api::init(
        'XXXXXXXXXX',
        'XXXXXXXXXXXXXXXXX',
        $_SESSION["facebook_access_token"]
    );

    $account = new AdAccount('act_XXXXXXXXXXXXX');

    $campaigns = $account->getCampaigns(array(
            CampaignFields::NAME,
            CampaignFields::OBJECTIVE,
            CampaignFields::EFFECTIVE_STATUS,
            CampaignFields::CONFIGURED_STATUS
    ), array(
        CampaignFields::EFFECTIVE_STATUS => array(
            ArchivableCrudObjectEffectiveStatuses::ACTIVE
        ),
    ));

    foreach($campaigns as $campaignset) {

        $cid = $campaignset->getData()['id'];

        $campaign = new Campaign($cid);

        $insights = $campaign->getInsights(array(
           InsightsFields::CAMPAIGN_NAME,
           InsightsFields::ADSET_NAME
        ));

        var_dump($insights->current()->getData());

    }


}

它将输出此

array (size=48)
  'account_id' => null
  'account_name' => null
  'action_values' => null
  'actions' => null
  'actions_per_impression' => null
  'ad_id' => null
  'ad_name' => null
  'adset_id' => null
  'adset_name' => null
  'call_to_action_clicks' => null
  'campaign_id' => null
  'campaign_name' => string 'Svendborg - Mødsparnord – kopi' (length=33)
  'cost_per_action_type' => null
  'cost_per_total_action' => null
  'cost_per_unique_click' => null
  'cost_per_inline_link_click' => null
  'cost_per_inline_post_engagement' => null
  'cpm' => null
  'cpp' => null
  'ctr' => null
  'date_start' => string '2015-11-05' (length=10)
  'date_stop' => string '2015-12-07' (length=10)
  'frequency' => null
  'impressions' => null
  'inline_link_clicks' => null
  'inline_post_engagement' => null
  'product_id' => null
  'reach' => null
  'relevance_score' => null
  'social_clicks' => null
  'social_impressions' => null
  'social_reach' => null
  'spend' => null
  'total_action_value' => null
  'total_actions' => null
  'total_unique_actions' => null
  'unique_clicks' => null
  'unique_ctr' => null
  'unique_social_clicks' => null
  'video_avg_pct_watched_actions' => null
  'video_avg_sec_watched_actions' => null
  'video_complete_watched_actions' => null
  'video_p100_watched_actions' => null
  'video_p25_watched_actions' => null
  'video_p50_watched_actions' => null
  'video_p75_watched_actions' => null
  'video_p95_watched_actions' => null
  'website_ctr' => null

以及其他一些广告系列,但每次都会在同一地点停止上述错误。但我看不出有什么不对。

1 个答案:

答案 0 :(得分:0)

这是因为我们的某个广告系列没有您查询的数据分析数据。您可以将其作为循环中的异常处理。

但是,我想为此推广最佳做法。在您的使用案例中,您应该真正使用广告帐户中的级别参数,除非您在该帐户下有数百个广告系列。

$adAccount = new AdAccount('<AD_CAMPAIGN_ID>');
$params = array(
  'level' => AdsInsightsLevelValues::Campaign,
  'date_preset' => InsightsPresets::LAST_7_DAYS,
);
$insights = $adAccount->getInsights(null, $params);
print_r($insights);

这样你甚至不需要for循环。

我们还有一个工具可以指导您在https://developers.facebook.com/apps/[app_id]/marketing-api/的入门会话中生成代码 基本上你可以选择指标,向导将为你生成一个工作代码。 (它现在只生成Java代码,但代码模式是相同的)