Search code examples
javascriptgoogle-analyticsweb-analytics

Google Analytics Custom Dimension usage with user scope


I'm planning to set a custom dimension with user scope for any visitor that goes into any page of certain directory in a website to classify users into two groups:

  • Visitors that have visited (ever) the section in mentioned directory.
  • Visitors that haven't visited (ever) the section in the mentioned directory.

I read this question talking about a similar matter and it states that google analytics' custom dimensions do not have default values so a solution to this is to always send the default value and only change it when the criteria is met but this comes into conflict (according to me) with google analytics documentation about how user scoped variables get their value: in short the last hit gets saved.

So I was thinking of the following approach, only set the custom variable with user scope when users visit any page within this directory and in the reporting use the include/exclude functionality to separate these two groups but I'm afraid I might be missing something.

  • Will this approach work considering that if a hit on this custom dimension does not happen google analytics won't consider it in the aggregations?
  • How can I set the custom variable to catch this users but dont overwrite it if they visit a page outside this directory?

Solution

  • I think your approach would work but you should bear in mind that any user who hasn't been assigned a value for this custom dimension will be excluded from any the reports that you do that have that use that dimension.

    I think you'd be better off setting a cookie that will track if the user has ever been to the pages in question. That way you could always send a value. Below I've borrowed the cookie functions off w3schools.com as I'm not accustomed to javascript cookies. I'm sure you could find a shorter way to do it.

    //Cookie functons borrowed from w3schools.com
    function setCookie(cname, cvalue, exdays) {
        var d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        var expires = "expires="+d.toUTCString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
    }
    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
        return "";
    }
    
    //start of Standard code goes here
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-XXXX-Y', 'auto');
    
    //now check for cookie value
    var everBeen = getCookie('everBeen');
    if(everBeen != 'hasBeen'){
        var path = window.location.pathname;
        if(path.indexOf('/requireddirectory') != -1){
            everBeen = 'hasBeen';
        } else{
            everBeen = 'neverBeen';
        }
    }
    
    setCookie('everBeen',everBeen,1461);
    
    ga('send', 'pageview', {
      'dimension1':  everBeen
    });