Skip to content

Instantly share code, notes, and snippets.

@markfknight
Created June 28, 2021 19:03
Show Gist options
  • Save markfknight/d3c341716df7a2cc521c62193e56838f to your computer and use it in GitHub Desktop.
Save markfknight/d3c341716df7a2cc521c62193e56838f to your computer and use it in GitHub Desktop.
useTracking
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