Skip to content

Instantly share code, notes, and snippets.

@rusrushal13
Created October 23, 2019 08:09
Show Gist options
  • Save rusrushal13/2300562f92aaef6e17c2d6d127b93c70 to your computer and use it in GitHub Desktop.
Save rusrushal13/2300562f92aaef6e17c2d6d127b93c70 to your computer and use it in GitHub Desktop.
react google analytics
#hook for initializing react google analytics
useEffect(() => {
initGA(Config.googleTrackingGAID);
if (
window.performance &&
performance.navigation.type === performance.navigation.TYPE_NAVIGATE
) {
PageView();
}
history.listen(location => {
PageView();
});
}, []);
import ReactGA from "react-ga";
export const initGA = trackingID => {
ReactGA.initialize(trackingID);
};
export const PageView = () => {
ReactGA.pageview(window.location.pathname + window.location.search);
};
export const GAEvent = (category, action, label) => {
ReactGA.event({
category: category,
action: action,
label: label
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment