Skip to content

Commit

Permalink
CMS-363: Update snowplow tracking (#1504)
Browse files Browse the repository at this point in the history
* CMS-363: Update snowplow tracking

* CMS-363: Add onRouteUpdate back
  • Loading branch information
ayumi-oxd authored Oct 28, 2024
1 parent 8cda580 commit 6bde08b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/gatsby/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import "bootstrap/dist/js/bootstrap.bundle"
import "@fortawesome/fontawesome-free/css/all.min.css"
import "./src/styles/style.scss"

import React from "react"
import useSnowplowTracking from "./src/utils/useSnowplowTracking"

export const onRouteUpdate = ({ location, prevLocation }) => {
if (typeof window.snowplow === 'function') {
window.snowplow("trackPageView");
// refresh snowplow link click tracking to pick up new links
window.snowplow("refreshLinkClickTracking");
}
sessionStorage.setItem("prevPath", prevLocation ? prevLocation.pathname : null);
};
}

const SnowplowWrapper = ({ element }) => {
useSnowplowTracking()
return element
}

export const wrapPageElement = ({ element }) => {
return <SnowplowWrapper element={element} />
}

// work-around for gatsby issue -- fix scroll restoration
// see https://github.com/gatsbyjs/gatsby/issues/38201#issuecomment-1658071105
Expand Down
13 changes: 13 additions & 0 deletions src/gatsby/src/utils/useSnowplowTracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useEffect } from "react"

const useSnowplowTracking = () => {
useEffect(() => {
if (typeof window.snowplow === "function") {
window.snowplow("trackPageView")
window.snowplow("refreshLinkClickTracking")
}
// empty dependency array ensures this runs on mount and unmount
}, [])
}

export default useSnowplowTracking

0 comments on commit 6bde08b

Please sign in to comment.