代码之家  ›  专栏  ›  技术社区  ›  LatentDenis

PHP AdWords API v201809-如何使用地理性能报告中的位置标准服务获取城市名称?

  •  0
  • LatentDenis  · 技术社区  · 7 年前

    CRITERIA_PERFORMANCE_REPORT ,我在拉 GEO_PERFORMANCE_REPORT 使用此示例: https://developers.google.com/adwords/api/docs/samples/php/reporting#download-a-criteria-performance-report-with-awql

    CityCriteriaId 此API报表的每个结果的属性。

    LocationCriterionService 根据返回的id来确定城市的名称,这些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;
    

    运行示例: 这是我的报告代码(这都在它自己的类中):

    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

    请注意: 我意识到另一种方法是将10万条某些记录(Google提供给具有分配ID的交叉引用城市的.csv记录)存储在本地数据库中,并用这种方法进行交叉引用,但这种方法是首选的。

    我开始用一些特殊的 位置标准服务 但是它会产生一个错误,因为谓词变量的参数是错误的( soap 您的 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";
    }
    

    错误是:

    [SelectorError.INVALID_PREDICATE_FIELD_NAME@selector;

    0 回复  |  直到 7 年前
        1
  •  0
  •   Lin Du Correcter    7 年前

    我用 Node.js

    谷歌广告词API: v201809 .

    这是我的案子:

    我明白了 xml 数据 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 和 city .

    他们是 countrycriteriaid 和 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"
            }
        ]
    }
    

    推荐文章