New Google Analytics API / DataSource!
Posted on 18/12/07 by Felix Geisendörfer
Hey folks,
sorry it took me forever, but after my old Google Analytics API fell apart due to the fact that Google published a new interface that also came with new reports / exporting formats I didn't have the time to come up with a new one.
Anyway, in a very productive CakePHP session with nate a couple weeks ago in Atlanta I finally implemented some new HttpSocket stuff that was needed (minimal support for cookies / ssl) for creating the new API as a datasource. Now that its done I'm fairly happy with the outcome of the new API : ).
Without further words: Download the 1.0 version here (A SVN:HEAD version of Cake 1.2 is highly recommended).
It doesn't have pretty API comments yet, so I'm gonna make up for it with a little tutorial:
Step 1: Set up your connection credentials
After you downloaded the API and placed it in app/models/datasources/google_analytics_source.php, open up your app/config.database.php and add the following lines:
'datasource' => 'google_analytics',
'user' => 'my-account@gmail.com',
'password' => 'my-password',
);
Naming your connection $analytics is a choice, anything else will do as well.
Step 2: Set up a little test controller:
class AnalyticsController extends AppController{
var $uses = array();
var $Analytics = array();
function beforeFilter() {
App::import('ConnectionManager');
$this->Analytics =& ConnectionManager::getDataSource('analytics');
}
function list_profiles() {
// List all profiles associated with your account
$profiles = $this->Analytics->listSources();
debug($profiles);
exit;
}
function show_reports() {
// The quickest way to test if the API is working for you.
$report = $this->Analytics->report('Dashboard');
debug($report);
// Use a specific profile id (see list_profiles action above), do:
$report = $this->Analytics->report(array(
'profile' => '290723',
'report' => 'Dashboard'
));
debug($report);
// You can also reference your profile by name instead
$report = $this->Analytics->report(array(
'profile' => 'www.thinkingphp.org',
'report' => 'Dashboard'
));
debug($report);
// Retrieve the raw XML instead of an array to parse it yourself (lets say using SimpleXml in PHP5):
$report = $this->Analytics->report(array(
'profile' => 'www.thinkingphp.org',
'report' => 'Dashboard'
), true);
debug($report);
// Retrieve a PDF report (make sure you set the right header before displaying):
$report = $this->Analytics->report(array(
'profile' => 'www.thinkingphp.org',
'report' => 'Dashboard',
'format' => 'pdf'
), true);
debug($report);
// Set some criteria on the report:
$report = $this->Analytics->report(array(
'profile' => 'www.thinkingphp.org',
'report' => 'Dashboard',
'from' => '2007-12-17',
'to' => '2007-12-18'
));
debug($report);
/**
* Small Reference of options:
*
* profile: The id or name of the profile (optional, default = first profile)
* report: A report string like 'Dashboard', 'TrafficSources', 'Keywords' (optional, default = 'Dashboard')
* from: A yyyy-mm-dd formated date (optional, default = 1 month ago)
* to: A yyyy-mm-dd formated date (optional, default = today)
* tab: 0-4 (optional, default = 0)
* view: 0-9+ (optional, default = 0)
* format: 0-3 or 'pdf', 'xml', 'csv', 'tsv' (optional, default = 'xml')
* compute: 'average', other options unknown? (optional, default = 'average')
* query: Here you can manually pass (and overwrite) any part of the raw HTTP GET query made to Google Analytics. (optional, default = array())
*/
}
}
Now you might wonder how get the list of reports that are available. Well usually that is pretty simple, just check the name of the menu entry you have open when browsing through the google analytics web interface and try using it in CamelCase. For example the 'Search Engines' view under traffic sources is the same as the 'SearchEngines' report. If this does not work, then use Firebug or something else to find the destination of one of the export links on the screen you are looking at. For example on the 'Direct Traffic' screen (under traffic sources as well), the xml export link points to 'export?fmt=1&id=5879334&pdr=20071116-20071216&cmp=average&&rpt=DirectSourcesReport' which means that the name of the report you need for this API is 'DirectSources'. If I get around I might create some listReports() function, but for now this should do the trick.
Another tip: If you are overwhelmed by the information returned by the default xml reports, try using the CSV / TSV ones, they should usually contain less unneeded information, but you'll have to parse them yourself.
Ok, I hope you guys enjoy and let me know if you have any questions / suggestions / problems.
-- Felix Geisendörfer aka the_undefined
Note: This data source uses web scraping. This means that Google could easily break it by making changes to the infrastructure of Google Analytics again. I also haven't heard back from google about their legal views on this technique since Late May 2006, but so far I haven't heard about Google approaching any vendors providing similar access to their service using web scraping either, which is a good sign.