Allow Google Analytics to detect page changes based on browser history events when using <ClientRouter />
#1184
Replies: 4 comments 4 replies
-
I just noticed that the comment above those lines says
Now I am not sure how to solve this for both Partytown on Firefox, and Google Analytics... |
Beta Was this translation helpful? Give feedback.
-
I think a refactor that would unlock this use case would be great! I'd consider this a fix, as it's apparently the way we use the history object that breaks Google Analytics. So I think you could directly send a PR, but let's see what other maintainers think. |
Beta Was this translation helpful? Give feedback.
-
Hi @jonasgeiler, @MoustaphaDev 👋🏼, I would be happy to revert the code to the simpler version that we had before withastro/astro#9419. So the hope is that 1.5 years changed a lot to the better for Firefox and Partytown? Or will that reopen withastro/astro#8862? |
Beta Was this translation helpful? Give feedback.
-
I'll create an issue from this discussion so it doesn't get lost. Old discussions can be forgotten, but issues are frequently re-triaged. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The Problem
I am currently trying to integrate Google Analytics into my site. I am using the Client-side Router (SPA mode) of Astro and due to that, I turned on detection for
Page changes based on browser history events
in the "Enhanced measurement" section of the Google Analytics settings. This way, calls topushState
,replaceState
andpopState
are detected and causepage_view
events.However, I noticed that Google Analytics is unable to detect my page navigations. The only event it can detect is going back in history with the back button, because that is an actual browser event, and not a wrapped History API function.
I looked deeper into it and I think the problem lies with Astro caching/saving a bind to the
history.pushState
andhistory.replaceState
methods inrouter.ts
:https://github.com/withastro/astro/blob/af83b85d6ea1e2e27ee2b9357f794fee0418f453/packages/astro/src/transitions/router.ts#L25-L27
An Example demonstrating the Problem
Here is also an example script showing the behavior of gtag.js's monkey-patching of History API function, and where the problem lies:
As you can see,
pushStateWrapper()
is the only one that works, apart from calling it directly.My Proposed Changes
It would be working properly if Astro is not caching/saving the
pushState
andreplaceState
methods in a variable, but instead callshistory.pushState
andhistory.replaceState
directly every time.That's why I would like to propose to change the three lines in
router.ts
to something like this:Depending on how Astro's build tools are set up this might need some TypeScript adjustment, but testing it with the latest TypeScript Playground version, I got no warnings/errors.
I am also not sure if
inBrowser && ...
was even a good idea in the first place, since you can't call a boolean? But I'm gonna leave that as-is.I'd be happy to create a PR myself, if this gets approval!
Should be a rather simple change.
Beta Was this translation helpful? Give feedback.
All reactions