Track Custom Events With Google Analytics
Hey! Thanks for stopping by! Just a word of warning, this post
is 7 years old, wow! If there's technical information in here it's more than likely out
of date.
Do you use Google Analytics (GA) for any of your sites?
I’ve used it in the past and it gives quite good information about
Want to track what users of your site are clicking and navigating to?
Let’s talk about Google Analytics, what is it used for
Tracking Events
Privacy Policy
Ambulance Chasers
We’re going to use the React context API
import React, { useContext, useEffect } from 'react'
const AnalyticsContext = React.createContext({})
export const AnalyticsProvider = ({ children }) => {
useEffect(() => {
if (typeof window.ga === 'undefined') {
window.ga = (x, y) => {
console.log(`I'm a fake GA`, x, y)
}
}
}, [])
const events = {
logButtonPress: e => {
window.ga('send', {
hitType: 'event',
eventCategory: 'buttonPress',
eventAction: 'click',
eventLabel: e.target.innerText,
})
},
logSocial: socialMediaPlatform => {
window.ga('send', {
hitType: 'event',
eventCategory: 'socialShareClicks',
eventAction: 'click',
eventLabel: socialMediaPlatform,
})
},
logNavigation: navItem => {
window.ga('send', {
hitType: 'event',
eventCategory: 'navigationClicks',
eventAction: 'click',
eventLabel: navItem,
})
},
}
return (
<AnalyticsContext.Provider value={events}>
{children}
</AnalyticsContext.Provider>
)
}
export const useAnalytics = () => {
return useContext(AnalyticsContext)
} As high up the component tree add the provider.
import { AnalyticsProvider } from '../components/GAEventTracking' Then use in the areas you Want
export const ButtonComponent = props => {
const analytics = useAnalytics()
return (
<button onClick={analytics.logButtonPress} {...props}>
{props.children}
</button>
)
}There's a reactions leaderboard you can check out too.
Sign up for the newsletter
Want to keep up to date with what I'm working on?
Join other developers and sign up for the newsletter.
I care about the protection of your data. Read the Privacy Policy for more info.