简体   繁体   English

使用铁路由器的Meteor中的Google Analytics

[英]Google Analytics in Meteor with Iron Router

Here is my attempt to get Google Analytics working with Iron Router 我试图让Google Analytics与Iron Router配合使用

In lib/analytics.js I have the following code (pasted from the code page in analytics): 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');
}

Then, in client/router.coffee is the following code: 然后,在client/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

But this did not seem to have worked. 但这似乎没有奏效。 How do I solve this? 我该如何解决这个问题?

EDIT TO ADD: 编辑添加:

I have tried GAnalytics, but I had a lot of issues with my deployment setup and Meteor settings 我尝试过GAnalytics,但是我的部署设置和Meteor设置存在很多问题

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

The way we solve for this is calling the page function onAfterAction 我们解决这个问题的方法是在onAfterAction上调用页面函数

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

A helpful hint, when using the Analytics.js project, is if you call analytics.debug() in your console you'll be able to see what your code is doing. 使用Analytics.js项目时,一个有用的提示是,如果您在控制台中调用analytics.debug() ,您将能够看到代码正在执行的操作。

We've used Analytics.js on a few meteor projects already so we extracted it into a meteor package: 我们已经在几个流星项目中使用了Analytics.js,因此我们将其解压缩为流星包:

okgrow:analytics okgrow:分析

We use Iron Router on most projects, so if it's installed, this package will automatically send page views based on route names (but doesn't complain if the project doesn't use Iron Router). 我们在大多数项目中使用Iron Router,因此如果已经安装,该软件包将根据路由名称自动发送页面视图(但如果项目不使用Iron Router,则不会抱怨)。 It also automatically logs user login/logout if you have the Accounts package installed and, of course, you can setup track() calls on events. 如果您安装了Accounts包,它还会自动记录用户登录/注销,当然,您可以对事件设置track()调用。

Hope this helps! 希望这可以帮助!

The option, in my opinion, is to use Template.rendered. 在我看来,该选项是使用Template.rendered。 One issue that you might face is that you need to be aware of Template data changes. 您可能面临的一个问题是您需要了解模板数据更改。 Example: 例:

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

  });
}

I recommend using it on template containers. 我建议在模板容器上使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM