Ultra handy PHP Google Analtyics API Class by Doug Tan

Over the past few weeks I’ve been using a PHP Google Analytics API Class that I’ve really been enjoying and figured I should make a post to share it with all of you. The class is just a single PHP file that does a lot of heavy lifting for you and lets you query Google Analytics with just a few lines of PHP.

I’ve been using the Analtyics API in a couple different projects, the main one being Metric. After you’ve included the PHP class, you can call all up pageviews reports (and much more) with some markup along the lines of:

include "googleanalytics.class.php";
try {
$ga = new GoogleAnalytics(‘{email}’,'{password}’);
$ga->setProfile(‘{GA Profile ID}’);
$ga->setDateRange(‘2005-01-01’, date(‘Y-m-d’));
$report = $ga->getReport(
array(‘dimensions’ => urlencode(‘ga:pagePath’),
‘metrics’ => urlencode(‘ga:pageviews’),
‘sort’ => ‘-ga:pagePath’,

// do something with $report

This isn’t really meant to be a full-on lesson or anything, I was mainly just posting to share PHP Google Analytics API Class“>the link. So bookmark it. You might need it sometime.

DATE20th October, 2011


TAGS , ,

Leave a Reply