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.
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'
});
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.
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.