【问题标题】:variable gets lost when using window.onload使用 window.onload 时变量会丢失
【发布时间】:2016-06-30 14:44:39
【问题描述】:

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

【问题讨论】:

  • 您好 Teemu,不确定帮助文件如何提供帮助 - 找不到任何适合这种情况的信息。我认为初始化的顺序有问题,并且变量丢失了。
  • "使用以下函数签名调用 ga() 命令队列函数会将命令推送到队列中......" 您正在尝试从异步执行的函数中获取值。
  • 你检查ClientID的赋值了吗?我猜这不是你想的那样。
  • 是的,我明白你在说什么。现在有没有办法获取这个值并将其作为事件传递?

标签: javascript variables google-analytics window.onunload


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    根据本文档: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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2018-05-12
      • 2011-12-05
      • 2011-03-09
      • 2012-10-18
      • 2012-12-22
      相关资源
      最近更新 更多