使用window.onload时变量会丢失

时间:2016-06-30 14:44:40

标签: javascript variables google-analytics window.onunload

我有以下脚本,但变量“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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>