0

我有以下脚本,但是变量“ClientID”不知何故丢失(未定义)。我想知道这是否是因为“window.load”语句?我能做些什么来确保将 ClientID 变量传递给这个函数吗?

<script>
            //Google analytics include
            (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','https://www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-xxxxxx-x', 'auto');
            ga('send', 'pageview');

            //store clientId in ClientID variable
            var ClientID = ga(function(tracker) {
                return tracker.get('clientId');
            });

            //After whole DOM is loaded addEventListener and send ClientID
            //Not working, somehow ClientID gets lost...
             window.onload = function () { 
                var myl = document.querySelector('div.mylivechat_collapsed');
                myl.addEventListener('click', function() {
                   ga('send', 'event', 'contact', 'livechat' , ClientID);
                });
            }
</script>
4

2 回答 2

0

似乎 ClientId 没有丢失但从未定义

<script>
  //Google analytics include
            (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','https://www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-xxxxxx-x', 'auto');
            ga('send', 'pageview');

            //store clientId in ClientID variable
            var ClientID = ga(function(tracker) {
                return tracker.get('clientId');
            });
    
            console.log('1:',ClientID);

            //After whole DOM is loaded addEventListener and send ClientID
            //Not working, somehow ClientID gets lost...
             window.onload = function () { 
               console.log('onload');
                var myl = document.querySelector('div.mylivechat_collapsed');
                myl.addEventListener('click', function() {
                   console.log('CLICK:',ClientID);
                   ga('send', 'event', 'contact', 'livechat' , ClientID);
                });
            }
</script>
<div class="mylivechat_collapsed">[CLICK ME]</div>

于 2016-06-30T15:06:14.210 回答
0

根据本文档:https ://developers.google.com/analytics/devguides/collection/analyticsjs/command-queue-reference#create

ga()函数undefined返回。因此,ClientID将是undefined

我不确定您到底想要什么,但也许您的意思是:

        window.onload = function () { 
           console.log('onload');
            var myl = document.querySelector('div.mylivechat_collapsed');
            myl.addEventListener('click', function() {
               console.log('CLICK:',ClientID);
               ga('send', 'event', 'contact', 'livechat' , tracker.get('clientId'));
            });
        }

如果是这种情况,您不需要变量ClientID.

于 2016-06-30T15:02:15.967 回答