Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Google API PHP Google Analytics API Examples

PHP Google Analytics API Examples

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 />";
	}
?>

<?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>
?>

<?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.

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

Dec 13, 2011Hoan Huynh
Top iPhone/ iPad apps For IT Administrators Or IT SupportList Of Ads Networks For Publishers And Bloggers
You Might Also Like:
  • Export Google Analytics Report To PDF In New Version
  • How To Export Google Analytics Report To PDF Or XML
  • Monitor Real Time Visitors, Viewing Pages And Traffic Sources In Google Analytics
  • How To Add User To Your Google Analytics Report
  • How To Send Google Analytics Report To Emails
  • How To Tracking Click Event With Google Analytics
  • How To Track Website With Multiple Google Analytisc Accounts
  • PHP open source google analytics competitor
  • Top Free Real Time Website Analytics Services And Tools
  • All Google Service APIs
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago Google APIGoogle, Google Analytics732
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,455 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,836 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,652 views
JQuery Allow only numeric characters or only alphabet characters in textbox
14,986 views
C# Read Json From URL And Parse/Deserialize Json
11,719 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development