Skip to content

Commit

Permalink
fix: pageviews: handle missed load event (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed May 29, 2024
1 parent f5fd2b7 commit ae6bd43
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/sdk/src/trackers/pageViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function pageViews(): Trackable.Manager {
},
});

return Closer.combine(
const listeners = [
spyOn(history, 'pushState', () => {
pageChanged({ type: 'pushstate' });
}),
Expand All @@ -28,9 +28,16 @@ export function pageViews(): Trackable.Manager {
listenOn(window, 'popstate', pageChanged),

listenOn(window, 'hashchange', pageChanged),
];

listenOn(window, 'load', pageChanged),
);
// if document is already loaded, call pageChanged otherwise listen for load event
if (document.readyState === 'complete') {
pageChanged({ type: 'load' });
} else {
listeners.push(listenOn(window, 'load', pageChanged));
}

return Closer.combine(...listeners);

function pageChanged({ type }: { type: string }) {
controller.setContext({
Expand Down

0 comments on commit ae6bd43

Please sign in to comment.