通过AdWords API获取活动统计数据(如点击、点击率、CPC等)


Getting campaign stats (e.g. Clicks, CTR, CPC, etc.) via AdWords API

我目前想做的是获取活动统计数据,如点击,印象,点击率,平均CPC等特定活动。不幸的是,我找不到如何通过AdWords API做到这一点。

到目前为止我发现的是,

  1. 也许,在CampaignService的早期版本中,我们能够通过做$campaign->campaignStats之类的事情来获得统计数据。不幸的是,我使用的是V201506,其中没有campaignStats对象/变量。
  2. 我可能可以使用'CAMPAIGN_PERFORMANCE_REPORT'获得这些统计数据,但它需要下载,我不想下载报告。我只是想要一个数组或类似的东西返回,以便我可以处理它。此外,我不想给出任何时间框架,我只是希望所有的时间统计返回该活动。这可能吗?
如果有人能帮我,我将非常感激。有点困在这里几个小时,浏览了整个AdWords API文档,但无法理解什么是最好的和简单的方法。

现在,Adwords API只允许统计数据通过报告服务。可以使用两种方法获取统计信息。

1)按照描述使用报告服务

2)您可以使用Adwords查询语言。看到这个

我不知道你是否还需要这个,但API V201806我找到了一个解决方案。在这个版本的API中存在getAsString()函数,它以字符串形式返回数据,而不是下载文件,我请求XML格式的数据,然后在PHP中将响应转换为XML对象。

这是我使用的代码:

class DownloadCriteriaReportWithAwql {
public static function runExample(AdWordsSession $session, $reportFormat){
    // Create report query to get the data for last 7 days.
    $query = (new ReportQueryBuilder())
        ->select([
            'CampaignId',
            'AdGroupId',
            'Id',
            'Criteria',
            'CriteriaType',
            'Impressions',
            'Clicks',
            'Cost',
            'Conversions'
        ])
        ->from(ReportDefinitionReportType::CRITERIA_PERFORMANCE_REPORT)
        ->where('Status')->in(['ENABLED'])
        ->duringDateRange(ReportDefinitionDateRangeType::LAST_7_DAYS)
        ->build();
    // Download report as a string.
    $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->downloadReportWithAwql(
        sprintf('%s', $query),
        $reportFormat,
        $reportSettingsOverride
    );
    //print "Report was downloaded and printed below:'n";
    $datos = $reportDownloadResult->getAsString();
    return ($datos);
}
public static function main(){
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->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()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();
    $string = self::runExample($session, DownloadFormat::XML);
    $xml = new 'SimpleXMLElement($string);
    return $xml;}}

这个问题是在2015年提出的,从那时起他们将API重命名为Google Ads API。当前版本是V6,其中获得点击,点击率,CPC和其他指标相对简单。

这里的文档说明:

这个页面显示了所有的指标和细分,可以放在同一个SELECT子句作为活动的字段

在此基础上,获得广告活动和点击的AWQL将看起来像这样(测试):

$query = "SELECT campaign.id, campaign.name, campaign.status, metrics.clicks FROM campaign ORDER BY campaign.name"
PHP中如何遍历结果的例子:
$stream = $googleAdsServiceClient->searchStream($customerId, $query);
    foreach ($stream->iterateAllElements() as $googleAdsRow) {
        /** @var GoogleAdsRow $googleAdsRow */
        $data['campaigns'][] = [
            'id'     => $googleAdsRow->getCampaign()->getId(),
            'clicks' => $googleAdsRow->getMetrics()->getClicks(),
        ];
    }