In this basic tutorial you'll learn how to create a report with Google Analytics API. Follow the next steps to use the Google Analytics API in a few seconds:
1. Download and insert the file googleanalytics.class.php in your hosting.
2. Use the following PHP code in the same folder. Add user, password, profile number and dates, and use the dimensions and metrics you need (check them all at https://developers.google.com/analytics/devguides/reporting/core/dimsmets).
include 'googleanalytics.class.php';
try {
$ga = new GoogleAnalytics('user@gmail.com','password');
$ga->setProfile('ga:48231979');
$ga->setDateRange('2014-10-01','2014-10-31');
$report = $ga->getReport(
array('dimensions'=>urlencode('ga:date,ga:pagePath'), // change dimensions
'metrics'=>urlencode('ga:sessions,ga:bounceRate'), // change metrics
'filters'=>urlencode('ga:country!=@Italy'), // change or remove this filter
'sort'=>'ga:date' // change the sorting criteria
)
);
<pre>
var_dump($report);
</pre>
}catch (Exception $e) {
print 'Error: ' . $e->getMessage();
}
It's very common to confuse an account number with a profile number. The profile number doesn't contain UA-. To find it out, go to Administration > Reporting View Settings, and you'll see the profile number in "View ID".