AdWords API 获取费用

发布于 2024-10-11 04:14:44 字数 255 浏览 3 评论 0原文

我正在寻找一种简单的解决方案,通过 API 从 Google AdWords 中提取每日费用。我查看了 Apility 和官方 AdWords API,但是第一个不再维护,而第二个则太过分了 - 我的意思是 76MB 未压缩代码只能获得日常成本?

有人知道从 Google AdWords 获取费用的简单解决方案吗?

I am looking for a simple solution to extract daily costs from Google AdWords via the API. I've looked at Apility and the official AdWords API, but the first is not maintained any more while the second is an overkill - I mean 76MB of uncompressed code to only get the daily costs?

Does anybody know of a simple solution to get the costs from Google AdWords?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南笙 2024-10-18 04:14:44

如果您指的是 API 费用,请参阅 AdWords API 中使用的 GetUnits 调用教程

如果您指的是运行广告系列费用,则可以使用 统计选择器。您将需要成本/微量。

If you're referring to API costs, there's the GetUnits call, used in the AdWords API Tutorial.

If you're referring to running campaign costs, you can use a StatsSelector. You will want the cost/microAmount.

雨巷深深 2024-10-18 04:14:44

使用

composer require googleads/googleads-php-lib

Laravel 5.8 Google Ads 脚本

v201809

<?php namespace App\Console\Commands\Google;

use App\Model\AdvExpense;
use App\Model\Campaign;
use App\Model\Source;
use App\Service\Xml;
use Carbon\Carbon;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201809\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDownloader;
use Google\AdsApi\AdWords\v201809\cm\CampaignService;
use Google\AdsApi\AdWords\v201809\cm\DateRange;
use Google\AdsApi\AdWords\v201809\cm\OrderBy;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\AdWords\v201809\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Illuminate\Console\Command;

class Expenses extends Command
{
    /**
     * The name and signature of the console command
     *
     * @var string
     */
    protected $signature = 'google:expenses';
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'Import Google Ads Expenses';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Import Google Ads Expenses";

    public function handle()
    {
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(storage_path('adsapi_php.ini'))->build();
        $session = (new AdWordsSessionBuilder())->fromFile(storage_path('adsapi_php.ini'))->withOAuth2Credential($oAuth2Credential)->build();

        $date = Carbon::yesterday();

        $this->saveExpenses($session, $date);
    }

    public function saveExpenses($session, $date)
    {
// Create selector.
        $selector = new Selector();
        $selector->setFields(['CampaignId', 'CampaignName', 'Clicks', 'Cost']);
        $selector->setDateRange(new DateRange(
            $date->format('Ymd'),
            $date->format('Ymd')
        ));

// Create report definition.
        $reportDefinition = new ReportDefinition();
        $reportDefinition->setSelector($selector);
        $reportDefinition->setReportName('Expenses report ' . uniqid());
        $reportDefinition->setDateRangeType(ReportDefinitionDateRangeType::CUSTOM_DATE);
        $reportDefinition->setReportType(ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT);
        $reportDefinition->setDownloadFormat(DownloadFormat::XML);

// Download report.
        $reportDownloader = new ReportDownloader($session);
        $reportDownloadResult = $reportDownloader->downloadReport($reportDefinition);
        $result = $reportDownloadResult->getAsString();

        $report = Xml::namespacedXMLToArray($result);
        foreach ($report['table']['row'] as $item) {
            $campaign_id = $item['@attributes']['campaignID'];
            $campaign_name = $item['@attributes']['campaign'];
            $cost = $item['@attributes']['cost'];

            if ($cost > 0) {
                $dbCampaign = Campaign::where('code', $campaign_id)->get()->first();

                if ($dbCampaign) {
                    $price = round($cost / 1000000);
                    if ($price) {
                        $dbCost = AdvExpense::updateOrCreate(
                            [
                                'campaign_id' => $dbCampaign->id,
                                'date' => $date,
                            ],
                            [
                                'price' => $price,
                            ]
                        );

                        if ($dbCost->exists) {
                            $this->info($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' OK');
                        } else {
                            $this->error($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' ERROR');
                        }
                    }
                } else {
                    $this->error('Campaign ' . $campaign_name . ' not found!');
                }
            }
        }
    }
}

Use

composer require googleads/googleads-php-lib

Script for Laravel 5.8

Google Ads v201809

