Install and configure Google Analytics 4 (GA4)
It seems like it was yesterday when we last talked about Google Analytics. For some time now, things have changed a lot! In this article you will learn how to install and configure Google Analytics 4 (GA4) on your Drupal website. But first, a little context would be nice, right?
What is Google Analytics 4
Google Analytics is an essential tool for any website owner who wants to better understand their users' behavior and improve their site's performance. With the arrival of Google Analytics 4 (GA4), new features and capabilities have been introduced to provide even more insights into traffic and user engagement.
Create a Google Analytics account 4
To use Google's acclaimed service, you need to create a free account here . Creating an account is not difficult, simply follow the steps and complete the information requested by Google.
When you have completed the steps of creating your account, you will need to create a Google Analytics Property . This "property" is nothing more than the name of your website or application. To create a property, you will have to enter some details about your company or about yourself, such as what the name of your website or app is, what the sector is, etc.
The last step will be decisive: it is about how the data will be collected. It is important that you select the source of your data, being able to choose between several platforms. If you are working with an application or web page, you should select the "Web" platform.
Next, you will need to configure the web flow. To do this, you need to enter the web domain and the name of your new flow.
Once created, the property will be linked to your Analytics account, but it is not working yet.
Configure the service in Drupal
Finally we have to link Google Analytics 4 on our Drupal website. To achieve this, we have two contributed modules:
- Google Analytics β While functional and compatible with the latest versions of Drupal and GA4, the developers recommend using Google Tag for future Google Analytics support.
- Google Tag : it is the module that is recommended to use. It is compatible with Google Analytics from version 2.x and maintains its stable and fully functional releases, in addition to integrating the Google Tag Manager service.
For obvious reasons, we will use the Google Tag module to continue setting up Google Analytics 4 on our website. You can easily install it via composer, visit the module page for more information on supported Drupal versions.
Once installed on our website, it will appear in Settings > Web Services > Google Tag .
Now, do you remember the flow we created in the Google Analytics account creation process? In this flow we define a name and the domain of our website. Now we must obtain the measurement ID from there, which is a label whose format is similar to G-XXXXXXXXXXXX . Now we need to copy that identifier and paste it into the Google Tag ID(s) field in the Google Tag module settings.
This should be enough, although you can make additional adjustments at the event or condition level. For example, you can modify that nothing is recorded for users who have an administrator role, to prevent administration pages from being crawled and data being falsified.
View Google Analytics data 4
You must wait approximately 48 hours before data from your website is reported, although it is usually less time.
When data from your website begins to be collected, you will be able to access the Reports window of your property, to view really useful information and tracking data, such as visits to each of your content, events carried out on your website, visits in real time...