Google Analytics

Google Analytics 4 - And How to Implement it in React

In 2023, Google released a replacement to Universal Analytics. What does this mean for your UI?

Why use Google Analytics?

One of the most valuable parts of a UI is the ability to access user engagement. Being able to track the number of times your website is clicked on, or how many minutes users typically spend on your app, is crucial. This feedback allows you to know whether or not your UI is succeeding. If you don’t know the level of engagement your UI is attracting, you will not be able to address flaws that may be driving users away from your website or app.

For example, the amount of clicks your site gets, versus the amount of time users spend in one session, can tell you a lot about what’s working and what’s not in your UI.

If users are clicking on your site, but clicking off of it just as fast, this points to a specific problem. Your UI has an attractive presentation that entices viewers to click on it, but there must be something unappealing or untrustworthy that is apparent the moment users see your UI.

If you are not getting a lot of clicks, but the users that do click have high engagement, this can mean the opposite. Your content may be high quality, but something in the advertisement of your UI isn’t working.

Luckily, Google provides these analytics for you. Google Analytics allows you to collect data from your website, and it automatically generates reports concerning how users interact with your website.

Downloading Google Analytics is simple. First, you create a Google Analytics account. Then you add a small piece of JavaScript code into each page of your site. This code tracks the activity on your website, the user’s language settings, and which browser users are using to run your UI. This data is then packaged and sent to Google Analytics. There, your data is processed, organized, and sent back to you in the form of a report. 

Until July 1st, 2023, Google used Universal Analytics as the key way to track user activity. Afterwards, Google switched to Google Analytics 4. This change provides many benefits to developers.

Google Analytics 4 streamlines many aspects of data tracking, making it easier for you. Simultaneously, Google Analytics 4 maintains the same syntax and format of Universal Analytics, so transitioning between the two is not frustrating and does not force you to learn an entirely new analytics tracker. 

GA4 has made tracking your analytics much easier. 

Initializing GA4

Adding Google Analytics 4 to your UI is simple.

  1. To do so, first create an Analytics account.
  2. Go to Admin, select Create, then select Property to make your property.
  3. Select Next. Specify your industry category and your business size.
  4. Select Next. Put in the specifics on how you want to use Google Analytics. GA4 allows you to customize how your reports are generated and what specific elements you’d like to see emphasized in your reports.
  5. Select Create.
  6. To add a data stream, enter the URL of your website and the name of your Stream.
  7. Choose to enable or disable enhanced measurement.
  8. Select Create stream.

Changes in GA4

Google Analytics 4 maintains the same framework and terminology as Universal Analytics. However, Google has addressed some of the limitations of Universal Analytics and sought to automate more aspects of the process of collecting data analytics. With Google Analytics 4, tracking the activity of your users is easier than ever.

Events

In Google Analytics 4, ‘events’ have replaced ‘hits’ as a means of tracking user activity. An event encompasses any way a user can interact with a website. Clicking on a page, clicking on an external link, and amount of time scrolling—all of these things are counted as events under Google Analytics 4. 

Using events, you are able to see the specifics of how users interact with your UI.

Google Analytics 4 added both automatic events and custom events. GA4 tracks automatic events without you telling it to. These automatically tracked events include:

  • First_visit: This counts the first time a user either visits your site or launches your app. This gives you a glimpse into how much traffic your UI receives.
  • Session_start: This stores when a user begins a new session. This allows you to track the activity of returning users.
  • User_engagement: User engagement usually adds an event once a user has been on your UI for ten seconds. Optionally, user engagement can periodically keep adding events the longer a user remains on the UI. This helps you track how much time users spend on your UI.

These events are automatically calculated by Google, although you can keep track of your own custom events by adding them to the code.

Enhanced Measurement

Enhanced measurement measures many different types of events automatically. This expands beyond the previously listed automatic events, and includes a wide breadth of events useful to any UI developer.

These events include:

  • Page views
  • Scrolling
  • Outbound clicks
  • Video engagement
  • Downloaded files
  • Site searches

By enabling events in the Google Analytics 4 interface, you are able to track all of these events without changing any part of your code. If you would like to opt out of automatically tracked events, you can simply turn off enhanced measurement.

Measurement ID

If you are familiar with Google’s Universal Analytics, you may be used to using tracking ID. In Google Analytics 4, tracking ID has been replaced by a measurement ID

Measurement ID plays an important role in managing your data as it’s being sent to Google Analytics in order to create your report. Each property has a measurement ID, and this ID is used to show what property to which a tracking code belongs. It acts as a link between your website and the data stream, and it ensures the data from your site is sent to the correct location to create the report.

There are a few key differences between measurement ID and tracking ID. 

  • All tracking IDs began with a UA- and were followed by a string of exclusively numbers.
  • All measurement IDs begin with a G- and are followed by a string of both numbers and letters.
  • Measurement IDs also use a different tracking library than tracking IDs.

To find a measurement ID:

  1. Find the Admin tab.
  2. Find data collection and modification.
  3. Click Data streams.
  4. Click Web.
  5. Click Web data stream.

The measurement ID is located on the top right of the page, next to Stream Name, Stream URL, and Stream ID.

Implementing GA4 in React

Image: A computer displaying code.
GA4 integrates seamlessly with React.

There’s no question as to why React is such a popular library for web development. Using JavaScript as its source code, React provides developers with an organized, structured approach to build their UI. 

Since React is broadly used, there exists a large community of people surrounding it, resulting in a wide variety of tutorials for virtually any issue a developer could come across. The versatility of React also plays a role in making it so widespread. As it can be used for development of the UI on web, desktop, mobile, and even VR platforms, React is a tool vital to the work of many UI developers.

In order to be compatible with the new Google Analytics update, React’s react-ga library will be replaced by a react-ga4 library. React-ga4 does away with some of the limitations of react-ga, and it allows for integration of GA4 into React projects.

How to Migrate from react-ga to react-ga4

The process of changing your library in React is very simple. 

You just have to update your import from react-ga to react-ga4.

React-ga4 consists of the same syntax and uses the same API of react-ga. This means that transferring between the two libraries will not take any relearning. In addition, migrating between libraries will not interrupt the rest of your code, and it will not mess up your existing UI.

Integrating React and GA4

Once you have changed your library in React and updated to Google Analytics 4, the process of integrating both is short.

  1. Make sure you have react-ga4 installed.
  2. Using your measurement ID, initialize Google Analytics.
  3. Attach event handlers to buttons.
  4. Use usaGA4Event to send this data to Google Analytics.

Google Analytics 4 is a powerful tool to enhance UI. Knowing the activity of your users provides invaluable insight into your website or app. Once you are armed with this knowledge, you are essentially in open communication with your users, and their events will tell you what is working, and what you need to improve.

One of the best parts of GA4 has to be automation of code that previously needed to be done by hand. The simplicity of enabling GA4 was also an essential design choice by Google. Are you looking to make your UI intuitive and organized? Experts at UXAX are ready to help. Armed with knowledge of marketing, design, and analysis, UXAX can provide consultation for anything your UI needs.

Newsletter
Get great insight from our expert team.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
By signing up you agree to our Terms & Conditions