[SPA & Tag Manager] History Change: how to get the new title? support

I have a SPA in React and I’m trying to track PageViews using Tag Manager via History Change.
I’ve followed this guide: https://developer.matomo.org/guides/spa-tracking
So I’ve already set the Matomo Configuration Variable with {{PageTitle}} and {{PageUrl}}.

The views are being tracked but Matomo shows the old page title (the url is correct) because the History change is fired before the document title updates.

How can I show the correct page title in Matomo?

Thanks in advance


You may have to push an object with current title and url in the data layer _mtm.
The data should come from your SPA router instead of the browser.


1 Like

Hi Ronan,
Thank you for the advice.
But wouldn’t this _mtm.push happen anyway after the history change trigger is fired?

 const location = useLocation();
 useEffect(() => {
    _mtm.push({ CustomPageTitle: title }); // data layer custom var
  }, [location]);

Is there a way to delay the execution of the history change trigger?

Thanks again!