2016-06-30 38 views
0

我有以下脚本,但变量“ClientID”以某种方式丢失(未定义)。我想知道这是否是因为“window.load”声明?有什么我可以做的,以确保ClientID变量继承到这个函数?变量在使用window.onload时会丢失

<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> 
+0

https://developers.google.com/analytics/devguides/collection/analyticsjs/command -queue-reference#添加命令到队列 – Teemu

+0

嗨Teemu,不知道如何帮助文件帮助 - 无法找到任何东西,这种情况下。我认为,事情得到初始化的顺序有问题,并且变量会丢失。 –

+0

“用以下函数签名调用ga()命令队列函数会将命令推送到队列中......”您试图从异步执行的函数中获取值。 – Teemu

回答

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

0

看来,客户端Id不是失去从未定义

<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>