Sab*_*ina 1 javascript google-analytics ruby-on-rails ruby-on-rails-7
您好,有一个 Rails 7 应用程序,想要放置我的 google 分析标签 js 片段。
我尝试的只是将此脚本添加到 application.html.erb 中,这不起作用(可能与 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>
Run Code Online (Sandbox Code Playgroud)
小智 7
这个方法对我有用
在你网站的 html head 中
<script src="https://www.googletagmanager.com/gtag/js?id=G-111111111" data-turbo-track="reload" async></script>
Run Code Online (Sandbox Code Playgroud)
并在 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1993 次 |
| 最近记录: |