我按照指示实现了hotjar(即将下面的代码复制粘贴到页面<head>
中(,但hotjar javascript只出现在访问网站的第一个页面上,因为它在<head>
中,这意味着它只在涡轮链接打开时加载一次。
有没有办法让涡轮链接保持打开状态,但像预期的那样拥有hotjar脚本功能?
我可以看到一些讨论,但没有明确的分辨率
供参考:
<script>
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:2019490,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
</script>
我相信你可以做这样的
<script>
document.addEventListener("turbolinks:load", function(event) {
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:2019490,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
})
</script>
在每次页面加载时触发代码
使用更新的"涡轮;图书馆
强制注入的脚本标记具有数据涡轮轨道="0";重新加载";属性似乎有效:
<script>
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:123456,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
// this seems to do the trick
r.setAttribute("data-turbo-track","reload")
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
</script>
资产发生变化时重新加载
Turbo Drive可以跟踪从一个页面到下一个页面的资产元素的URL,并在它们发生更改时自动发出完全重新加载。这样可以确保用户始终拥有应用程序脚本和样式的最新版本。
用数据turbo track=";重新加载";并在您的资产URL中包含版本标识符。标识符可以是一个数字、上次修改的时间戳,或者更好的是资产内容的摘要,如以下示例所示。
https://turbo.hotwired.dev/handbook/drive
在脚本hj:r.setAttribute("data-turbolinks-track","reload");
上添加此属性
<script>
(function(h,o,t,j,a,r){
h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
h._hjSettings={hjid:123123,hjsv:6};
a=o.getElementsByTagName('head')[0];
r=o.createElement('script');r.async=1;
r.setAttribute("data-turbolinks-track","reload"); // this is bypass reload turbolink
r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
a.appendChild(r);
})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');
</script>