Skip to content

Instantly share code, notes, and snippets.

@crrmacarse
Last active May 13, 2020 03:23
Show Gist options
  • Save crrmacarse/4dbf3f0e5488b03d982cc2a323ba9b7b to your computer and use it in GitHub Desktop.
Save crrmacarse/4dbf3f0e5488b03d982cc2a323ba9b7b to your computer and use it in GitHub Desktop.
Google Analytics Config
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