检索帐户中所有活动广告的Facebook

检索帐户中所有活动广告的Facebook

本文介绍了检索帐户中所有活动广告的Facebook Insights?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code allows me to retrieve all ads that between certain dates, but this includes ads that are no longer active.

我只想显示关于ACTIVE广告的见解. 如何获取帐户中所有有效广告的Facebook见解?

I want to show insights only for ACTIVE ads. How can I retrieve Facebook Insights for all active ads within an account?

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(composer.json中的"facebook/php-ads-sdk": "2.8.*"). 文档在此处.

I'm using the Facebook PHP Ads SDK 2.8 ("facebook/php-ads-sdk": "2.8.*" in composer.json). The documentation is here.

推荐答案

您应该在delivery_info上使用过滤到活动"状态.看起来像您的http请求中的以下内容

You should use filtering on delivery_info to Active. It would be looks like the following in your http request

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

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

Please refer to the filtering section in the document.

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字段

If you want to see the code, please check this filtering field in the PHP SDK. It's just mirroring the actual API fields

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

此外,我们最近还启动了基于场景的代码生成向导工具.您可以逐步完成创建广告报告"向导,它将为您生成代码. (尽管目前它仅支持Java)

Also, we recently launched a scenario based codegen wizard tool. You can walk through the "Create Ad Report" wizard, and it will generate code for you. (Although it only support Java for now)

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

The URL of the tool is: (replace your own app ID)https://developers.facebook.com/apps/your_app_id/marketing-api/quickstart/

这篇关于检索帐户中所有活动广告的Facebook Insights?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:03