PHP AdWords API v201809 - 如何使用 LocationCriterionService 从 GEO_PERFORMANCE_REPORT 获取城市名称?
Posted
技术标签:
【中文标题】PHP AdWords API v201809 - 如何使用 LocationCriterionService 从 GEO_PERFORMANCE_REPORT 获取城市名称?【英文标题】:PHP AdWords API v201809 - How to get city name using LocationCriterionService from GEO_PERFORMANCE_REPORT? 【发布时间】:2019-01-24 22:01:37 【问题描述】:我没有拉CRITERIA_PERFORMANCE_REPORT
,而是使用这个例子拉GEO_PERFORMANCE_REPORT
:https://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:这是我的报告代码(这都在它自己的类中):
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 的 foreach
循环中放置什么内容,我们将不胜感激。
请注意:我意识到另一种方法是将 100,000 条记录(Google 提供的用于交叉引用城市及其分配的 ID 的 .csv 文件)存储在本地数据库中并交叉引用方式,但首选这种方式。
编辑:
我开始使用一些特定于LocationCriterionService
的附加代码取得一些进展,但它会产生错误,因为我的谓词变量参数错误(php_info
中需要soap
dll):
$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";
错误是:
未捕获的 Google\AdsApi\AdWords\v201809\cm\ApiException: [SelectorError.INVALID_PREDICATE_FIELD_NAME @ 选择器; 触发器:'locationid']
【问题讨论】:
【参考方案1】:我使用Node.js
编写我的google adwords 应用程序。
Google 广告词 API:v201809
。
这是我的情况:
我得到GEO_PERFORMANCE_REPORT
的xml
数据如下:
<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
和 city
。
他们是countrycriteriaid 和citycriteriaid
现在,我想获取国家名称和城市名称。可以使用LocationCriterionService
这是selector
的代码 sn-p:
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"
]
希望对你有帮助。
【讨论】:
【参考方案2】:我也面临同样的困境。 @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
最后,您得到的错误是因为Location
对象中没有LocationId
或locationid
(无论您使用它的方式)。您应该改用Id
。请参阅https://developers.google.com/adwords/api/docs/reference/v201809/LocationCriterionService.Location#id 上的Location
对象的文档。
顺便说一句,我使用的 API 版本和你的一样:v201809
。
【讨论】:
以上是关于PHP AdWords API v201809 - 如何使用 LocationCriterionService 从 GEO_PERFORMANCE_REPORT 获取城市名称?的主要内容,如果未能解决你的问题,请参考以下文章
AdWords API 的 AddCampaigns.php 示例返回 internal_failure 错误
如何在 php 中的 Google Adwords API 中设置多个 ClientCustomerId?