首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >谷歌实验API在analytics.js中可用吗?

谷歌实验API在analytics.js中可用吗?
EN

Stack Overflow用户
提问于 2016-11-08 13:24:59
回答 1查看 187关注 0票数 1

我们尝试设置google实验,以便与后端设置一起工作,并发现有API可以让GA知道我们通过函数为用户显示了哪些变化:

代码语言:javascript
复制
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中,而我们不需要担心使用这个函数?

EN

回答 1

Stack Overflow用户

发布于 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。

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40488101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档