The gtag plugin sends the previous document title #7420
Labels
bug
An error in the Docusaurus core causing instability or issues with its execution
domain: analytics
Related to the gtag/GA plugins
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
The default Docusaurus gtag plugin reports wrong page titles to Google Analytics.
The gtag plugin adds a client module that uses
onRouteDidUpdate
event to send to GA the pathname and the page title.docusaurus/packages/docusaurus-plugin-google-gtag/src/gtag.ts
Lines 1 to 33 in a2c993b
The
title
element reported to GA is the title of the last visited page instead of the title of the current page.I suspect the issue is the way the title is updated internally with
react-helmet-async
which causes it to be the old title at the time of theonRouteDidUpdate
event.Reproduction
I created this reproduction of the issue by imitating the way the gtag plugin works by registering a client module with
onRouteDidUpdate
that logs to console the current location pathname, page title, and actual page h1 to see if the page content was updated at the time of the event.https://stackblitz.com/edit/github-fab3de?file=docusaurus.config.js
You can navigate to different pages and see in the console that both the pathname and the page content are updated in time for the event while the title is still of the previous page.
Logs:
Reproducible demo
https://stackblitz.com/edit/github-fab3de?file=docusaurus.config.js
Steps to reproduce
Expected behavior
It is expected that the reported title will be the actual title of the page.
Actual behavior
The title reported is the title of the previously visited page.
Your environment
Self-service
A possible solution is replacing
react-helmet-async
withreact-helmet
.If that's all that needs to be done and it won't break anything else then maybe I can issue a PR myself but I'm not familiar enough with the Docusaurus code base to know what else it could affect and I don't have a lot of time available to devote for this.
The text was updated successfully, but these errors were encountered: