Some background...
We are working with an SPA site built in React. We are currently integrating Google Analytics 4. We have enabled Enhanced Measurement for GA4, which collects a lot of data automatically, including pageview. Since the React router uses the Web History API to change pages, the automatic GA4 pageview is working almost perfectly out of the box. However, we have noticed that on some data collections, sometimes the page title lags and uses the title from the previous page.
The assumption is that this is happening because of the following... 1) when we start a navigation, React changes the URL right away without knowing any other meta data about the page (including the page title), and 2) once we fetch the meta data about the page, we then update the DOM with that data; but... 3) GA4 is triggering right away at the URL / history change, which is before we've added the proper title.
Knowing this, we understand that we could fix this issue by delaying the URL change until we have the proper meta data. However, since we are working with the React Router, there doesn't seem to be a built-in way to do this. It seems like we would have to write our own router which would be way outside of the scope for this project. Does anyone know of any way to do this without major surgery?
We also understand that we could fix this by disabling the automatic pageview and simply implementing a custom pageview event. However, our analytics manager has concerns around this because 1) in order to do this, we have to turn off Enhanced Measurement entirely; there is no way to keep Enhanced Measurement enabled and disable only the automatic pageview event, and 2) we would be moving to a non-standard implementation, which in the past has given us many headaches.
Has anyone come across these issues, and if so what kind of solutions did people come up with to solve this?