Home / PHP / Google Analytics API PHP Class – Updates May 22nd 2009

Google Analytics API PHP Class – Updates May 22nd 2009

I’ve made some updates to my Google Analytics API PHP Class today and posted the updated class and example files. The changes are documented here.

auth property is now public

The auth property which contains the authorization key after you’ve logged in is now a public property. You could therefore now do this:

$api1 = analytics_api();
$api1->login($email, $password);

$api2 = analytics_api();
$api2->auth = $api1->auth;

You could also save the auth key into a database and use it for subsequent requests; once authorized it’s valid for several days and this can cut down initial request time.

data method start and end parameters

I’ve added the ability to specify the values “today”, “yesterday” and “week” as the $start parameter when calling the data method. “week” sets the start and end dates to be the 7 days before today. Leaving them as false defaults to the month before today (i.e. the same behaviour as previously).

filters parameter added to the data method

The $filters parameter has been added to the data method for specifying filters. When a value is passed in for this parameter it adds “&filters=$filters” to the request string. I haven’t used filters yet myself with the exception of a couple of tests when adding this option.

debug parameter added to the data method

If you set debug to true when calling data the url that is used to call the Analytics API is echoed out to standard output which is useful for seeing what’s going on behind the scenes. If run from the CLI will echo it along with a linebreak; otherwise will put it in a <p> tag and end with a newline.

get_summary method added

A new method get_summary was added to get summary information for a profile: number of visits, number of pageviews, average time on site and pages per visit. I’ll post more about this new function tomorrow.

get_summaries method added

Works the same as the above but gets summaries for all profiles you have access to.

sec2hms method added

This function is from http://www.laughing-buddha.net/jon/php/sec2hms/ and is used to format the seconds returned in the get_summary method into h:m:s format.