Skip to content

Commit 97428d3

Browse files
authored
updated databeat to track every page navigation (#60)
1 parent 1a6e62b commit 97428d3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

snippets/databeat-tracker.js

+18
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,25 @@
5252
// Track initial pageview
5353
trackPageview();
5454

55+
// Listen for browser back/forward navigation
5556
window.addEventListener('popstate', () => setTimeout(trackPageview, 300));
57+
58+
// Override History API methods to track programmatic navigation
59+
const originalPushState = history.pushState;
60+
const originalReplaceState = history.replaceState;
61+
62+
history.pushState = function() {
63+
originalPushState.apply(this, arguments);
64+
setTimeout(trackPageview, 300); // Delay to ensure DOM is updated
65+
};
66+
67+
history.replaceState = function() {
68+
originalReplaceState.apply(this, arguments);
69+
setTimeout(trackPageview, 300); // Delay to ensure DOM is updated
70+
};
71+
72+
// Also track on hash changes for SPAs that use hash-based routing
73+
window.addEventListener('hashchange', () => setTimeout(trackPageview, 300));
5674
}
5775

5876
// Helper function to track pageviews

0 commit comments

Comments
 (0)