Using Google Analytics (GA4) with Cookie Consent

Important:

Google Analytics 4 has replaced Universal Analytics. You can now view event tracking data in GA4.

For more information, see Google's Universal Analytics deprecation announcement and UA→GA4.

Google Analytics automatically tracks certain events, such as page views and clicks on outbound links, but if there are custom events, you need to set them up by defining necessary variables and triggers.

OneTrust automatically provides Google Analytics users with event tracking data. You will need to enable a Google Analytics Tracking for the Banner and Preference Center while publishing test or production CDN scripts. This enables you to see how visitors interact with the software to help you with future decision making.

We have named a set of Event Categories, Actions and Labels which should be self-explanatory. Once the code is installed on your site, you will start to see the events appearing in your Google Analytics reports.

This guide is not an exhaustive guide for how to use Google Analytics for your website, but details how to get started sending events to Google Analytics.

You will need to create the following custom Data Layer Variables:

eventModel.optanonCategory

eventModel.optanonAction

eventModel.optanonLabel

Next, you will need to set up the following custom Trigger:

trackOptanonEvent

Last, you will need to create the following Tag and apply the Trigger.

OneTrust Cookie Consent

Once you have set up the Data Layer Variables, Trigger, and Tag, you should be able to view events in Google Analytics.

To create a new Data Layer variable

  1. In Google Tag Manager, select the Variables tab from the main menu. The Variables screen appears.

  2. Under User-Defined Variable, click the New button. The Variable Configuration modal app
  3. Name the variable eventModel.optanonCategory.


  4. Set the type to Data Layer Variable under the Variable Type section.
  5. Set the Data Layer Variable Name to eventModel.optanonCategory.
    Note:

    The Data Layer Variable Names must be set to eventModel.optanonCategory, eventModel.optanonAction, and eventModel.optanonLabel for the code to work as expected.

  6. Press the Save button.
  7. Repeat the process for the remaining two Data Layer variables: eventModel.optanonAction and eventModel.optanonLabel.

To create the trackOptanonEvent trigger

This trigger will fire when the toggles are changed in the Preference Center.

  1. In Google Tag Manager, select the Triggers tab from the main menu. The Triggers screen appears.

  2. Press New. The Trigger Configuration screen appears.

  3. Name the trigger trackOptanonEvent.


    Field

    Setting

    Trigger Type

    Custom Event

    Event Name

    trackOptanonEvent

    Use regex matching

    Enabled

    This trigger fires on

    All Custom Events

  4. Press the Trigger Configuration and set the Trigger type to Custom Event.
  5. Set the Event name to trackOptanonEvent and enable Use regex matching.
  6. Select All Custom Events.
  7. Click the Save button.

To create the OneTrust Cookie Consent tag

  1. In Google Tag Manager, select the Tags tab from the main menu. The Tags screen appears.

  2. Press New. The Tag modal appears.

  3. Under the Tag Configuration, press the edit button. The Choose tag type modal appears.

  4. Set the type to Google Analytics: GA4 Event.
  5. Name the tag OneTrust Cookie Consent and fill out the fields as detrailed below.

    Field

    Setting

    Name

    OneTrust Cookie Consent

    Tag Type

    Google Analytics: GA4 Event

    Configuration Tag

    None - Manually Set ID

    Measurement ID

    Enter the GA4 measurement ID (G-XXXXXXXXXX).

    Event Name

    trackOptanonEvent

  6. Enter your Measurement ID.
  7. Add the following Event Parameters to the tag:
    Tip:

    Click Add Row to add multiple Event Parameters.

    Click the + icon to add values from the variables already created.



    Parameter Name (Custom)

    Value

    OptanonCategory

    {{eventModel.optanonCategory}}

    OptanonAction

    {{eventModel.optanonAction}}

    OptanonLabel

    {{eventModel.optanonLabel}}

  8. Click the Triggering icon. The Choose a Trigger modal appears.

  9. Select trackOptanonEvent.
  10. Click Save.
  11. Publish the changes in Google Tag Manager.

To retrieve required scripts

To check events on a production site

  1. Open the production website.
  2. Open the Developer Tools in your browser.
  3. Navigate to the Console tab.


  4. Enter dataLayer in the Search field.


  5. Interact with the Banner or Preference Center.
  6. Review the dataLayer in the Console tab.
  7. Confirm the event trackOptanonEvent passed to Google Analytics via dataLayer.
Note:

optanonAction values can be one of the below based on interaction with Banner or Preference Center:

optanonAction Values

  • Banner Accept Cookies

  • Banner Reject All

  • Banner Close Button

  • Preference Center Opened From Banner

  • Preference Center Opened From Button

  • Preference Center Opened From Function

  • Preferences Save Settings

  • Floating Cookie Settings Open Button

  • Preferences Allow All

  • Preferences Reject All

  • Preferences Toggle On

  • Preferences Toggle Off

  • Preferences Close Button

To view events in real time

View events in real time to ensure that the events from the website are being sent to Google Analytics.

Method 1

  1. In Google Analytics, select Reports.
  2. Select Engagements > Events.
  3. Go to the website the Google Tag Manager instance is present on.
  4. Change the consent settings in your Banner or Preference Center. You will see the events in real-time with the event name trackOptanonEvent.


Method 2

  1. In Google Analytics, select Reports.
  2. Select Realtime Overview.


  3. Go to the Event Count by Event Name section.
  4. Click the event name trackOptanonEvent to view event parameters.




Tip:

You can also click View user snapshot to see the data of one active user. Google Analytics 4 now picks one random active user and shows the activity.