获取Google AdWords API所有广告系列统计信息,了解总展示次数和点击次数

时间:2014-03-13 04:45:54

标签: php google-adwords

如何获得Google AdWords API上所有广告系列的总展示次数和点击次数? 现在我正在这样做

  // Get the service, which loads the required classes.
  $campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);

  // Create selector.
  $selector = new Selector();
  $selector->fields =
      array('Id', 'Name', 'Impressions', 'Clicks', 'Cost', 'Ctr');
  $selector->predicates[] =
      new Predicate('Impressions', 'GREATER_THAN', array(0));

  // Set date range to request stats for.
  $dateRange = new DateRange();
  $dateRange->min = date('Ym01', time());
  $dateRange->max = date('Ymd', time());
  $selector->dateRange = $dateRange;

  // Make the get request.
  $page = $campaignService->get($selector);

  // get results.
  $impressions = 0;
  $clicks = 0;
  if (isset($page->entries)) {
    foreach ($page->entries as $campaign) {
        $impressions += $campaign->campaignStats->impressions;
        $clicks += $campaign->campaignStats->clicks;
    }
  } else {
    //print "No matching campaigns were found.\n";
  }

  return array('impressions'=>$impressions, 'clicks'=>$clicks);

我想知道我是否可以在不使用foreach的情况下获得总数并循环播放广告系列。

2 个答案:

答案 0 :(得分:1)

要获取帐户级统计信息,您可以使用AdWords API ACCOUNT_PERFORMANCE_REPORT。您可以CSV格式下载此报告。

我是Rubyist,但我相信这应该适用于PHP client library

    // AdWordsUser credentials come from "../auth.ini"
    $user = new AdWordsUser();
    $filePath = YOUR_FILE_PATH;
    $user->LoadService('ReportDefinitionService', ADWORDS_VERSION);

    $selector = new Selector();
    $selector->fields = array('AccountId', 'AccountDescriptiveName', 'Impressions', 'Clicks', 'Cost', 'Ctr');

    // no predicate necessary - this report already excludes zero-impression lines; 
    // plus, there is only one line, because it's the whole account

    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = WHATEVER_YOU_WANT_IT_TO_BE_NAMED;
    $reportDefinition->dateRangeType = 'LAST_7_DAYS';
    $reportDefinition->reportType = 'ACCOUNT_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'CSV';

    $options = array('returnMoneyInMicros' => TRUE);

    ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);

答案 1 :(得分:0)

$startDate = date('Ymd', strtotime('2018-10-12'));
$endDate = date('Ymd', strtotime('2018-11-13'));
$query = (new ReportQueryBuilder())
    ->select([
        'CampaignId',
        'AdGroupId',        
        'Impressions',
        'Clicks',
        'Cost'
    ])
    ->from(ReportDefinitionReportType::CRITERIA_PERFORMANCE_REPORT)
    ->where('Status')->in(['ENABLED', 'PAUSED'])
    ->during($startDate, $endDate)
    ->build();