0

这是我尝试让 Google Analytics 与 Iron Router 一起使用

lib/analytics.js我有以下代码(从分析中的代码页粘贴):

if (Meteor.isClient) {
    (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-45905917-2', 'ec2-54-246-15-224.eu-west-1.compute.amazonaws.com');
}

然后,inclient/router.coffee是以下代码:

Router.configure layoutTemplate: 'layout'

Router.map ->
    @route 'home', 
        path: '/'
        template: 'home'
        after: -> ga('send', 'pageview')

    for link in Pages.links
        route =  
            path: link.url
            template: link.toTemplate 
            after: -> ga('send', 'pageview')
        @route link.toTemplate, route

但这似乎没有奏效。我该如何解决这个问题?

编辑添加:

我尝试过 GAnalytics,但我的部署设置和 Meteor 设置有很多问题

4

3 回答 3

1

我们解决这个问题的方法是调用页面函数 onAfterAction

Router.onAfterAction(function() {
  analytics.page(this.route.getName());
});

在使用 Analytics.js 项目时,一个有用的提示是,如果您analytics.debug()在控制台中调用,您将能够看到您的代码在做什么。

我们已经在一些流星项目中使用了 Analytics.js,因此我们将其提取到流星包中:

okgrow:分析

我们在大多数项目中使用 Iron Router,所以如果安装了它,这个包会根据路由名称自动发送页面视图(但如果项目不使用 Iron Router 也不会报错)。如果您安装了 Accounts 软件包,它还会自动记录用户登录/注销,当然,您可以设置track()事件调用。

希望这可以帮助!

于 2015-02-02T19:53:42.530 回答
1

渲染模板时执行那些代码,看看template_rendered

于 2014-01-08T17:02:59.790 回答
0

在我看来,这个选项是使用 Template.rendered。您可能面临的一个问题是您需要了解模板数据更改。例子:

Template.yourTemplate.rendered = function(){
  var self = this; //In case you need it but self.data IS NOT REACTIVE

  this.autorun(function(){
    //This is reactive
    var data = Template.currentData();

    //place your analitycs code here

  });
}

我建议在模板容器上使用它。

于 2014-11-14T18:41:08.110 回答