diff --git a/src/plugins/docusaurus-plugin-segment/segment.js b/src/plugins/docusaurus-plugin-segment/segment.js index 8572c5d9a..e9e3ac850 100644 --- a/src/plugins/docusaurus-plugin-segment/segment.js +++ b/src/plugins/docusaurus-plugin-segment/segment.js @@ -29,17 +29,20 @@ function getOneTrustConsentContext() { } export default ExecutionEnvironment.canUseDOM ? { - onRouteUpdate({ location }) { - // Don't track page views on development - if (process.env.NODE_ENV === 'production' && window.analytics) { - window.analytics.page({ - app: 'docs', - page_type: 'DOCS_PAGE', - path: location.pathname, - url: location.href, - search: location.search, - ...getOneTrustConsentContext(), - }); - } + onRouteUpdate() { + // this forces deferred execution that ensures `window.location` is in sync + setTimeout(() => { + // Don't track page views on development + if (process.env.NODE_ENV === 'production' && window.analytics) { + window.analytics.page({ + app: 'docs', + page_type: 'DOCS_PAGE', + path: window.location.pathname, + url: window.location.href, + search: window.location.search, + ...getOneTrustConsentContext(), + }); + } + }, 0); }, } : null;