#native_company# #native_desc#
#native_cta#

Create Custom Google Analytics Interfaces Using PHP

By W. Jason Gilmore
on March 18, 2010

Google Analytics (GA) is the de facto solution for analyzing web site traffic and trends. Millions of users around the globe rely on GA for determining not only what web site content is resonating with visitors, but also how visitors came to arrive at the web site,
each visitor’s geographical location, their operating system and browser, and
much more. If you’re not regularly relying upon Google Analytics or a similarly
capable analytical service, then you’re left at a major competitive disadvantage
in terms of your ability to effectively understand the interests, demographics,
and technical requirements of your audience.
Yet logging into GA daily can be a tedious task. Faced with countless other work-related demands, adding even one more requirement can be overwhelming. Further, although you can configure GA to send formatted
reports via email on a daily, weekly, monthly, and quarterly basis (see Figure
1
), sometimes you’re just interested in quickly gaining a bird’s eye view of key
statistics, such as the current number of daily visitors and source of incoming
links. The inconvenience is compounded if your traffic includes mobile-obsessed web users, as
no viable solution for accessing Google Analytics via a mobile device currently
exists.


Google Analytics Reports
Click here for larger image


Figure 1. Google Analytics Report Configuration
Thankfully, last year Google made the Google Analytics Data Export API available for public use. Although still in beta,
it already provides a powerful yet simple way to access your GA data and
format it in a manner most convenient for your consumption, whether that’s by
integrating it into a corporate dashboard, monitoring trends using a desktop
application, or creating a simple mobile interface. In this article, I’ll show
you how to begin taking advantage of this API to create a handy interface
for web consumption.

Introducing the Google Analytics Data Export API

Like many web services, the Google Analytics Data Export API uses HTTP as the transportation protocol, responding to HTTP requests with raw XML data. Many PHP developers typically interact with web services such as this by using the cURL extension to send the HTTP requests and a feature such as the SimpleXML extension to parse the XML. Therefore, while you certainly can build our own solution from scratch, I suggest you use the powerful Google
Analytics API PHP Interface
(GAPI) instead. It’s a powerful open source PHP class that makes interacting with the API trivial.
Browse to the GAPI
web site
to retrieve the latest version of the class, and then place it within an
appropriate place within your project directory. To use this API, You need to
enable PHP’s cURL extension. (This operating system-dependent process is
well documented across the web. So, if you’re not sure how to proceed, do a quick search and you’ll find detailed instructions.) When you’ve installed it, proceed to the next section.

Connecting to the Analytics Service

Because GA data is often considered a mission-critical part of
business operations, you can’t simply connect to the API and retrieve
just any web site’s traffic statistics. You first need to log in to
the service so Google can confirm your account possesses the credentials
necessary to access a web site’s traffic profile. Therefore, you need a Google account, which I presume you already have configured in order to access your analytics data through the Google Analytics web site.
You pass your account username and password to GAPI when invoking the class, like this:

require 'gapi.class.php';
$gapi = new gapi("[email protected]", "secret")

From there, you might use the requestAccountData() method to
retrieve a list of web site profiles accessible by your account:

$gapi->requestAccountData();
foreach($ga->getResults() as $result)
{
  echo "{$result} Profile ID: {$result->getProfileId()}<br />";
}

Executing this example in conjunction with my account produces the following results:

www.wjgilmore.com ProfileID: 12345678
www.easyphpweb sites.com ProfileID: 87654321