When setting up Google Analytics 4 on your website, there are a number of steps to complete.
After setting up a data stream, you’re now ready to start collecting user data. At this point, you must now integrate the Google tag into your website so that views are registered in your property. The Google tag is a JavaScript code that collects and sends user data to the Google Analytics servers, as shown below.
As shown in the above figure, let’s say a user wants to visit www.foo.com and enters the URL into the address bar of the browser (1). The browser sends a request to the web server responsible for the domain www.foo.com and requests the HTML code for the start page (2). The web server sends back the HTML code of the start page (3). (Whether loading a file or querying a database makes no difference to the browser; it always receives HTML as the result.) The HTML code of the start page contains the Google tag. As soon as it has received the HTML information, the browser reads the page and starts JavaScript code (4), which in turn collects data. The script then sends a request to the Google Analytics server, including the collected data (5). The server receives the request, saves the transmitted data, and sends back a response code (6). When a new page is opened, such as www.foo.com/aboutus, the process starts again from the beginning.
To optimize the transmission, the tracking code can first buffer multiple events and then send these events in a single request. This process is referred to as batching.
To start collecting data for your website, GA4 provides several options in the data stream:
Each variant has its advantages and disadvantages, which we’ll briefly cover next.
In the web data stream details, the last item is View tag instructions. This item contains the installation instructions for integrating the Google tag. GA4 lists instructions for a whole range of systems to guide you through the setup. Simply copy the tag ID from the installation instructions, as shown in this figure, and enter this value in the relevant configuration for the plugin, CMS, or store.
Today, many offerings use WordPress as a website builder. With the Site Kit, Google offers its own WordPress plugin for the quick and easy integration of Google Analytics and other Google services, as shown below.
If your system is not on this list, looking for third-party plugins might be worthwhile. These plugins can extend a system with functions for code execution and configuration. For example, solutions from other manufacturers can display Google tags and data in WordPress.
With store systems, native integration or a plugin usually has an advantage in that the information for e-commerce tracking is displayed correctly, in addition to the Google tag.
Note: When using the CMS and store administration or plugins, you should also check whether they take the user’s consent selection into account. No tracking request should be sent, and no cookie should be set, without consent!
If your system is not on the list mentioned earlier, you can insert the Google tag manually into one of the templates or page templates. The Google tag (gtag) allows the integration of various Google tracking codes, as shown in the code listing below. The gtag is the basis for the integration of Google Analytics, but it can also request codes for Google Ads and Google Display & Video 360 (DV360).
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/
gtag/js?id=G-Z572914KR7"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-Z572914KR7');
</script>
In the web data stream, you’ll find the JavaScript code to copy and paste into your website in the installation instructions, under the Install manually tab, which is where the correct measurement ID is automatically stored. This element in the code is decisive because it determines which property the views are sent to. For Google Analytics data streams, the ID always starts with a G-, differing from Google Ads conversion tags, for example, which begin with AW.
Positioning the gtag Code in the Source Code of an HTML Page: An HTML page always has <head> and <body> elements. Codes and other files are loaded by the browser in sequence, first from the <head>, then from the <body>. Codes in the <head> are therefore loaded early. A best practice is to position the gtag code before the closing </head> tag, as shown in the listing below. The code can theoretically also appear later on the page if, for example, you don’t have access to the files containing the <head> area of your website.
<html>
<head>
... << Insert the gtag code here
</head>
<body>
... << Here is the second best place for the gtag code.
</body>
</html>
The direct integration of the tracking code promises optimal loading times and is relatively easy to implement if you have access to the backend or access to the files for your website. However, if you want to extend individual tracking, for example, for different clicks or for e-commerce, you’ll need in-depth programming skills in JavaScript.
Data Privacy: Anonymizing IP Addresses: The anonymization of an IP address is often described in the data privacy specifications for the older version of Google Analytics. GA4 always anonymizes the IP addresses of users. No additional command is necessary now; previously in UA, you had to use anonymizeIp.
When integrating gtag codes directly, an important step is to link them correctly to the consent query before the page is loaded for the first time. The consent manager and the Google tag must be configured accordingly, which is implemented differently depending on the combination.
Google Tag Manager is Google’s toolbox for all tasks related to tracking codes. This solution can configure tags, read and collect additional data from websites, and display the right tags at the right times.
For a larger setup with multiple tracking systems and many user actions that you want to measure, Google Tag Manager is an indispensable companion. Positioned between the website and the Google Analytics server, Google Tag Manager makes the development of your tracking codes independent of your website’s sprints and release cycles, as shown in this figure.
No Google Tag Manager Account? To install Google Tag Manager on your website, go to https://tagmanager.google.com and create an account and an empty container there. You’ll then receive a JavaScript code that you can integrate into your website (similar to the gtag). Next, create the gtag within your new Google Tag Manager container. More information about these first steps with the solution can be found here.
As a Google product, Google Tag Manager comes with the necessary templates for easy integration with Google Analytics. Simply note the measurement ID of the data stream during setup.
If you already have a running Google Tag Manager container, go to Google Tag Manager to create a new tag. In the list that appears, select the Google Tag. (Even if you click on the Analytics item, you’ll be taken to the Google Tag.)
In the settings window, enter the measurement ID in the corresponding field, as shown in this figure. At this stage, you can ignore the other options.
Now, the tag needs a trigger. Select the Initialization - All Pages trigger from the list. Finally, you must publish the container by clicking the Send button.
Triggering Google Analytics Tags The initialization trigger phase is fired at the earliest possible time when a page is loaded. This phase also requires correct interaction with your consent manager. If you run into issues when executing the tracking tag, select a later stage in the page structure, such as a Page View or DOM Ready.
The first Google tag ensures that the relevant pages of your website are tracked. In addition, it serves as a configuration template for individual GA4 event tags.
In a Google tag for individual event calls, select a GA4 configuration tag, from which the measurement ID for the event call is taken. As a result, you don’t need to constantly enter the measurement ID of your data stream.
Google Tag Manager is a powerful tool that supports many systems and codes. A more detailed introduction is available in the Google Tag Manager help here.
Reloading GA4 via an Existing Google Tag: Let’s say you’re already using the Google tag on a website, for example, for Google Ads. If so, you can link a newly created GA4 data stream to an existing Google tag. This method was originally intended as a simple method of extending an existing UA setup to include the then-new GA4 calls.
However, this approach has always entailed restrictions and challenges in various areas. You should therefore choose one of the methods we’ve already described. For more information about connected tags, go to this Google Support page.
The Google tag must not record user activities on a website or app without explicit permission. A number of data privacy requirements must be considered when tracking. To obtain the consent of your users for tracking, you’ll need a corresponding query that is presented to users when they first access your website.
You can then transfer the user’s selection to the Google tag or have the consent manager transfer it. The Google tag behaves differently depending on the consent (or refusal) given. The Google tag may only record user actions and data with the user’s consent.
The tag provides the consent mode for the transmission of these values. As described in this table, this mode comes in two variants, which we’ll cover next.
Simple Mode | Extended Mode | |
A user consents. | The Google tag records all user activities and sets cookies in the user's browser. | The Google tag records all user activities and sets cookies in the browswer. |
A user rejects. | The Google tag is blocked, does not send any data, and does not set any cookies. | The Google tag does not set cookies but instead sends pings to GA for modeling. |
When a page is viewed, the Google tag is loaded but does not yet send any data. The data won’t be sent until the Google tag receives the necessary consent signals. If a user refuses to have their activities tracked, the Google tag remains inactive, and the user will not be measured.
When a page is viewed, the Google tag waits again for the user to interact with the query. If consent is given, all actions are counted, and the user is permanently marked with a cookie. However, if consent is refused, no cookies are set, but user data is still sent in a trimmed-down version. Google refers to these views as pings. If a sufficient number of pings occurs, Google attempts to model and extrapolate user behaviors from those pings.
In both modes, Google Analytics assumes that the user has not yet made a selection or given consent when the page is viewed. This behavior can be adjusted in the Google tag settings.
Two variants of consent mode exist: In simple mode, no data is sent without the user’s consent. If consent is given, users are tracked with cookies.
In extended mode, without consent, the tag will still send requests to Google servers, but does not use cookies and omits some fields to take some privacy regulations into account. Based on these requests (Google calls them pings) GA will model the users in your reports.
The use of this technology should be carefully considered and discussed with your data protection officer. Before you think about integration, check the minimum number of users Google requires for modeling. If your website does not reach this limit, the extended mode will not provide any more data to your reports.
Google does not offer a separate banner or widget for displaying and obtaining consent. Instead, a whole host of solutions, both comprehensive consent manager services and (in some cases, free) plugins for various systems. A list of systems that directly support consent mode and provide data can be found here.
In the installation instructions, you’ll find a field for checking the integration at various points in the tabs. If you enter a URL, Google Analytics will check whether the Google tag is installed, as shown in the next figure. However, this test only works if you have integrated the Google tag directly. If you use Google Tag Manager, the test will fail.
We therefore recommend reviewing the Realtime overview report. Once you’ve integrated the Google tag of the newly created GA4 property, the first user activities should be visible in the report.
You can access this report via the GA4 menu. If you have integrated the Google tag into an existing website on which users are already browsing, the first entries should appear immediately, as shown in this figure.
However, if a completely new offer or a test environment, you must provide the initial data yourself. For this task, open the website from which you expect traffic in your browser.
An important step is that you now confirm the consent query, which is likely to appear. Only then will your visit be released for tracking and thus appear in the real-time view.
However, if your real-time report remains empty, start troubleshooting with the data call that the Google tag sends on the page.
For this step, go to your website and open the developer tools in the browser of your choice. In Chrome, for example, press (Ctrl) + (I). Go to the Network area. You’ll probably see a long list of entries. If not, reload the page.
In this window, you’ll see all files loaded by the browser when the page was viewed, as shown in the first figure below, including texts, images, fonts, and more. Now, use the filter field above the list to search for the collect?v=2 string, as shown in the second figure.
This step will show you all requests your browser has sent for tracking with GA4. If you do see requests, at least the data transfer has worked. Activities should then also be visible in the real-time report. If “real time” remains empty despite dispatch, investigate two possible causes:
However, if you do not find any collect calls in the list of network calls, you either have an issue with the code itself, or your browser does not want to send it. You should therefore check the following next:
Setting up Google Analytics 4 on your website involves more than just copying code; it requires choosing the right integration method, ensuring data privacy compliance, and confirming that data is flowing correctly into your reports. Whether you opt for a CMS plugin, direct code insertion, or Google Tag Manager, each approach offers different levels of flexibility and technical requirements. By carefully implementing and validating your setup, you’ll ensure accurate data collection that serves as the foundation for meaningful analysis in GA4. With the basics in place, you can now move on to customizing events and reports to better measure your users’ behavior and optimize your website performance.
Editor’s note: This post has been adapted from a section of the book Google Analytics 4: The Practical Guide by Markus Vollmert. Markus is the founder and managing director of the Cologne-based digital marketing agency luna-park.
This post was originally published 9/2025.