'Google analytics in React
How to implement Google Analytics on onCLick events? using react-ga module or some other similar modules.
E.g. if a website has search feature, say i want to search customer id or item id and, i want to track the search events.
Solution 1:[1]
Without any code it's hard to help you out and see how this solution fits in your project, but one way to do it is using ReactGa.
On the page you need the function import the reactGa
import ReactGA from 'react-ga';
Next create a function to handle the event
const eventTrack = (category, action, label) => {
console.log("GA event:", category, ":", action, ":", label);
ReactGA.event({
category: category,
action: action,
label: label,
})
}
now attach the event to the button
<button type="submit" className="search" onClick={eventTrack.bind(this,
"category", "Action", "Label")}>search</button>
this should get you on the way, you'll need to supply props for the search terms. By implementing the code you should be able to see the console.log and see exactly what get's logged.
On the ReactGa github, there is some great examples of using the component
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nichlas Birkholm |
