如何通过 Google AdWords API v201309 检索每日费用?

问题描述 投票:0回答:3

成功使用这个SO解决方案大约一年了,我获取Google AdWords广告系列每日费用的代码如下:

sum += campaign.campaignStats.cost.microAmount / 1000000m;

即我正在使用

campaignStats
属性

不幸的是,在 API 的最新版本 v201309 中,此属性不再存在。

我搜索了官方 .NET 包装器的所有示例,还浏览了 API 文档,只是没有找到任何关于如何管理它的线索。

因此我的问题是:

如何通过最新的 Google AdWords API 检索 AdWord 广告系列的每日费用?

更新1:

我在 AdWords API 论坛中找到了 此讨论。他们建议生成一份报告,获取并解析该报告。还有关于它的 AdWords 博客文章

.net google-api google-ads-api
3个回答
5
投票

这是 PHP 版本:

$filePath = tempnam('/tmp','adwordsreport');

    $user = new AdWordsUser(NULL, NULL,   NULL,      $developerToken,  $applicationToken, $userAgent, $ClientID, NULL, NULL,       $oauth2Info);

    $user->SetClientId($ClientID);

    // Log SOAP XML request and response.
    $user->LogDefaults();

    $user->LoadService('ReportDefinitionService', 'v201402');

    // Create selector.
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria',
        'CriteriaType', 'Impressions', 'Clicks', 'Cost');

    // Filter out deleted criteria.
    $selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED'));

    // Create report definition.
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Criteria performance report #' . uniqid();
    $reportDefinition->dateRangeType = 'ALL_TIME';
    $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'XML';

    // Exclude criteria that haven't recieved any impressions over the date range.
    $reportDefinition->includeZeroImpressions = FALSE;

    // Set additional options.
    $options = array('version' => 'v201402', 'returnMoneyInMicros' => TRUE);

    // Download report.
    ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);


    printf("Report with name '%s' was downloaded to '%s'.\n",
        $reportDefinition->reportName, $filePath);


    $doc = new DOMDocument();
    $doc->loadXML(file_get_contents($filePath));
    $xp = new DOMXPath($doc);
    $q = $xp->query("/report/table/row/@cost");
    $cost = 0;
    foreach($q as $el) {
      $v = $el->textContent;
      $cost += $v / 1000000;
    }

    echo $cost;

3
投票

这是我想出的可行解决方案:

private static decimal coreGetAdwordsSumInRange(DateTime start, DateTime end)
{
    var reportDefinition =
        new ReportDefinition
        {
            reportName = string.Format(@"Campaign performance report #{0}", DateTime.Now.Ticks),
            dateRangeType = ReportDefinitionDateRangeType.CUSTOM_DATE,
            reportType = ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT,
            downloadFormat = DownloadFormat.XML,
            includeZeroImpressions = false,
            selector = new Selector
            {
                fields = new[] { @"Cost" },
                dateRange = new DateRange {min = start.ToString(@"yyyyMMdd"), max = end.ToString(@"yyyyMMdd")}
            }
        };

    // --

    var sum = decimal.Zero;

    var tempFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + @".xml");
    try
    {
        var utils = new ReportUtilities(new AdWordsUser()) { ReportVersion = @"v201309" };
        utils.DownloadClientReport(reportDefinition, true, tempFilePath);

        var doc = new XmlDocument();
        doc.Load(tempFilePath);

        var costNodes = doc.SelectNodes(@"/report/table/row/@cost");
        if (costNodes != null)
        {
            foreach (XmlNode costNode in costNodes)
            {
                var cost = Convert.ToDecimal(costNode.InnerText);
                sum += cost/1000000m;
            }
        }
    }
    finally
    {
        File.Delete(tempFilePath);
    }

    return sum;
}

也可通过 Pastebin.com获取。


1
投票

使用

PHP
获取最新 API
v201506
v201509
进行编码以获取日期范围内的成本总和。

function getAdwordSumOfCost(AdWordsUser $user, $filePath, $startDate=null, $endDate=null, $campaignId = null) {
    $basePath = __DIR__."/../../";//path to google ads api
    // Load the service, so that the required classes are available.
    $user->LoadService('ReportDefinitionService', 'v201509');
    include_once($basePath.'vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/v201509/CampaignService.php');
    $selector = new Selector();
    $selector->fields = array('CampaignId', 'AdGroupId', 'Id', 'Criteria',
        'CriteriaType', 'Impressions', 'Clicks', 'Cost');

    if($campaignId)
        $selector->predicates[] = new Predicate('CampaignId', 'IN', array($campaignId));

    if($startDate && $endDate) {
        $selector->dateRange = new DateRange($startDate, $endDate);
    }

    // Create report definition.
    include_once($basePath.'vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/Util/v201509/ReportClasses.php');
    $reportDefinition = new ReportDefinition();
    $reportDefinition->selector = $selector;
    $reportDefinition->reportName = 'Criteria performance report #' . uniqid();
    $reportDefinition->dateRangeType = 'CUSTOM_DATE';
    $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
    $reportDefinition->downloadFormat = 'XML';

    // Exclude criteria that haven't recieved any impressions over the date range.
    $reportDefinition->includeZeroImpressions = false;

    // Set additional options.
    $options = array('version' => 'v201509');

    // Download report.
    include_once($basePath.'vendor/googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/Util/v201509/ReportUtils.php');
    ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);

    $doc = new DOMDocument();
    $doc->loadXML(file_get_contents($filePath));
    $xp = new DOMXPath($doc);
    $q = $xp->query("/report/table/row/@cost");
    $cost = 0.00;
    foreach($q as $el) {
        $v = $el->textContent;
        $cost += $v / 1000000;
    }

    return $cost;
}

用途:

$adwordsUser = new AdWordsUser();

//pass Adwords clientID, you can also pas campaign id as well if you want to get calculation only for a single campaign
$adwords = new Adwords('111-111-111');

//Will give you cost for yesterday
$cost = $adwords->getAdwordSumOfCost($adwordsUser, '/path/to/storage/result.xml', date('Ymd', strtotime('-1 day')), date('Ymd', strtotime('-1 day')));
© www.soinside.com 2019 - 2024. All rights reserved.