Reputation: 5185
I have to get the main info about my Google Analytics Goals.
I'm using GAPI lib, with this code:
<?php
require_once 'conf.inc';
require_once 'gapi.class.php';
$ga = new gapi(ga_email,ga_password);
$dimensions = array('pagePath', 'hostname');
$metrics = array('goalCompletionsAll', 'goalConversionRateAll', 'goalValueAll');
$ga->requestReportData(ga_profile_id, $dimensions, $metrics,
'-goalCompletionsAll', '', '2012-09-07', '2012-10-07', 1, 500);
$gaResults = $ga->getResults();
foreach($gaResults as $result)
{
var_dump($result);
}
cut this code is output:
object(gapiReportEntry)[7]
private 'metrics' =>
array (size=3)
'goalCompletionsAll' => int 12031
'goalConversionRateAll' => float 206.93154454764
'goalValueAll' => float 0
private 'dimensions' =>
array (size=2)
'pagePath' => string '/catalogs.php' (length=13)
'hostname' => string 'www.example.com' (length=13)
object(gapiReportEntry)[6]
private 'metrics' =>
array (size=3)
'goalCompletionsAll' => int 9744
'goalConversionRateAll' => float 661.05834464043
'goalValueAll' => float 0
private 'dimensions' =>
array (size=2)
'pagePath' => string '/price.php' (length=10)
'hostname' => string 'www.example.com' (length=13)
What I see on Google Analytics website on Goals URLs page with the same period of date is:
Goal Completion Location Goal Completions Goal Value
1. /price.php 9,396 $0.00
2. /saloni.php 3,739 $0.00
As you can see outputs doesn't match. Why? What's wrong?
Upvotes: 0
Views: 963
Reputation: 4050
also make sure you are not sampling. Google automatically samples and extrapolates if you have a) more than 50,000 views in the time period you query and b) not have analytics premium
There is an easy workaround, just have a loop that queries every day and at the end of the loop increments the date variable.
Upvotes: 0
Reputation: 330
You'll need to match the goal expression with the pagePath. First thing you've got to do download the gManagement extension from this site http://www.seerinteractive.com/blog/google-analytics-management-api-phpinterface-for-php. Then follow this steps:
1) Add the following code to the accountObjectMapper method from gapi class:
foreach ($entry->children('http://schemas.google.com/ga/2009')->goal as $goal){
if ($goal->attributes()->active=='true'){
$properties['name'] = strval($goal->attributes()->name);
$properties['number'] = strval($goal->attributes()->number);
foreach ($goal->children('http://schemas.google.com/ga/2009')->destination as $destination){
$properties['expression'] = strval($destination->attributes()->expression);
$properties['matchType'] = strval($destination->attributes()->matchType);
}
}
This must be inside the Load Entry loop.
2) Get the goals expression and matchType through the gManagement class:
$gm = new gManagementApi($account, $password);
$profiles = $gm->requestAccountFeed('~all', '~all');
$account_id = '';
$property_id = '';
foreach ($profiles as $profile) {
if ($profile->getProfileId() == $profile_id) {
$account_id = $profile->getAccountId();
$property_id = $profile->getWebPropertyId();
break;
}
}
$goals = $gm->requestAccountFeed($account_id, $property_id, '~all');
3) Build the filter for your PagePath query:
$filter = '';
foreach ($goals as $goal) {
if ($goal->getMatchType() == 'head') {
$filter .= "ga:pagePath=~^" . $goal->getExpression() . ",";
} else if ($goal->getMatchType() == 'exact') {
$filter .= "ga:pagePath==" . $goal->getExpression() . ",";
} else {
$filter .= "ga:pagePath=@" . $goal->getExpression() . ",";
}
}
return substr($filter,0,-1);
4) Make the same query your doing adding the built filter:
$ga->requestReportData(ga_profile_id, $dimensions, $metrics, '-goalCompletionsAll', $filter, '2012-09-07', '2012-10-07', 1, 500);
It's a lot of steps, but worked for me. Hope it works for you.
Upvotes: 1