'Adding a custom GA event from a util file. Best practice?

Just a quick one here. I want to use a custom event through out my app, allowing me to see what people are doing the most and the least etc.

Instead of copying and pasting my custom event function through out my codebase, I thought of creating a util file and exporting that instead. This is what I have done.

// util/ga.js

import ReactGA from 'react-ga4'

ReactGA.initialize('G-XXXX')

export const eventTracker = (category, action, label = '') => {
  
  return ReactGA.event({
    category: category,
    action: action,
    label: label,
  })

}

I would then use that function like so in my react app.


import {eventTracker} from '../utils/ga'

const event = eventTracker
event('button', 'something')

I am just curious, is this a good way to go about it? Or is there a better way of doing things?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source