From a82b6043a78287214a1749ee1ba33e244072d09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20W=C3=A9br?= <115245871+webrdaniel@users.noreply.github.com> Date: Tue, 10 Sep 2024 08:16:33 +0200 Subject: [PATCH] feat: Add hubspot tracking on client page change (#1194) I had to add a client module that exports a `onRouteDidUpdate` function --- clientModule.js | 13 +++++++++++++ docusaurus.config.js | 1 + 2 files changed, 14 insertions(+) create mode 100644 clientModule.js diff --git a/clientModule.js b/clientModule.js new file mode 100644 index 000000000..43d04a081 --- /dev/null +++ b/clientModule.js @@ -0,0 +1,13 @@ +// client module for callbacks on route change +// see https://docusaurus.io/docs/advanced/client#client-module-lifecycles +export function onRouteDidUpdate({ location, previousLocation }) { + // Don't execute if we are still on the same page; the lifecycle may be fired + // because the hash changes (e.g. when navigating between headings) + if (location.pathname !== previousLocation?.pathname) { + // hubspot tracking page view + // eslint-disable-next-line no-underscore-dangle, no-multi-assign + const _hsq = window._hsq = window._hsq || []; + _hsq.push(['setPath', window.location.pathname]); + _hsq.push(['trackPageView']); + } +} diff --git a/docusaurus.config.js b/docusaurus.config.js index be899e25d..1e2ebb18f 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -210,4 +210,5 @@ module.exports = { '^/legal/*', ], }, + clientModules: ['./clientModule.js'], };