Sigma-Labs-XYZ / Explain-AI

An AI-powered source of all human knowledge
https://explainai.me/
1 stars 1 forks source link

Google Analytics #68

Open DomVinyard opened 1 year ago

DomVinyard commented 1 year ago

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:

bp289 commented 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)

swdilip commented 1 year ago

How the Analytics Data collected can be used

Clicks

Page Views

Navigation Journeys

bp289 commented 1 year ago

ExplainAI GA Docs

Analytics for each page view

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>
  );
}

Analytics for events

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)

bp289 commented 1 year ago

Couple of things to be aware of:

Ademsk1 commented 1 year ago

@bp289 is that your actual GA ID? If it's sensitive, it'd be best to remove it from Github