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

Install

Philip Lawrence edited this page Nov 27, 2012 · 4 revisions

Google Analytics Extension

This wiki is about how to install and configure the Google Analytics extension in Yii.

Installation

Step 1: Upload the files

The first step is straightforward; simply unzip the files from the latest downloads into your extensions folder. You should now be able to navigate to protected/extensions/TPGoogleAnalytics/components/ and see a file called TPGoogleAnalytics.php

Optional: Git Submodule

You can also install this as a Git submodule. This can be done with running the following command in the root of your project, where the path to the extensions folder (protected/extensions) might need updating.

git submodule add git://github.com/TagPlanet/yii-analytics-ga.git protected/extensions/TPGoogleAnalytics

By using a submodule, this will guarantee you'll have access to the latest version at all times.

Step 2: Add in configuration

Within your configuration files (usually found under /protected/config/) there is the "components" section. Just like your db and cache components, we'll need to add in our own configuration for this. Add in the following code within the components section:

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

Step 3: (Optional) Add in auto-render

In order for the Google Analytics component to automatically render the code in the header, you must have the following two items configured:

  1. Configuration file - within the googleAnalytics configuration, you must include:
'googleAnalytics' => array(
    'class' =>'ext.TPGoogleAnalytics.components.TPGoogleAnalytics',
    'account' => 'UA-########-#',
    'autoRender' => true,
),
  1. Controllers - your controllers must have the following code:
protected function beforeRender($view)
{
    $return = parent::beforeRender($view);
    Yii::app()->googleAnalytics->render();
    return $return;
}

You can place this either within protected/components/Controller.php (or whichever Controller you are overloading) or within every single one of your controllers. In the event that you already have the method beforeRender within your controllers, simple add in the following line to it, before the return statement:

Yii::app()->googleAnalytics->render();

Configuration Options

This component allows for some flexibility within the configuration section. Below are all of the allowed configuration variables:

  • class - The TPGoogleAnalytics class location
    • Required: yes
    • Type: string
    • Default: ext.TPGoogleAnalytics.components.TPGoogleAnalytics
  • account - Your Google Analytics account ID
    • Required: yes
    • Type: string
    • Format: UA-########-#
    • Default: (none)
  • autoRender - Automatically render the Google Analytics code in the head. If you do set this to true, you will need to update your controller's beforeRender method (see:[GoogleAnalyticsInstallation#Step_3:_(Optional)_Add_in_auto-render Optionally adding in auto-render code])
    • Required: no
    • Type: boolean
    • Recommend Setting: true
    • Default: false
  • autoPageview - Automatically add _trackPageview to the code. By disabling this, you will need to call Yii::app()->googleAnalytics->_trackPageview(); for each view you want to track. Even when this is enabled, you can still call _trackPageview with a specified page name and it will not call _trackPageview twice.
    • Required: no
    • Type: boolean
    • Recommend Setting: true
    • Default: true
  • debug - Changes Google's JS to their (ga_debug.js file)[https://developers.google.com/analytics/resources/articles/gaTrackingTroubleshooting#gaDebug]
    • Required: no
    • Type: boolean
    • Recommend Setting: false
    • Default: false
  • prefix - Allow for multiple instances of Google Analytics on the page via namespacing in the JavaScript. See (Google's Docs)[https://developers.google.com/analytics/devguides/collection/gajs/#MultipleCommands] for more information.
    • Required: no
    • Type: string
    • Recommend Setting: ''
    • Default: ''