检索帐户中所有有效广告的Facebook数据洞察?

时间:2017-03-03 16:40:34

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

我的代码允许我检索特定日期之间的所有广告,但这包括不再有效的广告。

我想仅针对ACTIVE广告展示见解。 如何检索帐户中所有有效广告的Facebook数据洞察?

my:command

我正在使用Facebook PHP广告SDK 2.8(public function getInsights($levelType, $id, $aggLevel, $start, $end) { if ($levelType) { if ($id == null) { abort(400, 'You must provide the ID for the object you want to retrieve.'); } } else { $levelType = \AdAccount::class; $id = ACT_PREPEND . $this->fbConfig['account_id']; $aggLevel = AdsInsightsLevelValues::CAMPAIGN; } $variableClassWithNamespace = '\FacebookAds\Object\\' . $levelType; //TODO: avoid hard-coding class paths as strings $level = new $variableClassWithNamespace($id); $fields = [ InsightsFields::SPEND, InsightsFields::CAMPAIGN_ID, InsightsFields::CAMPAIGN_NAME, InsightsFields::ADSET_ID, InsightsFields::ADSET_NAME, InsightsFields::AD_ID, InsightsFields::AD_NAME, InsightsFields::UNIQUE_IMPRESSIONS, InsightsFields::INLINE_LINK_CLICKS, InsightsFields::INLINE_LINK_CLICK_CTR, InsightsFields::COST_PER_INLINE_LINK_CLICK, InsightsFields::ACTIONS, InsightsFields::COST_PER_ACTION_TYPE, InsightsFields::CPM, ]; $params = [ AdReportRunFields::LEVEL => $aggLevel, ]; if ($start) { $params[AdReportRunFields::TIME_RANGE]['since'] = $start; if (!$end) { $params[AdReportRunFields::TIME_RANGE]['until'] = (new \DateTime("+2 year"))->format('Y-m-d'); } } if ($end) { $params[AdReportRunFields::TIME_RANGE]['until'] = $end; if (!$start) { $params[AdReportRunFields::TIME_RANGE]['since'] = (new \DateTime("-1 year"))->format('Y-m-d'); } } if (!$start && !$end) { $params[AdReportRunFields::DATE_PRESET] = InsightsPresets::LIFETIME; } $insights = $level->getInsights($fields, $params); return $insights->getResponse()->getBody(); } 中的"facebook/php-ads-sdk": "2.8.*")。 documentation is here

3 个答案:

答案 0 :(得分:1)

您应该使用delivery_info上的过滤来激活。它看起来像你的http请求中的以下内容

filtering:[{"field":"campaign.delivery_info","operator":"IN","value":["active"]}]

请参阅文档中的过滤部分。

https://developers.facebook.com/docs/marketing-api/insights/parameters/v2.8

filtering
list<Filter Object>
Default value: Array
Filters on the report data. This parameter is an array of filter objects.
field
string
Required
operator
enum {EQUAL, NOT_EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL, LESS_THAN, LESS_THAN_OR_EQUAL, IN_RANGE, NOT_IN_RANGE, CONTAIN, NOT_CONTAIN, IN, NOT_IN, STARTS_WITH, ANY, ALL, AFTER, BEFORE, NONE}
Required
value
string

如果您想查看代码,请在PHP SDK中查看此过滤字段。它只是镜像实际的API字段

https://github.com/facebook/facebook-php-ads-sdk/blob/53bee21251a9d898591708b96d1afa9e13516e3d/src/FacebookAds/Object/Campaign.php#L220

此外,我们最近推出了基于场景的codegen向导工具。您可以浏览&#34;创建广告报告&#34;向导,它将为您生成代码。 (虽然它现在只支持Java)

该工具的网址是:(替换您自己的应用ID) https://developers.facebook.com/apps/your_app_id/marketing-api/quickstart/

答案 1 :(得分:0)

这很有效,您实际上是在请求所有处于ACTIVE状态的广告,然后将insights作为“嵌套”字段列表提供。

将123456替换为您的广告帐户(但请保留所需的'act_'

  

act_123456 /广告栏= effective_status,姓名,见解{total_action_value,total_actions,操作},adset_id,CAMPAIGN_ID&安培;过滤= [{ '场'? 'effective_status', '操作符': 'IN', '值': [ 'ACTIVE']}]

这也适用于/campaigns/adsets

这是我弄清楚如何做的唯一方法。我无法在根级别对/act_123456/insights进行任何过滤。

答案 2 :(得分:0)

对于无法为 /act_123456/insights 进行过滤的任何人,当您使用 'level' = 'ad' 时,您需要进行以下过滤:

{'field':'ad.effective_status','operator':'IN','value':['ACTIVE', 'PAUSED', 'DELETED', 'ARCHIVED']}

现在它确实为我应用了过滤!