History Change shows old page title

Hi,
I have a SPA in React and react-router-dom; I’m trying to track PageViews using Tag Manager.
I have followed these two guides:

So in my custom title is {{PageTitle}} and custom url is {{PageUrl}}.

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 wrong page title is logged: the previous route title (in other words the old title).
The issue is that the HistoryChange is triggered before my hook to update the title.

What can I do? I tried _paq.push(["setDocumentTitle", document.title]); inside the useEffectHook but it’s not seems to work.

For the guys working with React SPAs: what’s the best way to track page views in MTM with the correct title?
The hook is specific for Matomo so I can re-implement the whole update of document title.

Thank you in advance

Hi! I was wondering if you already found a fix for this issue? We have the same problem that the page title of the first page visited is stuck.

Hi PauRij,
Sadly, nope; I couldn’t find a fix for this issue, the only way I found to track the correct title is to turn off the History Change tag, sob.
Then I choose to track page views on my own programmatically with a custom event, e.g.:
_mtm.push({ event: "my-page-view" });

I also asked on Stack Overflow, a few days ago I got an answer: they advised me to add a delay in the tag’s Advanced Settings, but I haven’t tried it yet.

Here is the link: https://stackoverflow.com/a/78956669/27489883

It’s a bit strange that there’s no best practice or solution in the guides or on the web. This should be a fairly common issue, unless I’m missing something obvious.

Thanks for your quick answer! I thought the same… I tested the delay but with me it didn’t work unless I missed something as well. I’m now checking it with Matomo support.

You’re welcome!
Please let me know if you find a solution with Matomo Support.

1 Like

Have you found any solution?

Not yet. So far Matomo support also gave me the answer of the delay option as Matteo mentioned above.