Created
September 20, 2019 14:57
-
-
Save abedzantout/7faac252b7b3050b8e60143e784a813d to your computer and use it in GitHub Desktop.
G tag page and event track
This file contains hidden or 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
| // 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