Adding Google Analytics (GA4)
Uniform enables you to add personalization and testing capabilities to your applications. Google Analytics is a popular product for capturing and reporting on activity in your application. Uniform is able to push personalization and testing data to Google Analytics, improving the reach of your analytics.
info
This analytics integration in Uniform is for GA4. The previous generation product, (Universal Analytics), is being deprecated by 1st July 2023. You can learn more about Universal Analytics and its corresponding Uniform integration.
Tracking
This section describes how to activate tracking personalization and testing from Uniform in GA4.
Configure GA event
In GA4, Event Label and Event Category are deprecated in favor of parameters. To provide backward compatibility on tracking variants for personalization and tests on Uniform without label and category, you are required to manually create these custom dimensions for events in your Google Analytics dashboard.
Log into Google Analytics.
Navigate to your GA4 property.
Navigate to Configure > Custom definitions
Add the following custom dimensions:
Dimension name Scope Event parameter Notes Event Category Event event_categoryEvent Label Event event_labelUniform Control Group Event is_control_group
Activate GA plugin
The Uniform tracker can dispatch event data to Google Analytics using a plugin. You must add this plugin to your front-end application.
If your application does not already use the Google Analytics global site tag (gtag.js), you must add the script.
About this step
In a web application, the global site tag must be added immediately after the head tag is closed, meaning it must be the first thing after
</Head>.Add the following npm packages to your front-end application:
@uniformdev/context-gtagAbout this step
Make sure the package you add is version is 16.3 or greater.
Add the Google Analytics plugin to the Uniform Context object.
import { enableGoogleGtagAnalytics } from '@uniformdev/context-gtag';
...
const gaPlugin = enableGoogleGtagAnalytics({ emitAll: true });
const context = new Context({
...
plugins: [gaPlugin]
});
...About this step
The Uniform Context object is passed to the tracker. The precise code used to add the tracker to your application depends on your front-end.
The plugin causes the tracker to fire GA4 events when Uniform
renders personalized components or variants for A/B tests on
your application. Each event is accompanied by its corresponding
event_label, event_category, and is_control_group values.