doulan4371 2014-03-13 04:45
浏览 470

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

How to get total impressions and clicks for all campaigns on Google AdWords API? Right now I am doing this way

  // 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.
";
  }

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

I am wondering if I can just get total without using foreach and loop through the campaigns.

  • 写回答

2条回答 默认 最新

  • dqy1265 2014-03-13 18:26
    关注

    To get account-level stats, you can use the AdWords API's ACCOUNT_PERFORMANCE_REPORT. You can download this report in CSV format.

    I am a Rubyist, but I believe that this should work for the 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);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题