Instead of copying and pasting a custom event, I have created a utils/ga.js
file which is the below. I am just wondering if this would still be ok to use like this?
import ReactGA from 'react-ga4'
// initialize google analytics
ReactGA.initialize('G-7XXX')
// custom pageview with the location from react router
export const pageView = path => {
return ReactGA.send({hitType: 'pageview', page: path})
}
// custom event with label being an optional parameter
export const customEvent = (category, action, label = '') => {
return ReactGA.event({
category: category,
action: action,
label: label,
})
}
The use of useLocation here, will this still work? For example if I want to import the function like so:
import {pageView} from '../utils/ga.js'
import {useLocation} from 'react-router-dom'
// and use it like
let location = useLocation()
pageView(location.pathname)
Would the useLocation hook know the location even though I called it in a different file?
Also is this a good practice, especially with using the customEvent and calling it like
import {customEvent} from '../utils/ga.js'
const event = customEvent
event('somestring', 'some_event')