Skip to content

Instantly share code, notes, and snippets.

@rusrushal13
Created Oct 23, 2019
Embed
What would you like to do?
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