theme: Move tracking to turbo:load

This commit is contained in:
Bjørn Erik Pedersen
2025-11-04 22:26:15 +01:00
parent 88b451022d
commit c369e534d2
+6 -4
View File
@@ -48,6 +48,7 @@ var debug = 0 ? console.log.bind(console, '[index]') : function () {};
{
let containerScrollTops = {};
let turboLoaded = false;
// To preserve scroll position in scrolling elements on navigation add data-turbo-preserve-scroll-container="somename" to the scrolling container.
addEventListener('turbo:click', () => {
@@ -56,14 +57,15 @@ var debug = 0 ? console.log.bind(console, '[index]') : function () {};
});
});
addEventListener('turbo:render', () => {
if (document.documentElement.hasAttribute('data-turbo-preview')) {
addEventListener('turbo:load', () => {
if (turboLoaded) {
// Minimal Analytics does not support SPA out of the box, so we need to manually track page views on Turbo navigation.
// Render may be called twice, once for preview and once for the real render.
// Track only on the real render.
window.track('page_view');
}
turboLoaded = true;
});
addEventListener('turbo:render', () => {
document.querySelectorAll('[data-turbo-preserve-scroll-container]').forEach((ele) => {
const containerScrollTop = containerScrollTops[ele.dataset.turboPreserveScrollContainer];
if (containerScrollTop) {