Created
June 28, 2021 19:03
-
-
Save markfknight/d3c341716df7a2cc521c62193e56838f to your computer and use it in GitHub Desktop.
useTracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react'; | |
function useTracking(page = '') { | |
const [pageName, setPageName] = useState(page); | |
useEffect(() => { | |
const track = (pageName: string): NodeJS.Timeout | null => { | |
let timeout: NodeJS.Timeout | null = null; | |
if ((window as any).analytics && pageName) { | |
(window as any).analytics.page(pageName) | |
} else { | |
if (pageName) { | |
timeout = setTimeout(() => timeout = track(pageName), 200); | |
} | |
} | |
return timeout; | |
} | |
const timeout = track(pageName); | |
return () => clearTimeout(timeout as NodeJS.Timeout); | |
}, [pageName]); | |
return { setPageName } | |
} | |
export { useTracking } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment