简体   繁体   English

Google 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? 如何设置自定义变量以捕获此用户,但如果他们访问此目录外的页面,则不会覆盖它?

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. 我认为你最好设置一个cookie来跟踪用户是否曾经去过相关网页。 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. 下面我借用了w3schools.com上的cookie功能,因为我不习惯使用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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM