You probably know about the react-ga npm package but the sad news is, it does not work with the latest Google Analytics 4 version. It works with the old
UA-0000-01 tags, but not with the
G-XXXXXX tags. I’m sure an updated version of the package will solve it, but for the time being, I found a better and a working method of adding GA to your React application. And it’s so obvious, you will feel like, "why didn’t I think about it before?"
So how to add the new Google Analytics 4 in Reactjs app? You simply copy the Google Analytics script tag into the
<head> section of your index.html file in the public directory.
Yeah, I know right, it’s that obvious. I found it by trial and error. If you stick around, I will show you a step-by-step guide and also give you a glimpse at how did I figure it out.