PHP AdWords API v201809 - 如何使用 LocationCriterionService 从 GEO_PERFORMANCE_REPORT 获取城市名称?

PHP AdWords API v201809 - How to get city name using LocationCriterionService from GEO_PERFORMANCE_REPORT?

我没有使用 CRITERIA_PERFORMANCE_REPORT,而是使用以下示例来使用 GEO_PERFORMANCE_REPORThttps://developers.google.com/adwords/api/docs/samples/php/reporting#download-a-criteria-performance-report-with-awql

我正在从这个 API 报告中提取每个结果的 CityCriteriaId 属性。

我希望使用 LocationCriterionService 根据返回的 ID 找出城市的名称,但坚持使用最有效的方式来制作此查询。这是我目前所拥有的:

依赖关系:

namespace Google\AdsApi\Examples\AdWords\v201809\Reporting;
namespace Google\AdsApi\Examples\AdWords\v201809\Targeting;

require __DIR__ . '/vendor/autoload.php';

// For all the services/methods necessary from the Reporting namespace
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Query\v201809\ReportQueryBuilder;
use Google\AdsApi\AdWords\Reporting\v201809\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDownloader;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201809\cm\DateRange;
use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionReportType;
use Google\AdsApi\Common\OAuth2TokenBuilder;

// For all the services/methods necessary for the LocationCriterionService
// and Targeting namespace
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\v201809\cm\LocationCriterionService;
use Google\AdsApi\AdWords\v201809\cm\Predicate;
use Google\AdsApi\AdWords\v201809\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201809\cm\Selector;

runExample: 这是我的报告代码(这都是它自己的 class):

public static function runExample(AdWordsServices $adS, AdWordsSession $session, $reportF)
{
    $query = (new ReportQueryBuilder())
        ->select([
            'CityCriteriaId',
            'Clicks'
        ])
        ->from(ReportDefinitionReportType::GEO_PERFORMANCE_REPORT)
        ->duringDateRange(ReportDefinitionDateRangeType::LAST_7_DAYS)
        ->build();

    $reportDownloader = new ReportDownloader($session);
    $reportSettingsOverride = (new ReportSettingsBuilder())
        ->includeZeroImpressions(false)
        ->build();
    $reportDownloadResult = $reportDownloader->downloadReportWithAwql(
        sprintf('%s', $query),
        $reportF,
        $reportSettingsOverride
    );

    $reportResult = $reportDownloadResult->getAsString();
    $xml = simplexml_load_string($reportResult);
    $data = json_decode(json_encode((array)$xml), TRUE);

    $locationIDs = [];
    $results = $data['table']['row']['@attributes']['city'];
    foreach ($results as $cityID) {
        array_push($locationIDs, $cityID);
    }

    // Here's where I don't know what to do...
    // But any and all code for the LocationCriterionService would be done here

}

主要功能:(取自上面URL中的例子)

