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: [email protected] 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: [email protected] 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.
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.