Google AdWords API – Part 5
The last article about Google AdWords API is about how to get additional informations for keywords. For this example we want to know how much turnover a keyword has generated for a special date range (for example a day). This value (totalConvValue) was a part from the AdGroupCriterionService, but is now deprecated. This is the reason we now have to get it from another service.
ReportDefinitionService
Reports can be get from ReportDefinitionService. This works like all other services. The only difference is, that you can only get the whole report as XML or CSV. This is the reason, why it is a bit inconvenient. We have to store the file and the parse it to get the information. The code looks as follows:
function DownloadKeywordReport(AdWordsUser $user, $filePath, $adwords_version, $keyword_id, $days) { // Load the service, so that the required classes are available. $user->LoadService('ReportDefinitionService', $adwords_version); // Create selector. $selector = new Selector(); $selector->fields = array('KeywordText', 'Id', 'TotalConvValue'); // Filter out deleted criteria. //$selector->predicates[] = new Predicate('Status', 'NOT_IN', array('DELETED')); $selector->predicates[] = new Predicate('Id', 'IN', array($keyword_id)); $dateRange = new DateRange(); $str1 = '-1 days'; $str2 = '-1 days'; if($days > 0) { $str1 = '-'.$days.' days'; $str2 = '-'.$days.' days'; } $dateRange->min = date('Ymd', strtotime($str1)); $dateRange->max = date('Ymd', strtotime($str2)); $selector->dateRange = $dateRange; // Create report definition. $reportDefinition = new ReportDefinition(); $reportDefinition->selector = $selector; $reportDefinition->reportName = 'Criteria performance report #' . uniqid(); $reportDefinition->dateRangeType = 'CUSTOM_DATE'; $reportDefinition->reportType = 'KEYWORDS_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' => $adwords_version, 'returnMoneyInMicros' => FALSE); ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options); }
We first load our service. Then we create a selector and define what we want from the report service. We set the type of report, which is KEYWORDS_PERFORMANCE_REPORT. The format is XML. With DownloadReport we get the report and store it into a file on disk. The path and filename are set by params.
Parse your report
We now have a XML file with our needed data. The last step is to parse the XML file and get the values we want to store into database. This can be done the following way:
private function getTotalConvValuefromXML($file) { $xml = simplexml_load_file($file); foreach($xml->children() as $child) { if(strcmp($child->getName(),'table')===0) { foreach($child->children() as $row) { if(strcmp($row->getName(),'row')===0) { //echo "TotalConvValue: ".$row['totalConvValue']; return $row['totalConvValue']; } } } } return 0; }
Part 1 | Part 2 | Part 3 | Part 4 | Part 6
Greetings,
Awesome article on the AdWords API.
I’m looking for a way to retrieve Feeds or Business data, at least the id’s, in order to modify them. Maybe you have a reference.
Thank you.