我正在使用Google的analytics.js,当我们访问网站时,我需要知道用户是通过javascript访问新用户还是回访者。有没有办法获取这些信息?
答案 0 :(得分:1)
使用此选项以使用Cookie ID跟踪用户:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id
基本上,此代码将为当前用户创建唯一的用户ID,然后您可以检查它是否已存储在localStorage中,以便检查用户过去是否访问过您的网站:
var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
if (window.localStorage) {
ga('create', 'UA-XXXXX-Y', {
'storage': 'none',
'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function(tracker) {
localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});
} else {
ga('create', 'UA-XXXXX-Y', 'auto');
}
要检查用户ID是否存储在localStorage中,请使用以下命令:
var GA_LOCAL_STORAGE_KEY = 'ga:clientId', userId = false;
if (window.localStorage) {
userId = localStorage.getItem(GA_LOCAL_STORAGE_KEY);
}
if (userId) {
// Old user
} else {
// New user
}