2015-04-27 139 views
1

我複製Google Analytics(分析)代碼,並且收到此錯誤:Uncaught SyntaxError: Unexpected token)Google Analytics(分析)「Unexpected token」)

這裏是代碼:

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

     ga('create', 'UA-XXXXXXXX-X', 'auto'); 
     ga('send', 'pageview'); 
    </script> 

錯誤是在這條線:i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

怎麼辦?謝謝!

回答

8

您錯過了該行的左括號。請嘗試以下操作:

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

    ga('create', 'UA-XXXXXXXX-X', 'auto'); 
    ga('send', 'pageview'); 
</script> 
+0

哦,謝謝你,我怎麼能錯過。 –

0

如果您正在使用Smarty模板引擎,並得到Uncaught SyntaxError: Unexpected token ,使用{literal}標籤:

<script> 
    {literal} 
     (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','//www.google-analytics.com/analytics.js','ga'); 

     ga('create', 'UA-XXXXXXXX-X', 'auto'); 
     ga('send', 'pageview'); 
    {/literal} 
</script> 
相關問題