Google Analytics 自定义维度在用户范围内的使用情况

Google Analytics Custom Dimension usage with user scope

我打算为进入网站特定目录的任何页面的任何访问者设置一个具有用户范围的自定义维度,以将用户分为两组:

我读过这个 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 关于用户范围的变量如何获取它们的值:简而言之,最后一次点击被保存。

所以我想到了以下方法,仅当用户访问此目录中的任何页面时才使用用户范围设置自定义变量,并在报告中使用 include/exclude 功能来分隔这两个组,但我'恐怕我可能会遗漏一些东西。

我认为您的方法可行,但您应该记住,任何没有为此自定义维度分配值的用户都将被排除在您使用该维度的任何报告之外。

我认为您最好设置一个 cookie,以跟踪用户是否曾经访问过相关页面。这样你就可以随时发送一个值。下面我借用了 w3schools.com 的 cookie 功能,因为我不习惯 javascript cookie。我相信您可以找到更短的方法。

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