Google Analytics
Last updated
Was this helpful?
Last updated
Was this helpful?
In October 2020 Google launched GA4 - a major change to Google Analytics, one which is more event driven to reflect that there has been a fundamental shift away from traditional web sites to web applications and mobile apps.
What follows is a guide for implementing gtag.js into the codebase, this is different from Google Tag Manager.
Tag Manager ( a tool for processing events in a website and sending to a GA account ) can have difficulty with the complexity of site described in this guide as it's more of a full on javascript application than a website.
GA4 - it's wildly different than GA3 and Universal Analytics, and is the recommended path take by Google who is committed to it (as it works better with Javascript applications (like pathways), mobile apps etc).
gtag.js - Is a developer friendly library that exposes only a few methods for logging events later in this document each of these will be explored.
Install the npm package, it helps to ensure the that the google scripts are initialized correctly before calling functions.
This small but mighty package will do a few things:
Reference the google script file from google's CDN
Add a script tag to the head element to execute javascript to wire up GA
Expose GA4 in react in an easy to use way.
Update .env file to include the GA4 Measurement ID.
Create React App
Vite
Update index.tsx to:
Read the GA4 Measurement ID environment variable
Create an instance of the Ga4react class
Automatically enter into debug mode when running locally
Initialize the ga4react object prior to rendering the app to ensure GA4 is available in the app.
Once deployed, the website will not work as the webpage is referencing a script from a different domain and also using some inline code, thanks to CSP.
To get things working the HTTP headers will need to be updated:
Allow script and images to be loaded from a different domain
Allow a very specific inline script to be run (adding a hash of the script)
As the inline script includes your GA Measurement ID, you have to generate the hash yourself for each environment (assuming you use a different measurement ID for QA/UAT/Prod)
This will not work with proper security headersUpdate CSP
To generate the CSP hash use the following as an example:
Read more on CSP with these resources
Example CSP from a Static-Web-App config section for reference.
Update App.tsx to register every page change by subscribing to react routers' location changes
It is always good to update the page title for usability, it also benefits in GA
Testing everything out:
Debug the codebase and navigate to the webpage, a new floating box will be shown the lower right of the screen, click "connect" to connect to the Tag Assistant, a new browser window will open and from there it is easy to see google is receiving from the website.
(change out the code with your GA Measurement ID and use an to get the proper hash value, in the example above, the hash would be: sha256-IBkv3CC/Fwz7jbHfcCIlP19U0yv+7XjdPbTS4DBRBeg=
)
Google Analytics -
Google Analytics -
Google Tag Assistant -