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

问题描述 投票:0回答:1

而不是拉一个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:这是我的报告代码(这是它自己的类):

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);
}

关于我应该放在foreach循环内部的任何帮助,将从报告结果中循环遍历城市ID。

请注意:我意识到另一种方法是在本地数据库中存储100,000条记录(Google提供的.csv以及用其分配的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";
}

错误是:

未使用的Google \ AdsApi \ AdWords \ v201809 \ cm \ ApiException:[SelectorError.INVALID_PREDICATE_FIELD_NAME @ selector;触发: 'locationid']

php google-adwords adwords-api-v201802
1个回答
0
投票

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

Google adwords 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

他们是countrycriteriaidcitycriteriaid

现在,我想获得国家名称和城市名称。它可以使用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"
        }
    ]
}

希望它可以提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.