我正在尝试使用自定义变量在 session 范围内跟踪用户类型。在每次加载页面时,我想检查 GA 变量是否存在,如果不存在,则将其设置为基于 cookie 的 3 个值之一(访问者、注册、高级)。
TrackUser: function () {
var index = 5; //slot 5
var scope = 2; //set scope to session level
var key = "UserType";
var value;
if (_gaq != undefined) {
_gaq.push(function () { //this is async
var pageTracker = _gat._getTrackerByName(); //Gets the default tracker.
var vis = pageTracker._getVisitorCustomVar(5); //this should return the 'usertype' if its been set
//if no GA variable exists then lets set it
if (vis == undefined) { // !? THIS IS ALWAYS 'undefined' ?!
value = getUserType(); //get the users type
//send custom var to GA
_gaq.push(['_setCustomVar',
index, // This custom var is set to slot #1. Required parameter.
key, // The name acts as a kind of category for the user activity. Required parameter.
value, // This value of the custom variable. Required parameter.
scope // Sets the scope to session-level. Optional parameter.
]);
}
});
}
}
此函数在跟踪事件推送“_trackEvent”之前被调用,因此它可能在调用“_trackEvent”之前未完成。即便如此,它不会在下一页加载时发送吗?
还是我误解了 GA 如何将 cookie 与自定义变量一起使用?
顺便说一句:如果有更简单的方法来获取自定义变量(也许无需向 Google 发送请求)那将是理想的选择。
更新
阅读更多文档后,我意识到“_getVisitorCustomVar”仅返回在“访问者范围”中设置的自定义变量 (3)。谷歌显然在他们的服务器上跟踪“ session 范围”(2)变量,并且不设置客户端 cookie。在找到更优雅的解决方案之前,我只是设置自己的 session cookie,以检查是否已将有关用户类型的信息发送给 GA。
最佳答案
只能从 googles API 调用 _getVisitorCustomVar
获取“Visitor Scope”值
关于javascript - 如何检查是否设置了 Google Analytics 自定义变量( session 范围),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12570096/