public static function main()
{
    $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

    $session = (new AdWordsSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();

    self::runExample(new AdWordsServices(), $session, DownloadFormat::XML);
}

对于我应该在报告结果中循环遍历城市 ID 的 foreach 循环中放置什么,我们将不胜感激。

请注意:我意识到另一种方法是存储 100,000 条记录(.csv Google 提供交叉引用城市及其分配的 ID ) 在本地数据库中并以这种方式交叉引用,但首选这种方式。

编辑:

我开始在 LocationCriterionService 的一些额外代码上取得一些进展,但它会产生错误,因为我的 Predicate 变量参数错误(soap dll 在你的 php_info):

$locationCriterionService = $adS->get($session, LocationCriterionService::class);

$selector = new Selector();
$selector->setFields(
    [
        'Id',
        'LocationName',
        'CanonicalName'
    ]
);

$selector->setPredicates(
    [
        new Predicate('locationid', PredicateOperator::EQUALS, $locationIDs)
    ]
);

$locationCriteria = $locationCriterionService->get($selector);

if ($locationCriteria !== null) {
    foreach ($locationCriteria as $locationCriterion) {
        printf(
            "The search term '%s' returned the location '%s' of type '%s' "
            . "with ID %d, and reach %d (%s).\n",
            $locationCriterion->getSearchTerm(),
            $locationCriterion->getLocation()->getLocationName(),
            $locationCriterion->getLocation()->getDisplayType(),
            $locationCriterion->getLocation()->getId()
        );
    }
} else {
    print "No location criteria were found.\n";
}

错误是:

Uncaught Google\AdsApi\AdWords\v201809\cm\ApiException: [SelectorError.INVALID_PREDICATE_FIELD_NAME @ selector; trigger:'locationid']

我使用 Node.js 编写我的 google adwords 应用程序。

Google 广告词 API: v201809.

这是我的案例:

我得到 xmlGEO_PERFORMANCE_REPORT 数据是这样的:

<row campaignID='1733539359' campaign='Campaign Name3.0-119-1552463540147' impressions='1' countryTerritory='2818' city='1005392'/>
<row campaignID='1733539359' campaign='Campaign Name3.0-119-1552463540147' impressions='4' countryTerritory='2716' city='1028774'/>

如您所见,有两个字段名为:countryTerritorycity

他们是countrycriteriaid and citycriteriaid

现在,我想获取国家名称和城市名称。可以使用 LocationCriterionService

来完成

这是我 selector 的代码片段:

 const criterionIds = [2818, 2716, 1005392, 1028774];

 const selector = new AdWords.Selector.model({
      fields: ['CanonicalName', 'Reach'],
      predicates: [ {
        field: 'Id',
        operator: 'IN',
        values: criterionIds
      }],
      ordering: []
    })

我得到回复:

{
    "entries": [
        {
            "location": {
                "id": "2716",
                "Criterion.Type": "Location",
                "locationName": "Zimbabwe",
                "displayType": "Country",
                "targetingStatus": "ACTIVE"
            },
            "canonicalName": "Zimbabwe",
            "reach": "736000"
        },
        {
            "location": {
                "id": "2818",
                "Criterion.Type": "Location",
                "locationName": "Egypt",
                "displayType": "Country",
                "targetingStatus": "ACTIVE"
            },
            "canonicalName": "Egypt",
            "reach": "20300000"
        },
        {
            "location": {
                "id": "1005392",
                "Criterion.Type": "Location",
                "locationName": "Damietta",
                "displayType": "City",
                "targetingStatus": "ACTIVE",
                "parentLocations": [
                    {
                        "id": "21479",
                        "Criterion.Type": "Location",
                        "locationName": "Damietta Governorate",
                        "displayType": "Governorate",
                        "targetingStatus": "ACTIVE"
                    },
                    {
                        "id": "2818",
                        "Criterion.Type": "Location",
                        "locationName": "Egypt",
                        "displayType": "Country",
                        "targetingStatus": "ACTIVE"
                    }
                ]
            },
            "canonicalName": "Damietta,Damietta Governorate,Egypt",
            "reach": "593000"
        },
        {
            "location": {
                "id": "1028774",
                "Criterion.Type": "Location",
                "locationName": "Harare",
                "displayType": "City",
                "targetingStatus": "ACTIVE",
                "parentLocations": [
                    {
                        "id": "9070024",
                        "Criterion.Type": "Location",
                        "locationName": "Harare Province",
                        "displayType": "Province",
                        "targetingStatus": "ACTIVE"
                    },
                    {
                        "id": "2716",
                        "Criterion.Type": "Location",
                        "locationName": "Zimbabwe",
                        "displayType": "Country",
                        "targetingStatus": "ACTIVE"
                    }
                ]
            },
            "canonicalName": "Harare,Harare Province,Zimbabwe",
            "reach": "593000"
        }
    ]
}

希望对您有所帮助。

我遇到了同样的困境。 @slideshowp2 的建议以及您编辑的答案是解决此问题的正确方法。请参阅来自 Google 论坛的原始帖子:https://groups.google.com/forum/#!topic/adwords-api/gfbMiTrxcnU.

如其中所述,您应该从 LocationCriterionService. You can find an example of how to use this service at https://developers.google.com/adwords/api/docs/samples/php/targeting#get-location-criteria-by-name 中获取位置的真实名称。这是我为了获取我需要的数据而实际基于的,但我没有使用位置名称,而是使用位置 ID 作为搜索条件。

在这里您可以看到一个位置对象的示例:

0 => Google\AdsApi\AdWords\v201809\cm\LocationCriterion {#7340
  #location: Google\AdsApi\AdWords\v201809\cm\Location {#7343
    #locationName: "Fort Lauderdale"
    #displayType: "City"
    #targetingStatus: "ACTIVE"
    #parentLocations: array:4 [
      0 => Google\AdsApi\AdWords\v201809\cm\Location {#7338
        #locationName: "Broward County"
        #displayType: "County"
        #targetingStatus: "ACTIVE"
        #parentLocations: null
        #id: 1014969
        #type: null
        #CriterionType: "Location"
        -parameterMap: array:1 [
          "Criterion.Type" => "CriterionType"
        ]
      }
      1 => Google\AdsApi\AdWords\v201809\cm\Location {#7339
        #locationName: "Miami-Ft. Lauderdale FL"
        #displayType: "DMA Region"
        #targetingStatus: "ACTIVE"
        #parentLocations: null
        #id: 200528
        #type: null
        #CriterionType: "Location"
        -parameterMap: array:1 [
          "Criterion.Type" => "CriterionType"
        ]
      }
      2 => Google\AdsApi\AdWords\v201809\cm\Location {#7337
        #locationName: "Florida"
        #displayType: "State"
        #targetingStatus: "ACTIVE"
        #parentLocations: null
        #id: 21142
        #type: null
        #CriterionType: "Location"
        -parameterMap: array:1 [
          "Criterion.Type" => "CriterionType"
        ]
      }
      3 => Google\AdsApi\AdWords\v201809\cm\Location {#7336
        #locationName: "United States"
        #displayType: "Country"
        #targetingStatus: "ACTIVE"
        #parentLocations: null
        #id: 2840
        #type: null
        #CriterionType: "Location"
        -parameterMap: array:1 [
          "Criterion.Type" => "CriterionType"
        ]
      }
    ]
    #id: 1015027
    #type: null
    #CriterionType: "Location"
    -parameterMap: array:1 [
      "Criterion.Type" => "CriterionType"
    ]
  }
  #canonicalName: "Fort Lauderdale,Florida,United States"
  #reach: 1450000
  #locale: null
  #searchTerm: null
  #countryCode: null
}

最后,您得到的错误是因为 Location 对象中没有 LocationIdlocationid(无论您使用它的方式如何)。您应该改用 Id。请参阅位于 https://developers.google.com/adwords/api/docs/reference/v201809/LocationCriterionService.Location#idLocation 对象的文档。

顺便说一句,我使用的 API 版本与您的相同:v201809.