Skip to content

aniltc/cakephp-plugin-google-analytics

 
 

Repository files navigation

Install

Several possibilities :

  • git clone git://github.com/msadouni/cakephp-plugin-google-analytics.git google_analytics in your plugin directory
  • Download and unzip into a google_analytics folder in your plugins folder
  • If your project is already versioned with Git : git submodule add git://github.com/msadouni/cakephp-plugin-google-analytics.git plugins/google_analytics

Configuration

Duplicate config/google_analytics.php.default into config/google_analytics.php and fill your account information in $google_analytics array

Usage

In a controller :

  • var $uses = array('GoogleAnalytics.GoogleAnalyticsAccount'); or
  • $this->loadModel('GoogleAnalytics.GoogleAnalyticsAccount')

To get all accounts for the given credentials : $this->GoogleAnalyticsAccount->find('all');

This will return an array of Accounts :

[0] =>
    [Account] =>
        [id] => account url
        [updated] => last update datetime
        [title] => account title
        [tableId] => ga:profileId
        [accountId] => account id
        [profileId] => profile id (the id you need to perform searches next)
        [webPropertyId] => tracker id on your website
[1] =>
    [Account] => ...

Grab the Account.profileId you need and get the Account data :

$data = $this->GoogleAnalyticsAccount->find('first', array(
    'conditions' => array(
        'profileId' => $profileId,
        'start-date' => 'YYYY-MM-DD',
        'end-date' => 'YYYY-MM-DD')));

The start-date and end-date conditions are mandatory. You can add other conditions to perform searches :

$data = $this->GoogleAnalyticsAccount->find('first', array(
    'conditions' => array(
        'profileId' => $profileId,
        'start-date' => 'YYYY-MM-DD',
        'end-date' => 'YYYY-MM-DD'
        'dimensions' => 'country',
        'metrics' => 'newVisits',
        'sort' => '-newVisits')));

will get you the new visits per country, ordered by descending new visits, for the period given. you can also pass several dimensions (maximum 7), metrics (maximum 10) and sort options (must match dimensions or metrics given) by passing arrays :

$data = $this->GoogleAnalyticsAccount->find('first', array(
    'conditions' => array(
        'profileId' => $profileId,
        'start-date' => 'YYYY-MM-DD',
        'end-date' => 'YYYY-MM-DD'
        'dimensions' => array('country', 'city'),
        'metrics' => array('newVisits', 'uniquePageviews'),
        'sort' => array('country', 'city', '-newVisits'))));

You can find the allowed dimensions, metrics and sort options on Google Analytics API page

TODO

  • Test HTTP errors on connection and requests
  • Add support for filters
  • Many other stuff I guess, fork it and have fun

THANKS

Sources that have been of great help :

About

Google Analytics plugin for CakePHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published