简体   繁体   English

Google Analytics(分析):读取正文中的clientId

[英]Google Analytics : read clientId inside body

I load the new Universal Analytics code in the head, and get the clientId 我将新的Universal Analytics代码加载到头部,并获取clientId

<script type="text/javascript">// <![CDATA[
    (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');

    ga('create', 'UA-12345678-00', 'example.com');
    ga('send', 'pageview');
    var clientId = '';
    ga(function (tracker) {
        clientId = tracker.get('clientId');
    });
// ]]></script>

In the head I also load a script to change the content of a link 在头部,我还加载了一个脚本来更改链接的内容

<script>$("a#test").attr('href', ($("a#test").attr('href') + (clientId)));</script>

Inside the body there is the link that should get changed: 体内有应该更改的链接:

<a id="test" href="http://example.com?client=">test</a>

Unfortunately the clientId value doesn't append to the link as it should, anyone can help me ? 不幸的是,clientId值没有按原样附加到链接上,任何人都可以帮助我吗?

Thanks! 谢谢!

Just wait for onload event of window object as GA use an ajax request, should work: 只需等待窗口对象的onload事件,因为GA使用ajax请求,应该可以正常工作:

$(window).on('load',function(){
    $("#test").attr('href', $("#test").attr('href') + (clientId));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM