<?php/** * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
namespace Google\AdsApi\Examples\AdWords\v201809\Reporting;
// require __DIR__ . '/../../../../vendor/autoload.php';require __DIR__ . '/../../../../../../autoload.php';
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201809\DownloadFormat;use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinition;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\Predicate;use Google\AdsApi\AdWords\v201809\cm\PredicateOperator;use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionReportType;use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\Common\OAuth2TokenBuilder;
/**
* Downloads CRITERIA_PERFORMANCE_REPORT for the specified client customer ID. */class DownloadGeoPerformanceReportWithSelector{
public static function runExample(AdWordsSession $session, $filePath) {
// Create selector. $selector = new Selector();
$selector->setFields(
['Impressions', 'Clicks', 'Conversions', 'Cost', 'Date', 'ExternalCustomerId', 'CountryCriteriaId','CampaignId'] );
/* $selector->setFields( [ 'CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Cost' ] );*/ // Use a predicate to filter out paused criteria (this is optional)./* $selector->setPredicates( [ new Predicate('Status', PredicateOperator::NOT_IN, ['PAUSED']) ] );*/
// Create report definition. $reportDefinition = new ReportDefinition(); $reportDefinition->setSelector($selector); $reportDefinition->setReportName(
'Geo Performance Report #' . uniqid() ); $reportDefinition->setDateRangeType( ReportDefinitionDateRangeType::LAST_7_DAYS ); $reportDefinition->setReportType( ReportDefinitionReportType::GEO_PERFORMANCE_REPORT ); $reportDefinition->setDownloadFormat(DownloadFormat::CSV);
// Download report. $reportDownloader = new ReportDownloader($session);
// Optional: If you need to adjust report settings just for this one
// request, you can create and supply the settings override here. Otherwise, // default values from the configuration file (adsapi_php.ini) are used. $reportSettingsOverride = (new ReportSettingsBuilder())->includeZeroImpressions(false)->build(); $reportDownloadResult = $reportDownloader->downloadReport( $reportDefinition, $reportSettingsOverride ); $reportDownloadResult->saveToFile($filePath); printf( "Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->getReportName(), $filePath ); }
public static function main() {
// $mcc = '237-147-9138';// $account_id = '9901274003';
// 测试 geo 类别取不到数据问题 // 该账号在 adwords_tracking_country 表里找不到成本数据$mcc = '945-959-2387';$account_id = '5911335188'; // geo 没数据 ,location 有数据
$mccConfigPath = dirname(__FILE__) . '/adwauth.' . $mcc . '.ini';
// Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile($mccConfigPath)->build();
// See: AdWordsSessionBuilder for setting a client customer ID that is // different from that specified in your adsapi_php.ini file.
// Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile($mccConfigPath)->withOAuth2Credential($oAuth2Credential)->withClientCustomerId($account_id)->build();
$filePath = sprintf( '%s.csv', tempnam(sys_get_temp_dir(), 'geo-performance-report-') ); self::runExample($session, $filePath); }}
DownloadGeoPerformanceReportWithSelector::main();
<Screenshot removed>
class DownloadCampaignLocationTargetReportWithSelector
{
public static function runExample(AdWordsSession $session, $filePath) { // Create selector. $selector = new Selector();
$selector->setFields(
['Impressions', 'Clicks', 'Conversions', 'Cost', 'Date', 'ExternalCustomerId', 'CampaignId', 'Id']
);
/* $selector->setFields( [ 'CampaignId', 'AdGroupId', 'Id', 'Criteria', 'CriteriaType', 'Impressions', 'Clicks', 'Cost' ] );*/ // Use a predicate to filter out paused criteria (this is optional)./* $selector->setPredicates( [ new Predicate('Status', PredicateOperator::NOT_IN, ['PAUSED']) ] );*/ // Create report definition. $reportDefinition = new ReportDefinition(); $reportDefinition->setSelector($selector); $reportDefinition->setReportName(
'Campaign Location Target Report #' . uniqid()
); $reportDefinition->setDateRangeType( ReportDefinitionDateRangeType::LAST_7_DAYS ); $reportDefinition->setReportType(
ReportDefinitionReportType::CAMPAIGN_LOCATION_TARGET_REPORT
); $reportDefinition->setDownloadFormat(DownloadFormat::CSV);
// Download report. $reportDownloader = new ReportDownloader($session); // Optional: If you need to adjust report settings just for this one // request, you can create and supply the settings override here. Otherwise, // default values from the configuration file (adsapi_php.ini) are used. $reportSettingsOverride = (new ReportSettingsBuilder())->includeZeroImpressions(false)->build(); $reportDownloadResult = $reportDownloader->downloadReport( $reportDefinition, $reportSettingsOverride ); $reportDownloadResult->saveToFile($filePath); printf( "Report with name '%s' was downloaded to '%s'.\n", $reportDefinition->getReportName(), $filePath ); }
public static function main() {
// cd lib/vendor/googleads/googleads-php-lib/examples/AdWords/v201809/Reporting/// php DownloadCampaignLocationTargetReportWithSelector.php// 到不到 location 信息账号$mcc = '945-959-2387';$account_id = '9929989200';
// 该账号在 adwords_tracking_country 表里找不到成本数据
// $mcc = '945-959-2387';// // $account_id = '5385670407';// $account_id = '5911335188'; // geo 没数据 ,location 有数据
$mccConfigPath = dirname(__FILE__) . '/../../../../../../../../scripts/adwords/conf/adwauth.' . $mcc . '.ini';
// Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile($mccConfigPath)->build();
// See: AdWordsSessionBuilder for setting a client customer ID that is // different from that specified in your adsapi_php.ini file. // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile($mccConfigPath)->withOAuth2Credential($oAuth2Credential)->withClientCustomerId($account_id)->build();
$filePath = sprintf( '%s.csv',
tempnam(sys_get_temp_dir(), 'campaign-location-target-report-') ); self::runExample($session, $filePath); }}
DownloadCampaignLocationTargetReportWithSelector::main();