Last active
May 13, 2020 03:23
-
-
Save crrmacarse/4dbf3f0e5488b03d982cc2a323ba9b7b to your computer and use it in GitHub Desktop.
Google Analytics Config
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, { EventArgs, TimingArgs } from 'react-ga'; | |
const GA_CODE = process.env.GA_TRACKING_CODE; | |
/** returns an initialized ReactGA instance */ | |
const initGA = () => ReactGA.initialize(GA_CODE); | |
/** | |
* Send page view | |
* @param page optional. Defaults to current browser URL | |
*/ | |
export const sendPageView = (page?: string) => { | |
initGA(); | |
const windowLocation = window.location; | |
const defaultUrl = windowLocation.pathname + windowLocation.search; | |
const url = page || defaultUrl; | |
ReactGA.pageview(url); | |
}; | |
/** | |
* Send modal view | |
* @param modalName | |
*/ | |
export const sendModalView = (modalName: string) => { | |
initGA(); | |
ReactGA.modalview(modalName); | |
}; | |
/** | |
* Send event | |
* @param event [EventArgs] | |
*/ | |
export const sendEvent = (event: EventArgs) => { | |
initGA(); | |
ReactGA.event(event); | |
}; | |
/** | |
* Send timing event | |
* @param timing Sends event args | |
* | |
* More: https://developers.google.com/analytics/devguides/collection/gtagjs/user-timings | |
*/ | |
export const sendTiming = (timing: TimingArgs) => { | |
initGA(); | |
ReactGA.timing(timing); | |
}; | |
export default initGA; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment