获得Google AdWords API的所有活动统计的总印象和点击


get Google AdWords API all campaign stats for total impressions and clicks

如何在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和循环的情况下获得总数。

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

我是一个ruby主义者,但我相信这应该适用于PHP客户端库:

    // 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);
$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();