'google analytics tag not working in rails 7 app
Hi have a rails 7 application and want to place my google analytics tag js snippet.
What I tried was just adding this script to the application.html.erb, which does not work (probably related to hotwire / turbo)?:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-1234556789"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-123456789');
</script>
Solution 1:[1]
This method worked for me
In your site html head
<script src="https://www.googletagmanager.com/gtag/js?id=G-111111111" data-turbo-track="reload" async></script>
and in the application.js
document.addEventListener("turbo:load", function(event) {
window.dataLayer = window.dataLayer || []
function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
gtag('config', 'G-111111111', {'page_location': event.detail.url})
}, false)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ferit Ă–ztosun |
