I have a SPA in React and react-router-dom; I’m trying to track page views with History Change using Matomo Tag Manager.
I have followed these two guides:
- https://developer.matomo.org/guides/spa-tracking
- https://matomo.org/faq/new-to-piwik/how-do-i-start-tracking-data-with-matomo-on-websites-that-use-react/
When the route changes, i update the document title using a useEffect hook:
useEffect(() => {
document.title = title;
}, [title]);
The page view is being tracked, but the title of the previous route is recorded and displayed on the Matomo Dashboard.
The problem is that the History Change tag is triggered before the page title update.
I tried to push the title to the Data Layer with
_mtm.push({ CustomPageTitle: title })- or
_paq.push(["setDocumentTitle", title])
but in both cases, they are pushed after the history change is triggered.
The only way I found to track the correct title is to turn off the History Change tag and track page views on my own programmatically with a custom event, e.g.:
_mtm.push({ event: "my-page-view" });
but it's a workaround.
I can't use _mtm.push({ event: "mtm.PageView" }); due to this bug
Is there a way to do this using the Matomo Tag Manager?