我正在尝试用rails 5实现谷歌分析,因为我想保留我的引擎,所以我遵循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()这不管用。我试着用Google analytics debugger.but调试它
_gaq.push processing "_setAccount" for args: "[UA-XXXXXXX-XX]": 这个在控制台里。当我试着直接打电话
GoogleAnalytics.trackPageview()我在我的分析帐户里得到数据。但把这个扔出去
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.我的控制台出错了。有什么问题吗?请帮帮忙。
发布于 2017-09-20 06:36:23
您所指的这篇文章相当古老(在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 %>将该文件放入您的资产/javascripts/文件夹中,并确保通过检查或编辑您的资产/javascripts/application.js文件来加载该文件。
document.addEventListener 'turbolinks:load', (event) ->
if typeof ga is 'function'
ga('set', 'location', event.data.url)
ga('send', 'pageview')发布于 2020-05-22 11:39:31
我用Turbolinks和Webpacker在Rails 6上尝试了下面的内容,它可以工作,我希望它能帮助到别人。
将此添加到布局文件的头部:
<script async src="https://www.googletagmanager.com/gtag/js?id=YY-XXXXXXXXX-Z"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
</script>然后将其添加到您的application.js文件中:
$(document).on("turbolinks:load", function() {
gtag('config', 'YY-XXXXXXXXX-Z', {'page_location': event.data.url});
})用您自己的标记替换YY-XXXXXXXXX-Z,您就完成了。
https://stackoverflow.com/questions/46314712
复制相似问题