Reputation: 293
How to get total impressions and clicks for all campaigns on Google AdWords API? Right now I am doing this way
// Get the service, which loads the required classes.
$campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);
// Create selector.
$selector = new Selector();
$selector->fields =
array('Id', 'Name', 'Impressions', 'Clicks', 'Cost', 'Ctr');
$selector->predicates[] =
new Predicate('Impressions', 'GREATER_THAN', array(0));
// Set date range to request stats for.
$dateRange = new DateRange();
$dateRange->min = date('Ym01', time());
$dateRange->max = date('Ymd', time());
$selector->dateRange = $dateRange;
// Make the get request.
$page = $campaignService->get($selector);
// get results.
$impressions = 0;
$clicks = 0;
if (isset($page->entries)) {
foreach ($page->entries as $campaign) {
$impressions += $campaign->campaignStats->impressions;
$clicks += $campaign->campaignStats->clicks;
}
} else {
//print "No matching campaigns were found.\n";
}
return array('impressions'=>$impressions, 'clicks'=>$clicks);
I am wondering if I can just get total without using foreach and loop through the campaigns.
Upvotes: 0
Views: 5042
Reputation: 1
$startDate = date('Ymd', strtotime('2018-10-12'));
$endDate = date('Ymd', strtotime('2018-11-13'));
$query = (new ReportQueryBuilder())
->select([
'CampaignId',
'AdGroupId',
'Impressions',
'Clicks',
'Cost'
])
->from(ReportDefinitionReportType::CRITERIA_PERFORMANCE_REPORT)
->where('Status')->in(['ENABLED', 'PAUSED'])
->during($startDate, $endDate)
->build();
Upvotes: 0
Reputation: 425
To get account-level stats, you can use the AdWords API's ACCOUNT_PERFORMANCE_REPORT
. You can download this report in CSV format.
I am a Rubyist, but I believe that this should work for the PHP client library:
// AdWordsUser credentials come from "../auth.ini"
$user = new AdWordsUser();
$filePath = YOUR_FILE_PATH;
$user->LoadService('ReportDefinitionService', ADWORDS_VERSION);
$selector = new Selector();
$selector->fields = array('AccountId', 'AccountDescriptiveName', 'Impressions', 'Clicks', 'Cost', 'Ctr');
// no predicate necessary - this report already excludes zero-impression lines;
// plus, there is only one line, because it's the whole account
$reportDefinition = new ReportDefinition();
$reportDefinition->selector = $selector;
$reportDefinition->reportName = WHATEVER_YOU_WANT_IT_TO_BE_NAMED;
$reportDefinition->dateRangeType = 'LAST_7_DAYS';
$reportDefinition->reportType = 'ACCOUNT_PERFORMANCE_REPORT';
$reportDefinition->downloadFormat = 'CSV';
$options = array('returnMoneyInMicros' => TRUE);
ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
Upvotes: 1