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

Hi,
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

Hi,

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.

Regards,
Ronan HELLO

1 Like

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

 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!