How to tag for both Universal Analytics and Google Analytics 4 properties

This article describes how to instrument a website/web-app with tags to populate Universal Analytics (UA) and Google Analytics 4 (GA4) properties at the same time. More advanced functionality (e.g. ecommerce) is covered in companion articles.

Use the following buttons to pick the library you are currently using:

Set up the basic page tag

To populate both your UA and GA4 properties, you’ll need to either add a new tag or modify your existing page tag.

If you use Google Tag Manager for your UA property, add a GA4 configuration tag.

  1. Click Tags > New.
  2. Click Tag Configuration.
  3. Select Google Analytics: Google Analytics 4 Configuration.
  4. Enter your Measurement ID.
  5. Click Triggering and select appropriate events that would cause the tag to fire.
  6. Save the tag configuration and publish your container.

Learn more about Google Tag Manager's GA4 tag templates.

Generate a virtual pageview

If you generate virtual pageviews in analytics.js, for example:

ga('set', 'page', '/my-virtualpage-path');
ga('send', 'pageview');

you can do the same thing in gtag.js as follows:

gtag('config', 'G-XXXXXXXXXX', {
  'page_path': '/my-virtualpage-path'
});

If you use Google Tag Manager for your UA property, add a Google Analytics GA4 Event tag and configure it to send a virtual pageview event.

  1. Click Tags > New.
  2. Click Tag Configuration.
  3. Select Google Analytics: Google Analytics 4 Event.
  4. For Configuration Tag, select the Configuration Tag you created earlier.
  5. For Event Name, enter page_view.
  6. Click Triggering and select appropriate events that would trigger your virtual page views.
  7. Save the tag configuration and publish your container.

Learn more about Google Tag Manager's GA4 tag templates.

Disable data collection

If your site's privacy policy provides an option for the user to opt-out of Google Analytics, you’ll need to instrument the opt-out for both your UA and GA4 properties.

To disable data collection with Google Tag Manager implementations, you can disable data collection in several ways, including:

  • Define a variable that indicates if the user has opted out, and use triggers and exceptions to prevent a tag from firing if this variable is true.
  • Hard-code window['ga-disable-G-XXXXXXXXXX'] = true; into your web page code so that it executes before any Tag Manager containers are loaded.
  • Create a Custom HTML tag with window['ga-disable-G-XXXXXXXXXX'] = true; and use Tag Sequencing to ensure it fires before your Google Analytics tags.

IP anonymization

In GA4 properties, IP addresses are automatically anonymized.

Cross-domain Measurement

In GA4 properties, cross-domain measurement configuration is handled entirely from the Google Analytics user interface. See Measure activity across domains.