I am trying to implement Google analytics with rails 5. Since i wanted to keep my turbolinks, i am following http://railsapps.github.io/rails-google-analytics.html this method. Added 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()
This doesnt work. I tried to debug it using Google analytics debugger.but its only showing
_gaq.push processing "_setAccount" for args: "[UA-XXXXXXX-XX]":
this in the console. When i tried calling directly
GoogleAnalytics.trackPageview()
I am getting data in my analytics account. But throwing this
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.
error in my console. What is the issue? Please help.
The article you are referring to is quite old (in Rails Land). Did you try
https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20
?
In 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 %>
Put this file into your assets/javascripts/ folder and assure it gets loaded by checking or editing your assets/javascripts/application.js file as appropriate
document.addEventListener 'turbolinks:load', (event) ->
if typeof ga is 'function'
ga('set', 'location', event.data.url)
ga('send', 'pageview')
I tried the following on Rails 6 with Turbolinks and Webpacker and it works, I hope it helps someone.
Add this to the layout file's head:
<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>
And then add this to your application.js file:
$(document).on("turbolinks:load", function() {
gtag('config', 'YY-XXXXXXXXX-Z', {'page_location': event.data.url});
})
Replace YY-XXXXXXXXX-Z
with your own tag and you are done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.