Advanced - TagPlanet/yii-analytics-ga GitHub Wiki

Google Analytics Extension

This wiki is about the advanced usage for the Google Analytics extension in Yii.

Debugging Google Analytics instances

To debug an instance, you set the debug parameter to true. This will change the location of the JS file that is loaded to a development-specific version, which pushes more information into your browsers console. [@TODO] It also pushes information into the Yii logs so that you can easily spot what variables are set when.

Multiple Google Analytics instances

There are times when you need to have multiple Google Analytics instances on a site. Usually the custom variables do not line up, different account numbers, etc. Because of this, we should create a separate instance of the Google Analytics component in the config files.

In a normal setup, you'd see something like the following in /protected/configs/main.php:

'googleAnalytics' => array(
    'class' => 'ext.TPGoogleAnalytics.components.TPGoogleAnalytics',
    'account' => 'UA-1234567-98',
),

However in this case, we'd need 2 of these. This creates a problem though - the names of the components are the same and thus the later one would overwrite the first one. Instead, different component names should be created. It is suggested to use something like the vendor's name (if you're using a 3rd party agency) or an internal identifier for that Google Analytics instance. Let's say we're using TagPla.net as an analytics vendor and an in-house solution as the other. We'll call one instance tpGoogleAnalytics for the TagPla.net isntance and the other intGoogleAnalytics for the internal (or in-house) instance. The configuration will now look like the following:

'tpGoogleAnalytics' => array(
    'class' => 'ext.TPGoogleAnalytics.components.TPGoogleAnalytics',
    'account' => 'UA-1234567-98',
    'namespace' => 'tp',
),
'intGoogleAnalytics' => array(
    'class' => 'ext.TPGoogleAnalytics.components.TPGoogleAnalytics',
    'account' => 'UA-1234567-99',
    'includeFile' => false,
),

It should be noted that different namespaces (at least 1 is required) need to be used. Otherwise the second instance will overwrite the first. Also, included in the second component, you'll see that includeFile is disabled. This will make turn off the output (as seen below) isn't forcing your visitors to load the GA files twice.

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

If you should enable autoRender, remember to change the component names:

<?php

protected function beforeRender($view)
{
    $return = parent::beforeRender($view);
    Yii::app()->tpGoogleAnalytics->render();
    Yii::app()->intGoogleAnalytics->render();
    return $return;
}

Then later on in your controller/view, you could then call the following like normal:

Yii::app()->tpGoogleAnalytics->_setCustomVar(1, 'User Group', $User->Group->Name, 2);
Yii::app()->intGoogleAnalytics->_setCustomVar(1, 'User Group', $User->Group->Name, 2);