Open DomVinyard opened 1 year ago
Here are some useful articles on how to implement this feature/ documentation.
https://medium.com/geekculture/how-to-use-google-analytics-on-reactjs-in-5-minutes-7f6b43017ba9
https://www.npmjs.com/package/react-ga4 (we opted to use the react-ga4 library as opposed to implementing google analytics manually, this specifically because it was compatible with google analytics 4)
https://developers.google.com/analytics/devguides/collection/ga4#:~:text=Google%20Analytics%204%20is%20an,geared%20towards%20a%20developer%20audience. (Information on how google analytics works)
Firstly we import in the ReactGA object from the react-ga4 library, then we can initialize it with the GA measurement id provided in your google analytics account using the ‘initialise’ method.
With this step completed, we can use the ‘send’ method to send a pageview every time the path of the page is changed to google analytics.
import ReactGA from "react-ga4";
ReactGA.initialize("G-NDPH0VPH2Y");
function App() {
useEffect(() => {
ReactGA.send({
hitType: "pageview",
page: window.location.pathname,
});
});
return (
<div className="App">
//other components here
</div>
);
}
For the events, we opted to create a new file called gaEvents.js in the utils folder. This file contains all the functions that can be used for any relevent events. All can be found inside this file and should be implemented wherever needed for future features. The following functions are currently exported from this file, with the parameters provided:
sendGAEvent(data)
data = { category: "", action: “", label: “”,}
sendRelationEvent(parent, child)
sendBreadCrumbEvent(child, parent)
sendClickEvent(element, name)
audienceChangeOnSubjectEvent(subject, audience)
sendAudienceLevelChangeEvent(level)
Couple of things to be aware of:
This extension when enabled will allow you to view logs for GA in your console: https://chrome.google.com/webstore/detail/google-analytics-debugger/jnkmfdileelhofjcijamephohjechhna?hl=en
Ga will not work if you have an adblocker enabled.
@bp289 is that your actual GA ID? If it's sensitive, it'd be best to remove it from Github
User Story:
As a product owner, I want to add Google Analytics to our React application so that we can track user behavior and improve our product.
Acceptance Criteria: