PHP Google Analytics API Examples
Posted by in Google API December 13, 2011 2 Comments

As you already know that Google Analytics is the most popular website analytics and tracking tools that is being used by almost webmaster. For developers, Google Analytics is also the best choice because it supports API that allow to access all reports and data inside their Google Analytics profile.

By using the API, you can create your custom report or extract desired information that you would like to share with your customers/third parties or just display right on your website/blog (WordPress).

There are some great men create great class which helps us access to Google Analyics report data easier. I’m using PHP gapi.class.php class that you can download from http://code.google.com/p/gapi-google-analytics-php-interface/downloads/list

In order to start integrating with the Google Analytics report data, you first need to have Google account login detail to authenticate and the Google Analytics profile ID you would like to access.

A piece of PHP code below with list all Google Analytics Profile IDs belong to your account that may help you to identify the right profile ID to get report data.

1. PHP List All Google Analytics Profile IDs

<?php
	define('ga_email','your_google_account'); // ex: name@gmail.com
	define('ga_password','your_google_password');
 
	require 'gapi.class.php';
 
	$ga = new gapi(ga_email,ga_password);
 
	$ga->requestAccountData();
 
	foreach($ga->getResults() as $result)
	{
		echo $result . ' (' . $result->getProfileId() . ")<br />";
	}
?>

After downloaded the gapi.class.php, there are some examples (also includes PHP example above) you can play with. In this post, I will give you another example that pull the top pages with highest page views.

2. PHP Get Top Highest Page Views

<?php
define('ga_email','your_google_account'); // ex: name@gmail.com
define('ga_password','your_google_password');
define('ga_profile_id','your_profile_id');
 
require 'gapi.class.php';
 
$ga = new gapi(ga_email,ga_password);
 
$ga->requestReportData(ga_profile_id,array('pagePath','pageTitle'),array('pageviews'),'-pageviews', $filter,"2011-11-12", "2011-12-12");
?>
<table border="1">
<tr>
  <th>Page Path</th>
  <th>Page Title</th>
  <th>Page View</th>
</tr>
<?php
foreach($ga->getResults() as $result):
?>
<tr>
  <td><?php echo $result->getpagePath();?></td>
  <td><?php echo $result->getpageTitle();?></td>
  <td><?php echo $result->getpageViews();?></td>
</tr>
<?php
endforeach
?>
</table>
?>

3. Data Feed Query Explorer

After played with some examples which are packed with the downloaded file, you should play with Data Feed Query Explorer to build parameters in queries to get data from Google Analytis profiles.

Data Feed Query Explorer

Data Feed Query Explorer

You have to click on Authenticate With Google Analytics button in order to grant Google access to your Google Analytics profiles.

4. Demonstration

A picture below show how do we build parameters to pull top 50 page with highest page views.

Top Page PageViews Data Feed Query Explorer

Top Page PageViews Data Feed Query Explorer

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.ywordpress.com/wordpress-services/hire-wordpress-developer/ Wordpress Developers

    I have seen a very informative blog. Really I like this blog. This blog gives us very good knowledge about web development.

  • http://reastablish.com/tag/weight-loss-2/page/6/ reastablish

    Hi, Neat post. There’s a problem along with your website in web explorer, would check this? IE nonetheless is the market chief and a good portion of other people will miss your fantastic writing because of this problem.