<?php namespace App\Console\Commands\Google;

use App\Model\AdvExpense;
use App\Model\Campaign;
use App\Model\Source;
use App\Service\Xml;
use Carbon\Carbon;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\Reporting\v201809\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201809\ReportDownloader;
use Google\AdsApi\AdWords\v201809\cm\CampaignService;
use Google\AdsApi\AdWords\v201809\cm\DateRange;
use Google\AdsApi\AdWords\v201809\cm\OrderBy;
use Google\AdsApi\AdWords\v201809\cm\Paging;
use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201809\cm\Selector;
use Google\AdsApi\AdWords\v201809\cm\SortOrder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Illuminate\Console\Command;

class Expenses extends Command
{
    /**
     * The name and signature of the console command
     *
     * @var string
     */
    protected $signature = 'google:expenses';
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'Import Google Ads Expenses';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "Import Google Ads Expenses";

    public function handle()
    {
        $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(storage_path('adsapi_php.ini'))->build();
        $session = (new AdWordsSessionBuilder())->fromFile(storage_path('adsapi_php.ini'))->withOAuth2Credential($oAuth2Credential)->build();

        $date = Carbon::yesterday();

        $this->saveExpenses($session, $date);
    }

    public function saveExpenses($session, $date)
    {
// Create selector.
        $selector = new Selector();
        $selector->setFields(['CampaignId', 'CampaignName', 'Clicks', 'Cost']);
        $selector->setDateRange(new DateRange(
            $date->format('Ymd'),
            $date->format('Ymd')
        ));

// Create report definition.
        $reportDefinition = new ReportDefinition();
        $reportDefinition->setSelector($selector);
        $reportDefinition->setReportName('Expenses report ' . uniqid());
        $reportDefinition->setDateRangeType(ReportDefinitionDateRangeType::CUSTOM_DATE);
        $reportDefinition->setReportType(ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT);
        $reportDefinition->setDownloadFormat(DownloadFormat::XML);

// Download report.
        $reportDownloader = new ReportDownloader($session);
        $reportDownloadResult = $reportDownloader->downloadReport($reportDefinition);
        $result = $reportDownloadResult->getAsString();

        $report = Xml::namespacedXMLToArray($result);
        foreach ($report['table']['row'] as $item) {
            $campaign_id = $item['@attributes']['campaignID'];
            $campaign_name = $item['@attributes']['campaign'];
            $cost = $item['@attributes']['cost'];

            if ($cost > 0) {
                $dbCampaign = Campaign::where('code', $campaign_id)->get()->first();

                if ($dbCampaign) {
                    $price = round($cost / 1000000);
                    if ($price) {
                        $dbCost = AdvExpense::updateOrCreate(
                            [
                                'campaign_id' => $dbCampaign->id,
                                'date' => $date,
                            ],
                            [
                                'price' => $price,
                            ]
                        );

                        if ($dbCost->exists) {
                            $this->info($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' OK');
                        } else {
                            $this->error($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' ERROR');
                        }
                    }
                } else {
                    $this->error('Campaign ' . $campaign_name . ' not found!');
                }
            }
        }
    }
}
巷雨优美回忆 2024-10-18 04:14:44

您不应该使用 APILity,因为它支持的 API 版本几年前就已被贬值。该项目被叫停,取而代之的是新的官方 PHP AdWords API 库。它的某些功能仍然有效(例如 AdWords 报告服务),但这种情况不会持续太久。

如果可能的话,我建议使用官方库,因为它使开发和升级变得更加简单,但如果您确实需要更轻的占用空间,您可以尝试使用标准 PHP Soap 扩展自己构建 API 调用。

You shouldn't use APILity as the API version it supports was depreciated several years ago. The project was halted in favour of the new official PHP AdWords API Library. Some functions of it still work (such as the AdWords Reporting Service) but that won't last for too long now.

I would recommend using the official library if possible because it makes development and upgrading so much simpler but if you really need a lighter footprint you can try constructing the API calls yourself using the standard PHP Soap extension.

故事灯 2024-10-18 04:14:44

我建议使用 AdWords 脚本,https://developers.google.com/adwords/scripts/。直接在 AdWords 用户界面中编写 Javascript,可以很好地处理基本内容。

I'd recommend AdWords Scripts, https://developers.google.com/adwords/scripts/. Write Javascript directly in AdWords UI, handles basic stuff pretty well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文