javascript - 生成空白页面的 Google Universal Analytics 跟踪代码

标签 javascript php google-analytics smarty universal-analytics

这是我第一次使用新版 Google Analytics(Universal Analytics),所以当我将以下代码直接放在 </head> 之前时,我的所有 URL 都会出现空白页。

这是谷歌给出的代码:

<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-1234567-89', 'mysite.com');
  ga('send', 'pageview');

</script>

有人知道为什么会这样吗?好像是个bug,但我不知道怎么解决。

最佳答案

根据@Brad 的评论,我检测到 PHP Smarty 因 Universal Analytics 代码跟踪而崩溃,所以我找到了这篇文章:

Use {Literal} Smarty Tag to Add Google Analytics Tracking Code to BlogSome Hosted Blogs

然后我按照下面的操作进行操作,跟踪代码完美运行!

<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-1234567-89', 'mysite.com');
    ga('send', 'pageview');

    {/literal}

</script>

关于javascript - 生成空白页面的 Google Universal Analytics 跟踪代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19502183/

相关文章:

javascript - 声明式与编程式 GraphQL

google-analytics - Google Analytics(分析)似乎无法实现结果

javascript - 确定 Google Analytics 是否已加载

php - 如何使用 PHP OPCache?

javascript - 当新 key 位于 json 中时,PHP ajax 添加新的 div

google-analytics - 如何使用计算字段拆分 Google Data Studio 中的事件值

javascript - 使用 select1 上的选定值填充 select2,无需刷新页面或使用按钮

javascript - 调用 globalization.getPreferredLanguage 时获取所有可用语言值的列表

javascript - 使用 Dropzone.js 添加文件之前是否可以有弹出选项?

php - 我在将 PHP 中的数组转为 JSON 结构用于 Javascript 时遇到问题