mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 06:12:28 +02:00
fix(gtag): send the newly rendered page's title instead of the old one's (#7424)
Co-authored-by: Joshua Chen <sidachen2003@gmail.com> Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
7ab97d4726
commit
49ecd8f472
2 changed files with 27 additions and 14 deletions
|
@ -15,6 +15,12 @@ const {trackingID} = globalData['docusaurus-plugin-google-gtag']!
|
||||||
const clientModule: ClientModule = {
|
const clientModule: ClientModule = {
|
||||||
onRouteDidUpdate({location, previousLocation}) {
|
onRouteDidUpdate({location, previousLocation}) {
|
||||||
if (previousLocation && location.pathname !== previousLocation.pathname) {
|
if (previousLocation && location.pathname !== previousLocation.pathname) {
|
||||||
|
// Normally, the document title is updated in the next tick due to how
|
||||||
|
// `react-helmet-async` updates it. We want to send the current document's
|
||||||
|
// title to gtag instead of the old one's, so we use `setTimeout` to defer
|
||||||
|
// execution to the next tick.
|
||||||
|
// See: https://github.com/facebook/docusaurus/issues/7420
|
||||||
|
setTimeout(() => {
|
||||||
// Always refer to the variable on window in case it gets overridden
|
// Always refer to the variable on window in case it gets overridden
|
||||||
// elsewhere.
|
// elsewhere.
|
||||||
window.gtag('config', trackingID, {
|
window.gtag('config', trackingID, {
|
||||||
|
@ -26,6 +32,7 @@ const clientModule: ClientModule = {
|
||||||
page_location: window.location.href,
|
page_location: window.location.href,
|
||||||
page_path: location.pathname,
|
page_path: location.pathname,
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -13,10 +13,16 @@ function logPage(
|
||||||
location: Location,
|
location: Location,
|
||||||
previousLocation: Location | null,
|
previousLocation: Location | null,
|
||||||
): void {
|
): void {
|
||||||
console.log(`${event}
|
console.log(event, location.pathname, {
|
||||||
Previous location: ${previousLocation?.pathname}
|
location,
|
||||||
Current location: ${location.pathname}
|
prevLocation: previousLocation,
|
||||||
Current heading: ${document.getElementsByTagName('h1')[0]?.innerText}`);
|
heading: document.getElementsByTagName('h1')[0]?.innerText,
|
||||||
|
title: document.title,
|
||||||
|
description: (
|
||||||
|
document.querySelector('meta[name="description"]') as HTMLMetaElement
|
||||||
|
)?.content,
|
||||||
|
htmlClassName: document.getElementsByTagName('html')[0]?.className,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onRouteUpdate({
|
export function onRouteUpdate({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue