我们尝试设置google实验,以便与后端设置一起工作,并发现有API可以让GA知道我们通过函数为用户显示了哪些变化:
cxApi.setChosenVariation(chosenVariation, opt_experimentId);
当我访问官方文档:https://developers.google.com/analytics/devguides/collection/gajs/experiments时,它说:"ga.js是一个遗留库。如果您要启动一个新的实现,我们建议您使用这个库的最新版本analytics.js。对于存在的实现,请学习如何从ga.js迁移到analytics.js。“
我们确实使用analytics.js。
这是否意味着ga.js中的所有函数都在analytics.js中,而我们不需要担心使用这个函数?
发布于 2018-03-16 15:24:24
您可以尝试只使用浏览器的实现,如在https://developers.google.com/analytics/devguides/collection/analyticsjs/experiments中解释的那样。而不是ga.js或analytics.js,您可以尝试gtag.js。访问https://developers.google.com/analytics/devguides/collection/gtagjs/migration,了解如何从analytics.js迁移到gtag.js。下面我将分享我如何实现我的实验的代码。记住,要在Google中创建该实验,您将在BEHAVIOR > Experiments部分找到它。在下面的代码中,您需要使用自己的实验ID。
<head>
.......................................
.......................................
.......................................
<!-- Load the Content Experiment JavaScript API client for the experiment -->
<script src="//www.google-analytics.com/cx/api.js?experiment=MY_EXPERIMENT_ID"></script>
<script>
// Ask Google Analytics which variation to show the user.
var chosenVariation = cxApi.chooseVariation();
// Define JavaScript for each page variation of this experiment.
// Wait for the DOM to load, then execute the view for the chosen variation.
$(document).ready(function(){
switch (chosenVariation) {
case 0:
// Original: Do nothing. This will render the default HTML.
break;
case 1:
//document.getElementsByClassName('logo_tagline')[0].value = 'I love programming';
$(".logo_tagline:first").text("I love programming");
break;
case 2:
//document.getElementsByClassName('logo_tagline')[0].value = 'Programming is my passion';
$(".logo_tagline:first").text("Programming is my passion");
break;
case 3:
//document.getElementsByClassName('logo_tagline')[0].value = 'I enjoy writing code';
$(".logo_tagline:first").text("I enjoy writing code");
}
});
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
</head>https://stackoverflow.com/questions/40488101
复制相似问题