File tree 1 file changed +18
-0
lines changed
1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change 52
52
// Track initial pageview
53
53
trackPageview ( ) ;
54
54
55
+ // Listen for browser back/forward navigation
55
56
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 ) ) ;
56
74
}
57
75
58
76
// Helper function to track pageviews
You can’t perform that action at this time.
0 commit comments