我正在寻找一种简单的解决方案,通过API从Google AdWords中提取每日费用。我查看了Apility和官方AdWords API,但第一个不再维护,而第二个是过度杀伤 - 我的意思是76MB未压缩的代码只能获得每日费用?
有人知道从Google AdWords中获取费用的简单解决方案吗?
答案 0 :(得分:2)
如果您指的是API费用,则会在AdWords API Tutorial中使用GetUnits调用。
如果您指的是投放广告系列费用,则可以使用StatsSelector。您需要成本/ microAmount。
答案 1 :(得分:0)
您不应该使用APILity,因为它支持的API版本在几年前已经过折旧。该项目暂停,以支持新的官方PHP AdWords API库。它的某些功能仍然有效(例如AdWords报告服务)但现在不会持续太长时间。
我建议尽可能使用官方库,因为它使开发和升级变得更加简单,但如果你真的需要更轻的占用空间,你可以尝试使用标准的PHP Soap扩展来自己构建API调用。
答案 2 :(得分:0)
我推荐使用AdWords脚本https://developers.google.com/adwords/scripts/。直接在AdWords用户界面中编写Javascript,可以很好地处理基本内容。
答案 3 :(得分:0)
使用
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!');
}
}
}
}
}