2014-03-31 96 views
6

我花了相當多的時間研究如何在本地運行的文件上使用GA,但不使用http://localhost:(some_port)在沒有服務器或本地主機的情況下使用Google Analytics?

我使用的每種方法都不會返回任何常規數據。這是最接近我得到的東西:

<script type="text/javascript"> 
      var _gaq = _gaq || []; 
      _gaq.push(['_setAccount', 'UA-47519364-1']); 
      _gaq.push(['_setDomainName', 'none']); 
      _gaq.push(['_setAllowLinker', 'true']); 
      _gaq.push(['_trackPageview']); 

      (function() { 
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga_debug.js'; 
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
      })(); 
     </script> 

任何人都有這種工作在這種方式之前?

+0

我在看同樣的問題,我想答案是「你不能這樣做」,雖然我不積極。但是,當使用file:// URL在本地查看文件時,您肯定可以使用競爭服務,例如Keen.io。 – Harlan

回答

6

感謝您使用'_debug.js'腳本來啓發我的眼睛。 我正在使用分析模塊並出現同樣的問題。 所以我用調試腳本,並得到了試圖發送一個事件時,出現以下錯誤:

Unallowed document protocol. Aborting hit.

我用Google搜索了一下,發現這個線程exlaining如何繞過它: https://productforums.google.com/forum/#!topic/analytics/KNz8TimivXo

這是回答:

ga('create', 'UA-**********-6', {'cookieDomain': 'none'}); 
ga('set', 'checkProtocolTask', function(){ /* nothing */ }); 
ga('send', 'pageview'); 
+0

爲了在Chrome擴展中使用Google Analytics,還需要此解決方法。 – jdunning

+0

此外,您可以將'checkProtocolTask​​'設置爲'null'而不是使用noop函數。 – jdunning

相關問題