我在一个文档链接圈中完全迷失了,没有任何东西给我我想要的答案。
我想要实现的目标很简单,点击一个API端点并获取广告系列的关键数据(每次点击费用,点击次数,展示次数,费用等)。可能将其设置为每天运行的cron。
在此处https://github.com/googleads/googleads-php-lib的Github页面上,它会将您带到此链接,以代表您的客户端(网络流)设置API访问权限https://github.com/googleads/googleads-php-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow)
我已经按照上述说明创建了adwords clientID和客户机密,但是接下来的步骤要求将用户重定向到登录页面以确认访问权限?
是否仍然存在此问题,因为它根本无法在cron上每周工作一次以获取一些数据,因此用户将必须手动进行操作。
答案 0 :(得分:1)
您必须设置OAuth2进行api访问,应遵循these 的说明。
因此,现在您已经为adsapi_php.ini
配置了凭据,并可以查询api。
然后,您可以使用php sdk
来执行查询:
$dir = '/adsapi_php.ini' // the path of your .ini file;
require '/vendor/autoload.php' // load the sdk;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
... // load what you need
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile($dir)
->build();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$adWordsServices = new AdWordsServices();
以上只是示例,我建议您以github表格的形式查看更详细,更有效的示例。
修改
要获得有关一个广告系列的统计信息,您必须执行Criteria performance report。
这是我通常使用的代码示例:(请注意,这是大型应用程序的一部分,可能无法直接使用)。它会生成广告系列的上周报告。
$dir = 'path/to/adsapi_php.ini';
require 'path/to/vendor/autoload.php';
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\AdWords\v201802\cm\AdGroupService;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdService;
use Google\AdsApi\AdWords\v201802\cm\OrderBy;
use Google\AdsApi\AdWords\v201802\cm\PolicyApprovalStatus;
use Google\AdsApi\AdWords\v201802\cm\SortOrder;
use Google\AdsApi\AdWords\v201802\cm\Paging;
use Google\AdsApi\AdWords\v201802\cm\ExpandedTextAd;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdOperation;
use Google\AdsApi\AdWords\v201802\cm\AdType;
use Google\AdsApi\AdWords\v201802\cm\AdGroupAdStatus;
use Google\AdsApi\AdWords\Reporting\v201802\DownloadFormat;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDefinition;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDefinitionDateRangeType;
use Google\AdsApi\AdWords\Reporting\v201802\ReportDownloader;
use Google\AdsApi\AdWords\ReportSettingsBuilder;
use Google\AdsApi\AdWords\v201802\cm\Predicate;
use Google\AdsApi\AdWords\v201802\cm\PredicateOperator;
use Google\AdsApi\AdWords\v201802\cm\ReportDefinitionReportType;
use Google\AdsApi\AdWords\v201802\cm\Selector;
use Google\AdsApi\AdWords\v201802\cm\DateRange;
use Google\AdsApi\Common\OAuth2TokenBuilder;
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile($dir)
->build();
$adWordsServices = new AdWordsServices();
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->withClientCustomerId($accountId)
->build();
getaveragestats($oAuth2Credential, $session,$accountId,$campaignid,$campaingname);
function getaveragestats($oAuth2Credential, $session,$accountId,$campaignid,$campaingname){
$oneweekago = strtotime("-1 week");
$oneweekAgo = date("Ymd",$oneweekago);
$datefrom = $oneweekAgo;
$dateto = date("Ymd");
$selector = new Selector();
$selector->setDateRange(new DateRange($datefrom, $dateto));
$selector->setFields(['Date','Impressions','AveragePosition','AverageCpc']);
$selector->setPredicates([
new Predicate('CampaignId', PredicateOperator::IN, [$campaignid])]);
$reportDefinition = new ReportDefinition();
$reportDefinition->setSelector($selector);
$reportDefinition->setReportName('Report per la campagna #' . $campaignid);
$reportDefinition->setDateRangeType(ReportDefinitionDateRangeType::CUSTOM_DATE);
$reportDefinition->setReportType(ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT);
$reportDefinition->setDownloadFormat(DownloadFormat::CSV);
// Download report.
$reportDownloader = new ReportDownloader($session);
$reportSettingsOverride = (new ReportSettingsBuilder())
->includeZeroImpressions(false)
->build();
$reportDownloadResult = $reportDownloader->downloadReport($reportDefinition, $reportSettingsOverride);
$reportAsString = $reportDownloadResult->getAsString();
$your_array = array_filter(explode("\n",$reportAsString));
$titolo = $your_array[0];
$rowNames = $your_array[1];
$rowNames = explode(",", $rowNames);
$total = end($your_array);
$tot = explode(",", $total);
$result = [];
for ($x = 2; $x < (count($your_array)-1); $x++) {
$row = explode(",", $your_array[$x]);
$tmp = array(
'data'=>$row[0],
'impressions'=>$row[1],
'position'=>$row[2],
'cpc'=> intval($row[3])/100000
);
$result[]=$tmp;
}
function sortFunction( $a, $b ) {
return strtotime($a["data"]) - strtotime($b["data"]);
}
$avrg = getAverage($result);
return $avrg
}