我想用以下Skript实现我自己的Test-Client ID:
<script>
(function(){
var cookie = readCookie("customClientID");
var storage = window.localStorage;
var storageClientID = storage.getItem("customClientID"");
var userID = userId();
var agent = window.navigator.userAgent;
if(!cookie && !storageClientID){
createCookie("customClientID"", userID, 2);
storage.setItem("customClientID"", readCookie("customClientID""));
}else if(cookie && !storageClientID){
storage.setItem("customClientID"", readCookie("customClientID""));
}else if(!cookie && storageClientID){
createCookie("customClientID"", storageUserId, 2);
}else{}
//Create UUID
function userId() {
try{
return new Date().getTime() + '.' + Math.random().toString(36).substring(5) + '.' + hash(agent);
}catch(e){}
}
//Hash Function
function hash(str){
try{
var hash = 0;
for(var i = 0; i < str.length; i++){
var charI = str.charCodeAt(i);
hash = ((hash<<5)-hash)+charI;
hash = hash & hash; // Convert to 32bit integer
}
hash = hash & hash;
hash = hash.toString().replace("-", "");
return hash;
}catch(e){}
}
//Read Cookie https://www.quirksmode.org/
function readCookie(name) {
try{
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}catch(e){}
}
//Create Cookie https://www.quirksmode.org/
function createCookie(name,value,days) {
try{
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}catch(e){}
}
})();
if块检查cookie是否已设置且cookie值是否也出现在本地存储中。如果未找到cookie,则使用值生成函数userId()设置新cookie。
要从cookie中获取customClientID值,我使用内部GTM First Party Cookie变量。上面脚本的自定义HTML标记在All Pages - Pageview上触发。
最后,我在每个标记中设置了第一方Cookie变量(customClientID)值的自定义维度(用户范围)。
当我比较原始GA ClientID的用户指标时,我为每个ClientID获得1个用户,这是正确的。但是,如果我将自定义报告中的customClientID与相同的日期范围进行比较,例如有一天,我得到了很多具有相同customClientID的用户。与原始ClientID相比,总数也更少。
我们有一个高流量的网站,所以我的第一个假设是脚本可能会减慢以处理大量用户同时进入和浏览。许多用户将获得相同的customClientID。
第二个假设是自定义维度应仅在页面视图标记内设置。
希望您了解我的问题,并提前多多感谢!
PS:我也在这里发布了https://productforums.google.com/forum/#!topic/tag-manager/syet129zIqI;context-place=forum/tag-manager。
最佳, 安东