Google Analytics Management API PHP Interface
About 9 weeks ago Google released their Google Analytics Managment API which has been huge for the developer community. An open source Python library came out 2 weeks ago and today we’re releasing our open source PHP library.
This library allows your programmers to now access profiles, web properties, and goals programmatically. And because it extends the existing ga:pi() library, you can also programmatically get analytic data straight from Google Analytics at the same time. With the combined power, your developer can get direct access to very specific data from very specific profiles without having to download everything then sorting through it all afterwards. Way faster and way more efficient!
In condensed programmer speak: “This library is a class extension to the GA:PI() PHP5 library. With it, you can access analytic data feeds and management feeds returned as a single PHP5 object needing only one authorization token.” :D
The source repository is available on Google Code or you can download it directly from this blog post. Because it extends the gapi class, you will also need to download that too:
Google Analytics Management API PHP Interface (Google Code project)
Google Analytics Management API PHP Interface extension (Direct download – ZIP file)
How to use:
Using our PHP library is very similar to using the parent library:
require('gapi.class.php');
require('gManagementApi.class.php');
define('GA_EMAIL', 'myUsername');;
define('GA_PASSWORD', 'mySecretPassword');
$ga = new gManagementApi(GA_EMAIL, GA_PASSWORD);
// Get all the accounts
$ga->requestAccountFeed();
// Get all web properties for all your accounts
$ga->requestAccountFeed('~all');
// Get all web properties for specfically UA-12345-2
$ga->requestAccountFeed('12345');
// To get all the profiles you have access to for web property under account 12345
$ga->requestAccountFeed('12345', '~all');
// To get the profiles for a specific web property UA-12345-2 under account 12345
$ga->requestAccountFeed('12345', 'UA-12345-2');
// To get all the goals for profile 6789 for web property UA-12345-2 under acount 12345
$ga->requestAccountFeed('12345', 'UA-12345-2', '~all');
// To get everything and the kitchen sink
$ga->requestAccountFeed('~all', '~all', '~all');
// To get all advanced segments
$ga->requestAdvancedSegmentFeed();
Posted: 10.19.10

king:
I have past it on my website. it is working ok. I like it
Adrian:
Thats awesome I’m going to download it and come back with my thoughts. Well done Wil and the team. Interesting I signed up for the newsletter 3 weeks ago but haven’t gotten any e-mails yet. Wouldn’t articles like this be sent to my inbox? Keep up the great work!
One more thing before I go, we need more youtube videos of Wil.
Jonas:
Hi, Is it possible to retrieve also the goal name ?
I only get a title like this : Google Analytics Goal 1
Thank you
Chris Le:
Yes. From the docs: To get all the goals for profile 6789 for web property UA-12345-2 under acount 12345
$ga->requestAccountFeed('12345', 'UA-12345-2', '~all');The last part “~all” tells Google Analytics that you want to get a list of all the goals, their names, etc.
Good luck!
Ruben:
Hi, I’m having the same problem as Jonas. Even if I use detailed parameters, I only get generic names for the goals.
Thanks in advance
Jonas:
Hi Ruben. I found a solution
add the following code in the accountObjectMapper function:
foreach ($entry->children(‘http://schemas.google.com/ga/2009‘)->goal as $goal){
if ($goal->attributes()->active==’true’){
//$properties['goals'][intval($goal->attributes()->number)] = strval($goal->attributes()->name);
$properties['goals']["goal_name"] = strval($goal->attributes()->name);
$properties['goals']["goal_number"] = strval($goal->attributes()->number);
}
}
Ruben Verhack:
Hi Jonas! Thank you very much for your quick answer! Works perfectly :)