Skip to content

Instantly share code, notes, and snippets.

@abedzantout
Created September 20, 2019 14:57
Show Gist options
  • Save abedzantout/7faac252b7b3050b8e60143e784a813d to your computer and use it in GitHub Desktop.
Save abedzantout/7faac252b7b3050b8e60143e784a813d to your computer and use it in GitHub Desktop.
G tag page and event track
// Add your GA tracking id here
export const GA_TRACKING_ID = '';
const isProduction = process.env.NODE_ENV.toLowerCase() === 'production';
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const trackPageView = url => {
if (isProduction) {
// @ts-ignore
window.gtag('config', GA_TRACKING_ID, {
page_path: url
});
}
};
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
export const trackEvent = ({ action, category, label, value }) => {
if (isProduction) {
// @ts-ignore
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment