Created
October 23, 2019 08:09
-
-
Save rusrushal13/2300562f92aaef6e17c2d6d127b93c70 to your computer and use it in GitHub Desktop.
react google analytics
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
#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(); | |
}); | |
}, []); |
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 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