Skip to content

Instantly share code, notes, and snippets.

@psahni
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save psahni/9368035 to your computer and use it in GitHub Desktop.

Select an option

Save psahni/9368035 to your computer and use it in GitHub Desktop.
Custom Variables Description - Getting Started

#Custom Variables

  • Custom variables are a way for you to insert custom data into Google Analytics.

  • Very useful for analyzing user behavior on page views

  • There are 4 parts to a custom variable: 1. The name of the variable 2. The values for each variable 3. The index or slot of the variable and the scope of the variable 4. The scope of the variable

  • Example Syntax:- _setCustomVar(index, name, value, opt_scope)

    • When user visits his dashboard after successful login, you can set custom variable like this- _gaq.push(['_setCustomVar',1,'Status','Logged-in',1]);
    • As soon as user logs out, you can set custom variable like this- _gaq.push(['_setCustomVar',1,'Status','Logged-out',1]);
  • There are three kind of custom variables

    • Page level (value = 3)
    • Session level (value = 2)
    • Visitor level (value = 1)

    Visitor level custom variables are stored in the cookie, while session level session level variables are maintained from the server side. Mostly we use page level custom variables.

  • Access Custom variables

    • Login to your gmail account
    • Go to analytics.google.com
    • In the Audience section, Custom -> Custom variables
  • We can set 5 custom variables

  • Location of custom variable code It is very important to put the custom variable code at the right place. Here is a sample code

        var _gaq = _gaq || [];
         //This is tracking ID. Set it from your admin section google analytics account
        _gaq.push(['_setAccount', 'UA-16297xxx-4']);
        _gaq.push(['_setDomainName', 'example.com']);
         // Always put setting custom variables code before the _trackPageview
        _gaq.push(['_setCustomVar',1,'VisitorType','Member', 1]);    
         //This _trackPageview sends the GIF request to the google
        _gaq.push(['_trackPageview']);
    
        (function() {
          var gac = document.createElement('script'); gac.type = 'text/javascript'; gac.async = true;
          gac.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gac, s);
        })();
  • Useful Resources - https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables?hl=en http://cutroni.com/blog/2011/05/18/mastering-google-analytics-custom-variables/ http://cms.pollen.io/google-analytics-applications/ http://techpad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment