Skip to content

Commit b079d78

Browse files
committed
Defer GA
1 parent 59b810d commit b079d78

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

html/base.html

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,27 @@
248248
window.dataLayer = window.dataLayer || [];
249249
function gtag(){dataLayer.push(arguments);}
250250

251-
// Defer GA configuration until after page loads to avoid blocking main thread
252-
window.addEventListener('load', function() {
253-
gtag('js', new Date());
254-
gtag('config', 'G-Z7XL0RM921');
251+
// Defer GA configuration with bounce tracking
252+
let gaLoaded = false;
253+
function loadGA() {
254+
if (!gaLoaded) {
255+
gtag('js', new Date());
256+
gtag('config', 'G-Z7XL0RM921');
257+
gaLoaded = true;
258+
}
259+
}
260+
261+
// Load GA on first user interaction
262+
['mousedown', 'touchstart', 'keydown', 'scroll'].forEach(event => {
263+
document.addEventListener(event, loadGA, { once: true, passive: true });
255264
});
265+
266+
// Track bounces: load GA when user is about to leave
267+
window.addEventListener('beforeunload', loadGA, { once: true });
268+
document.addEventListener('visibilitychange', loadGA, { once: true });
269+
270+
// Fallback: load after 1.5 seconds
271+
setTimeout(loadGA, 1500);
256272
</script>
257273
<body>
258274
<section class="header">

0 commit comments

Comments
 (0)