Rails中不乏“涵盖谷歌分析”的“教程”,但我找不到实际完成的教程。有人可以告诉我,如果我已经结合了正确的信息,并有我需要建立遗传算法,如果没有,我做错了什么?谢谢。这是在rails 5应用程序中设置Google Analytics的正确方法吗?
我制作了一个名为google_analytics.js.coffee的文件,它位于assets/javascript文件夹中。它包含了(从http://reed.github.io/turbolinks-compatibility/google_analytics.html):
class @GoogleAnalytics
@load: ->
((i, s, o, g, r, a, m) ->
i['GoogleAnalyticsObject'] = r
i[r] = i[r] or ->
(i[r].q = i[r].q or []).push arguments
return
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
return
) window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'
ga 'create', GoogleAnalytics.analyticsId(), 'auto'
# If Turbolinks is supported, set up a callback to track pageviews on page:change.
# If it isn't supported, just track the pageview now.
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
document.addEventListener "page:change", (->
GoogleAnalytics.trackPageview()
), true
else
GoogleAnalytics.trackPageview()
@trackPageview: (url) ->
unless GoogleAnalytics.isLocalRequest()
ga 'send',
hitType: 'pageview'
page: location.pathname
@isLocalRequest: ->
GoogleAnalytics.documentDomainIncludes "local"
@documentDomainIncludes: (str) ->
document.domain.indexOf(str) isnt -1
@analyticsId: ->
# your google analytics ID(s) here...
'UA-MYIDHERE-X'
GoogleAnalytics.load()
然后,因为从来没有人谈论这是否是所有的需要,我用这个代码从这里(https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20),并把它放在application.html.erb的头,尽管那个人正在将论坛中的对话代码拼凑在一起。他的代码还说要在google_analytics.js.coffee中添加四行代码。那是另一种方法吗?劣质的?它是否需要顶级的@GoogleAnalytics类?对老兵来说显而易见的东西对新兵来说并不明显!反正这里是我把我的应用程序布局标题:
<% if Rails.env.production? %>
<script type="text/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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-MYIDHERE-X', 'auto');
</script>
现在我有一个代码和上面的代码,所以G_A.js.coffee页是我准备请按照下列步骤部署到Heroku的(来自这里:http://railsapps.github.io/rails-google-analytics.html)?
$ git add -A
$ git commit -m "analytics"
$ RAILS_ENV=production rake assets:precompile
$ git add -A
$ git commit -m "assets compiled for Heroku"
非常感谢任何人从一开始就开始正确的教程,不要做出假设,并贯彻到底。
为什么不直接复制你从谷歌获得的分析并将其粘贴到布局文件中。这就是我的做法,它工作得很好。 –
并且您正在使用通过heroku部署的rails 5,并且完全没有其他功能? – Nick
我正在使用Rails 4并部署在Heroku上。我曾经写过博客,当你写博客时,你所要做的就是将你的Google Analytics(分析)代码放入布局中的div或其他内容中。 Google使用JavaScript在用户访问的每个页面上运行,就是这样。没有什么太复杂。 –