admin管理员组文章数量:1426472
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=";></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-123456789');
</script>
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./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>
Share
Improve this question
edited Jan 15, 2022 at 6:24
Linda Lawton - DaImTo
118k39 gold badges225 silver badges501 bronze badges
asked Jan 14, 2022 at 13:17
SabrinaSabrina
3694 silver badges14 bronze badges
4
- If you inspect the view do you see the js in there? Just want to verify it is on the page but not firing. – Rockwell Rice Commented Jan 14, 2022 at 14:00
- when I inspect its not there. – Sabrina Commented Jan 15, 2022 at 18:40
-
ok so you added those lines to
application.html.erb
but it is not showing? And it doesn't work if you just refresh the page, or do a hard reload of the page? Since this is a google tracking code I just want to make sure, you did deploy the changed code to the site, correct? – Rockwell Rice Commented Jan 15, 2022 at 20:28 - I added it to application.html.erb, thats correct! The thing is that Rails 7 uses turbo which is why I think it does not work with inline js but thats just a guess. – Sabrina Commented Jan 16, 2022 at 16:15
1 Answer
Reset to default 8This method worked for me
In your site html head
<script src="https://www.googletagmanager./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)
本文标签: javascriptgoogle analytics tag not working in rails 7 appStack Overflow
版权声明:本文标题:javascript - google analytics tag not working in rails 7 app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745381885a2656202.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论