谷歌分析API的多个实验

时间:2014-07-16 09:01:47

标签: javascript google-analytics google-experiments

我们使用analytics api下载实验和变体,并为我们的访问者进行变体选择(即此处所述的服务器端实验:https://developers.google.com/analytics/solutions/experiments-server-side

当访问者访问正在进行实验的网址并且他们有选定的变体时,他们会获得如下所述的javascript:

<script>
cxApi.setChosenVariation(1, 'a9BcDEFgHijKl8mON-Opqw');
</script>

这很好用。我们希望运行多个实验(例如,涉及菜单的站点范围实验和特定于页面的实验),变体选择以及我们最终的一切正常。对于用户来说,当他们参与多个实验时,他们会多次调用setChosenVariation:

<script>
cxApi.setChosenVariation(1, 'a1BcDEFgHijKl2mON-3pqr');
cxApi.setChosenVariation(1, 'z9YxWVVuTsrPl8oNM-7lkj');
</script>

我找不到任何理由不应该起作用,但在实验结果中,当发生这种情况时,我们会看到所有用户只被分配到一个实验,尽管两个实验都有结果(创建转换率) > 100%)。

conversion rate > 100%

是否有对此行为的解释(我觉得第二次调用可能会覆盖第一次调用?)和/或正确的方法来执行此操作?

非常感谢

3 个答案:

答案 0 :(得分:6)

轻松回答并不适合我。感谢Google Analytics Debugger扩展,我可以看到ga('send','pageview')这两个人都在发送有关第二次实验的数据。使用synchronous call工作,我最终得到了类似的东西:

var sendExperiment = function(tracker, experimentVar, experimentId) {
  cxApi.setChosenVariation(experimentVar, experimentId);
  tracker.send('event', 'experiment', 'view',{'nonInteraction': 1});
}

ga(function(tracker) {
 sendExperiment(tracker, 1, 'a1BcDEFgHijKl2mON-3pqr');
 sendExperiment(tracker, 2, 'z9YxWVVuTsrPl8oNM-7lkj');
});

答案 1 :(得分:1)

我认为您应该在每个实验下设置选定的变体时将实验值发送到Google Analytics。 代码像这样:

cxApi.setChosenVariation(1, 'a1BcDEFgHijKl2mON-3pqr'); 
ga('send', 'pageview');
cxApi.setChosenVariation(1, 'z9YxWVVuTsrPl8oNM-7lkj'); 
ga('send', 'pageview');

答案 2 :(得分:0)

我意识到这是一个古老的问题,但我最近也不得不想出一个解决这个问题的方法,并且我认为我会分享我的方法,并采用一些现实世界的约束措施。

在高级别,我的方法以下列方式工作:

  1. 加载Google内容实验API代码
  2. 设置Analytics并创建第二个跟踪器(针对相同的配置文件),该跟踪器仅用于跟踪我正在运行的第二个并发实验。
  3. 定制常规GA代码。
  4. 创建一个函数来检查用户当前所在的页面是否是我正在运行给定测试的页面,如果是,请找到该特定实验的用户所选的变体,执行它并将信息发送回具有自定义事件的相关GA跟踪器。
  5. 不幸的是,我不得不采用这种方法作为我正在使用的平台,只允许我编辑全局标题而不是任何类型的页面特定基础。因此,正如我所提到的,它只是针对我正在运行测试的URL检查规范URL标记。

    <!-- Load the regular Content Experiments JS API without any ID parameters -->
    <script src="//www.google-analytics.com/cx/api.js"></script>
    
    <!-- Setup Google Analytics -->
    <script>
      (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');
    
      // Create the first regular tracker like you normaly would
      ga('create', 'UA-XXXXXXXX-1', 'auto', {'allowLinker': true, 'siteSpeedSampleRate': 90});
      // Create the second named tracker just for experiments
      ga('create', 'UA-XXXXXXXX-1', 'auto', 'experimentTracker');
    
      //Setup all of the regular customizations for the regular tracker if you need to
      ga('require', 'linker');
      ga('require', 'linkid', 'linkid.js');
      ga('require', 'ec');
      ga('require', 'displayfeatures');
      ga('linker:autoLink', ['example.com','domain2.com'], false, true);
    
      // Send the pageview like normal for the regular tracker
      ga('send', 'pageview');
    </script>
    
    <script>
      // Define the different experiments you wish to run and the page
    	var experimentOneID = "a1BcDEFgHijKl2mON-3pqr";
    	var	experimentTwoID = "z9YxWVVuTsrPl8oNM-7lkj";
    	var experimentOneURL = "http://www.example.com/experiment-1/";
    	var experimentTwoURL = "http://www.example.com/experiment-2/";
    
      var runContentExperiment = function(experimentID) {
        // Ask Google Analytics which variation to show the user and specify the experiment ID.
        var chosenVariation = cxApi.chooseVariation(experimentID);
        // Set the chosen variation for GA
        cxApi.setChosenVariation(chosenVariation, experimentID);
    
        // Here is where we have the page specific code changes you might want to make
        if (experimentID === experimentOneID) {
          var pageVariations = [
            function() {},  // Original: Do nothing. This will render the default HTML.
            function() {    // Variation 1 of Experiment 1
              // Do Something here in experiment 1
            }
          ];
          pageVariations[chosenVariation]
          ga('send', 'event', 'Content Experiment', 'View', experimentID, { 'nonInteraction': 1 });
        }
        else if (experimentID === experimentTwoID) {
          var pageVariations = [
            function() {},  // Original: Do nothing. This will render the default HTML.
            function() {    // Variation 1 of Experiment 2
              // Do Something here in experiment 2
            },
            function() { // Variation 2 of Experiment 2
    
            }
          ];
          pageVariations[chosenVariation]
          ga('experimentTracker.send', 'event', 'Content Experiment', 'View', experimentID, { 'nonInteraction': 1 });
        }
      }
    
      // Check the canonical URL of the page and make sure it matches the one we want
      var canonical = document.querySelector("link[rel='canonical']").href;
      if (canonical === experimentOneURL) {
        $(function() {
          runContentExperiment(experimentOneID);
        });
      } 
      else if (canonical === experimentTwoURL) {
        $(function() {
          runContentExperiment(experimentTwoID);
        });
      } 
    </script>