Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

GoogleAnalytics Advanced

Philip Lawrence edited this page Oct 10, 2012 · 1 revision

Google Analytics Extension

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

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',
),

It should be noted that different namespaces (at least 1 is required) need to be used. Otherwise the second instance will overwrite the first.

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);