首页 > 解决方案 > 谷歌分析不适用于 MeteorJS 应用程序

问题描述

我正在按照https://github.com/reywood/meteor-iron-router-ga上的说明在MeteorJS 网络应用程序上实施 Google Analytics。

  1. 安装了 iron-router-ga 库
$ meteor add reywood:iron-router-ga
  1. 根据需要提供 Meteor 设置(在 Heroku 环境变量上):
{
    "public": {
        "ga": {
            "id": "UA-XXXX-Y"
        }
    }
}
  1. 找到route.js,添加如下代码:
Router.configure({
    trackPageView: true, //https://github.com/reywood/meteor-iron-router-ga
    ...
});

if (Meteor.isClient) {
    Router.plugin('reywood:iron-router-ga');
}

但没有任何效果。在 Google Analytics(分析)仪表板上完全静默。关于我应该如何调试的任何提示?目标是使用访问的路径/页面跟踪站点访问。

标签: meteorgoogle-analytics

解决方案


我从来没有使用过你提到的那个包,但天真的方法效果很好。

添加到您的头部index.html

  <script type="text/javascript" src="/js/google_analytics.js"></script>

public/js/google_analytics.js

(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-y', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');

一个问题可能是,当通过客户端路由到达这些页面时,这是否仍会跟踪页面浏览量,我认为答案是肯定的,但尚未验证。


推荐阅读