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

时间:2019-01-24 22:01:37

标签: php google-adwords adwords-api-v201802

我使用以下示例代替了CRITERIA_PERFORMANCE_REPORThttps://developers.google.com/adwords/api/docs/samples/php/reporting#download-a-criteria-performance-report-with-awql

我正在为此API报告中的每个结果提取GEO_PERFORMANCE_REPORT属性。

我正在寻找使用CityCriteriaId来根据返回的ID来确定城市的名称,但这些ID仍停留在最有效的查询方式上。这是我到目前为止的内容:

依赖项:

LocationCriterionService

runExample::这是我拥有的报告代码(全部在其自己的类中):

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;

主要功能(摘自上面URL中的示例)

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

}

对于我应该放在该public static function main() { $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); $session = (new AdWordsSessionBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); self::runExample(new AdWordsServices(), $session, DownloadFormat::XML); } 循环(该循环从报告结果中遍历城市ID)内部的任何帮助,我们将不胜感激。

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

编辑:

我开始使用一些特定于foreach的附加代码取得了一些进展,但是由于我的Predicate变量参数错误({{1}中的LocationCriterionService dll是必需的),它产生了一个错误。 }):

soap

错误是:

  

未捕获的Google \ AdsApi \ AdWords \ v201809 \ cm \ ApiException:   [SelectorError.INVALID_PREDICATE_FIELD_NAME @选择器;   触发:“ locationid”]

2 个答案:

答案 0 :(得分:0)

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

Google adwords API:v201809

这是我的情况:

我这样获得xml的{​​{1}}数据:

GEO_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'/> countryTerritory的字段。

它们是countrycriteriaidcitycriteriaid

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

这是我的city的代码段:

selector

然后我得到答复:

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

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

希望它可以提供帮助。

答案 1 :(得分:0)

我面临着同样的困境。 @ slideshowp2所建议的内容以及您对答案所做的修改是解决此问题的正确方法。查看来自Google论坛的原始主题:https://groups.google.com/forum/#!topic/adwords-api/gfbMiTrxcnU

如此处所述,您应该从LocationCriterionService获取位置的真实名称。您可以在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
}

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

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