sha*_*mza 1 javascript ruby google-analytics ruby-on-rails
我正在尝试使用rails 5实现Google分析.由于我想保留我的turbolinks,我正在关注http://railsapps.github.io/rails-google-analytics.html此方法.添加了app/assets/javascripts/google_analytics.js.coffee:
class @GoogleAnalytics
@load: ->
# Google Analytics depends on a global _gaq array. window is the global
scope.
window._gaq = []
window._gaq.push ["_setAccount", GoogleAnalytics.analyticsId()]
# Create a script element and insert it in the DOM
ga = document.createElement("script")
ga.type = "text/javascript"
ga.async = true
ga.src = ((if "https:" is document.location.protocol then
"https://ssl" else "http://www")) + ".google-analytics.com/ga.js"
firstScript = document.getElementsByTagName("script")[0]
firstScript.parentNode.insertBefore ga, firstScript
# 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()
if url
window._gaq.push ["_trackPageview", url]
else
window._gaq.push ["_trackPageview"]
window._gaq.push ["_trackPageLoadTime"]
@isLocalRequest: ->
GoogleAnalytics.documentDomainIncludes "local"
@documentDomainIncludes: (str) ->
document.domain.indexOf(str) isnt -1
@analyticsId: ->
# your google analytics ID(s) here...
'UA-XXXXXXX-XX'
GoogleAnalytics.load()
Run Code Online (Sandbox Code Playgroud)
这不起作用.我试图使用谷歌分析调试器调试它.但它只显示
_gaq.push processing "_setAccount" for args: "[UA-XXXXXXX-XX]":
Run Code Online (Sandbox Code Playgroud)
这在控制台中.当我试着直接打电话
GoogleAnalytics.trackPageview()
Run Code Online (Sandbox Code Playgroud)
我在分析帐户中获取数据.但抛出这个
Method _trackPageLoadTime is deprecated. _trackPageLoadTime is deprecated. Site Speed tracking is enabled by default with trackPageview call at 1% sampling. Use _setSiteSpeedSampleRate for changing sample rate.
Run Code Online (Sandbox Code Playgroud)
我的控制台出错了.有什么问题?请帮忙.
你所指的文章相当陈旧(在Rails Land中).你试过了吗
https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20
?
在application.html.erb中:
<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-XXXXXXXX-X', 'auto');
</script>
<% end %>
Run Code Online (Sandbox Code Playgroud)
将此文件放入您的assets/javascripts /文件夹中,并确保通过检查或编辑您的assets/javascripts/application.js文件来加载它
document.addEventListener 'turbolinks:load', (event) ->
if typeof ga is 'function'
ga('set', 'location', event.data.url)
ga('send', 'pageview')
Run Code Online (Sandbox Code Playgroud)