无法让Google Analytics(分析)在Rails项目上工作 [英] Cant get google analytics working on rails project

查看:53
本文介绍了无法让Google Analytics(分析)在Rails项目上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用Rails 5,并且一直在寻找一种通过TurboLink在我的应用程序中实现Google Analytics(分析)的方法.我访问的每个站点都有不同的说法,我经历了很多SO问题,但似乎没有一个可行或过时.

Currently I'm using rails 5 and have been looking for a method of implementing google analytics in my application with turbolinks. Every site I go to says something different, and I've gone through loads of SO questions and none of them seem to work or are outdated.

以下是我尝试过的方法的简短列表:

Here's a shorter list of the methods I've tried:

https://medium.com/weareevermore/how-to-add-google-analytics-tracking-that-works-with-turbolinks-c5023610846d

http://nithinbekal.com/posts/turbolinks-google-analytics/

使用Google Analytics(分析)设置4条Turbolink

http://railsapps.github.io/rails-google-analytics.html

我真的以为最后一个是最有前途的\ _(ツ)_/

I really thought that last one was gonna be most promising ¯\_(ツ)_/¯

所以我的问题是将Google Analytics(分析)集成到Rails应用程序中的最新方法是什么?

So my question is what is the most updated method of integrating google analytics into a rails application?

推荐答案

在使用Rails 5和Turbolink之前,我遇到了一个问题,并遇到了这个

I ran into an issue before with Rails 5 and Turbolinks and came across this issue for help.

您似乎正在使用机架跟踪器(基于 ekremkaraca 的评论).如果您不想使用依赖项,则可以使用部分代码和javascript代码段以最少的代码来设置Google Analytics(分析).我以这种方式配置了一些应用程序:

It looks like you have things working with rack-tracker (based on ekremkaraca's comment). If you don't want to use a dependency you can set up Google Analytics with fairly minimal code using a partial and javascript snippet. I have a few apps configured this way:

app/assets/javascripts/google_analytics.js

document.addEventListener('turbolinks:load', function(event) {
  if (typeof ga === 'function') {
    ga('set', 'location', event.data.url);
    ga('send', 'pageview');
  }
});

app/views/layouts/_ga.html.haml (在此示例中,使用 haml 而不是 erb )

app/views/layouts/_ga.html.haml (Using haml and not erb in this example)

:javascript
  (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', 'YOUR_GA_CODE_HERE');

app/views/layouts/application.html.haml (在 head 中包含 _ga 部分)

!!!
%html
  %head
    = stylesheet_link_tag    'application', media: 'all'
    = javascript_include_tag 'application'

    = render 'layouts/ga'
  %body
    = yield

app/assets/javascripts/application.js

...
//= require google_analytics

这篇关于无法让Google Analytics(分析)在Rails项目上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