Hi!

We implemented an internal tool for our company that shows some stats like 
how many orders we had with our online shops on what days, etc. A few days 
ago we added the adwords costs to it. We used the adwords API with the 
official PHP library (https://github.com/googleads/googleads-php-lib/).
Unfortunately this week we've seen irregularities in the used 
"CRITERIA_PERFORMANCE_REPORT". We download the report in CSV format and 
then parse it with PHP. One of our OM guys told me on 18th, that the cost 
for 17th was indeed wrong. I did let our script download the report as 
"CSVFOREXCEL" to check it, converted all "." to "," (the German version of 
Excel needs that) and loaded it into Excel. I looked up the totals and they 
hat the correct data. So I debugged the code over and over and couldn't 
find any problem.
As a last step I removed the "Total"-line in the CSV in Excel and did let 
Excel calculate the sum on it's own (like the script does, that imports the 
data into our database (for caching historic data) and I didn't trust my 
eyes. The sum was what not same like the one in the total line.

Here's the method for downloading the report (the used adwords api version 
is 201402):
protected function downloadStatsReport($file, $filetype = 'XML', 
$adWordsUser, $type = self::TODAY, $timestamp = null, $timestamp2 = null) {
// selector
$selector = new Selector();
$selector->fields = array(
'Impressions',
'Clicks',
'Cost',
'Conversions',
'ConversionValue',
);
if ($type == self::CUSTOM) {
if ($timestamp2 == null || intval($timestamp2) == 0) {
$selector->dateRange = new DateRange(date('Ymd', $timestamp), date('Ymd', 
$timestamp));
}
else {
$selector->dateRange = new DateRange(date('Ymd', $timestamp), date('Ymd', 
$timestamp2));
}
}

$selector->predicates[] = new Predicate('Status', 'NOT_IN', 
array('DELETED'));

// report definition
$reportDefinition = new ReportDefinition();
$reportDefinition->selector = $selector;
switch ($type) {
case self::TODAY:
$reportDefinition->dateRangeType = 'TODAY';
break;
case self::YESTERDAY:
$reportDefinition->dateRangeType = 'YESTERDAY';
break;

case self::CUSTOM:
$reportDefinition->dateRangeType = 'CUSTOM_DATE';
break;
}
 $reportDefinition->reportType = 'CRITERIA_PERFORMANCE_REPORT';
$reportDefinition->downloadFormat = $filetype;
$reportDefinition->includeZeroImpressions = FALSE;
$reportDefinition->reportName = basename(__FILE__).'#'.basename($file);

// options
$options = array('version' => ADWORDS_VERSION, 'returnMoneyInMicros' => 
FALSE);

$adWordsUser->LoadService('ReportDefinitionService', ADWORDS_VERSION);
ReportUtils::DownloadReport($reportDefinition, $file, $adWordsUser, 
$options);
}

 And here the method that calls it (and calculates the sums for the stats, 
that we import in our database):
protected function getStatsForShopsFromCSV($type = self::TODAY, $timestamp 
= null, $timestamp2 = null) {
$data = array();
foreach ($this->getAdWordsShops() as $id => $shop) {
$data[$id] = array(
'impressions' => 0,
'clicks' => 0,
'cost' => 0.0,
'convertedClicks' => 0,
'totalConvValue' => 0.0,
'clickConversionRate' => 0.0,
);

foreach (explode(',', $shop['adwords_campaign_accounts']) as $customerID) {
$user = $this->getAdWordsUser($shop['adwords_mcc'], $customerID);

$reportFile = $this->getTempfileName($this->command.'_report', 'csv');
$this->downloadStatsReport($reportFile, 'CSV', $user, $type, $timestamp, 
$timestamp2);

$csv = fopen($reportFile, 'r');
// skip first line and header
fgets($csv);
fgets($csv);

while ($line = fgetcsv($csv)) {
if (count($line) < 5) continue;
if (strtolower($line[0]) == 'total') break;

$data[$id]['impressions'] += $line[0];
$data[$id]['clicks'] += $line[1];
$data[$id]['cost'] += $line[2];
$data[$id]['convertedClicks'] += $line[3];
$data[$id]['totalConvValue'] += $line[4];
}

fclose($csv);

if ($data[$id]['clicks'] > 0) {
$data[$id]['clickConversionRate'] = $data[$id]['convertedClicks'] / 
$data[$id]['clicks'] * 100;
}

unlink($reportFile);
}
}

return $data;
}


Maybe somebody can help me and tell me why I have that difference.

Thanks in advance.

Kind Regards,
Markus Zhang

PS: There are no deleted campaigns, so it can't have anything to do 
filtering them out.
PS2: Please excuse the code quality and the missing comments. This is only 
a first version. It will get improved as soon as I'm sure there is no 
problem with it.

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and Google+:
https://googleadsdeveloper.blogspot.com/
https://plus.google.com/+GoogleAdsDevelopers/posts
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to