#native_company# #native_desc#
#native_cta#

Create Custom Google Analytics Interfaces Using PHP Page 2

By W. Jason Gilmore
on March 18, 2010

Retrieving a Web Site’s Traffic Data

Notice how in the previous example I retrieved the profile IDs of each
web site associated with my account. This is relevant because you request a
web site’s statistics using its assigned profile ID. You accompany the profile
ID with several parameters that determine the nature of the data you’d
like to query. You start by defining the query dimension, which
determines the context in which you’d like to view the data. For instance, you
might wish to learn more about the total number of page views for a given time
period, but in what context? Page views according to country? Browser? Visitor
type (returning or new)? You define this context (seven of which are allowed)
within the requestReportData() method’s first parameter.
Next, you define the desired metrics. I already mentioned page views as a
metric example, but you can also query for useful data such as total time
spent on the site, total visitors, and—if AdWords is integrated into your
Analytics account—data such as the total number of times users clicked on a
particular ad. To view a complete list of dimensions, metrics, and other
parameters, see the Dimensions
and Metrics Reference
.
Let’s consider an example. Suppose you wanted to retrieve a list of visits that have occurred during the past 24 hours according to country:
$ga->requestReportData(12345678, array('country'), // Set the context
array('visits'), // Retrieve total number of visits
array('-visits'), // Display in descending order
'', // Optional filter
'2010-03-09', // Query start date
'2010-03-09'); // Query end date

foreach($ga->getResults() as $result)
{
echo '<strong>'.$result.'</strong><br />';
echo 'Visits: ' . $result->getVisits() . '<br />';

Executing this script produces output similar to the following:

India
Visits: 11
United Kingdom
Visits: 11
United States
Visits: 9
Spain
Visits: 5
Croatia
Visits: 3
Indonesia
Visits: 3
Romania
Visits: 3

This is just one of countless query combinations at your disposal. To learn more about what’s possible, see the Google Analytics Data Export API’s Data
Feed documentation
.

So Much More Is Possible

The example provided in this tutorial barely touches the surface in terms of
what’s possible with the Google Analytics Data Export API. Check out this
gallery of examples
for some insight and inspiration regarding just how far you
can go using this powerful API. And as always, if you’ve done something
interesting with what you learn in this tutorial, let us know about it in the
comments!

About the Author

Jason Gilmore is the founder of EasyPHPWebsites.com and the author of
several popular books, including “Easy
PHP Web sites with the Zend Framework
” and “Beginning
PHP and MySQL: From Novice to Professional
” (currently in its third edition).
Check out his new DZone reference card, titled “Getting
Started with the Zend Framework
.”