Block page view with query parameter in Google Analytics
Learn in this tutorial how to exclude pageviews from Google Analytics tracking when they have unwanted URL query parameters.
Any query strings in Page URLs or JavaScript redirects can cause tracking issues. For example, a CMS might embed user data in the URL, which can lead to limitations in analysis and expose user data to all services on the website. Therefore, you can simply exclude such pageviews from tracking by their URL parameters.
In my previous tutorial, we already discussed how to filter query parameters from the URL, so that the parameters no longer appear in Google Analytics reports.
If you don't want to have such pageviews in your analytics data at all, I will now show you how to completely block tracking for certain query parameters.
And - I will show you two different methods to achieve that!
The first method is easy and only requires the use of Google Tag Manager in combination with a special trigger configuration that checks for the presence of the parameter.
The second method achieves the same result in a different way, and therefore we will also look at it.
Both methods work with a single URL parameter or with a list of query parameters.
How to block Google Analytics pageviews with URL query parameters?
To illustrate the following methods, let's assume that we want to block all pageviews that contain the exclude=true
parameter.
We can either add a condition to a pageview trigger or alternatively, we can create a blocking trigger. Let's look at both options 🤓.
Adding query parameter conditions to a pageview trigger
In Google Tag Manager, there is a built-in variable called Page URL. This variable contains the entire browser address as a string, which means that we can check it against any query parameters in the trigger configuration settings.
To do this, go to your triggers in the GTM container and create a new pageview trigger. Select that this trigger should only fire on some pageviews.
Next, select the Page URL variable from the dropdown menu and add the "does not contain" rule, along with the example parameter mentioned above, exclude=true
.
If you click save now, you have created a trigger that fires on all pageviews, except those that contain the query parameter 💪.
Next, use this trigger for the Google Analytics 4 tag that sends the event for pageviews. In most cases, this is the GA4 Configuration tag, if the checkbox for "Send a page view event when this configuration loads" is checked there.
Done.
How to Stop GA Tracking for a List of Query Parameters?
If you have a list of query strings for which tracking should be suspended every time they appear in the web address, then set the trigger with a Regex matching rule. Don't worry, you don't have to learn Regex specifically for this.
Regex (short for Regular Expressions) is a sequence of characters that describe a pattern in a string, which can be used to search text and perform an action when there is a match.
Creating Regex Rule for URL Parameters
Let's assume we want to exclude all page views that contain the following query strings from web tracking:
exclude=true
debug=4387969876
userName=hans.hansen
email=hans@hansens.com
Multiple strings can be concatenated in Regex using the pipe symbol.
Since the values for the variables debug
, userName
, and email
are dynamic, we also include a wildcard in the Regex rule:
Regex matching rule: exclude=true|debug=.*|userName=.*|email=.*
Using GTM Trigger with Regex Rule
Next, we create a new trigger in Google Tag Manager in which the Page URL does not match the regular expression.
Afterwards save the trigger and apply it to your page view tag. Finished.
Create blocking trigger with URL parameter
The second method is to add a blocking trigger to an event or page view tag.
The difference is that with the first method, you change the general trigger for page views by specifying the URL parameters. However, this change can lead to complications due to effects on other tags with the same trigger 🙄.
Therefore, it is usually easier to create a single blocking trigger and then add it to the tag as a trigger exception.
So in the second method, we leave the page load trigger as is and just add our blocking trigger to the tags where we want the query strings to stop the tag.
The advantage of this method is that in a GTM container with many tags, you can easily add an additional block trigger to any tag without affecting other tags. This is better than with the first method, where we make changes to the general page view tag and all other tags with the same trigger are also affected.
To create the blocking trigger, follow the same steps as described above for the page view trigger.
After that, visit the tag that should not be executed on page views with parameters.
Then click on Exceptions in the trigger section and add the trigger as an exception.
You can hence continue to use e.g. a general page view trigger ("All Pages") as the regular trigger for the tag in combination with the exception. The tag will always be executed unless the condition with the query parameters in the exception trigger applies.
After that, click save and publish the GTM container.
How to block events other than page views based on query strings
It is also possible to block events other than page loads based on query parameters in the URL.
To do this, the type of the blocking trigger must be modified to respond to any type of event. Then, the modified blocking trigger can be used for all possible events in Google Tag Manager.
If the associated URL sent with the event contains the defined query parameter, the event will be blocked as stated.
To do this, change the previously created trigger and set the trigger type to "Custom event". As event name write .*
and check the box "Use regular expression match".
We use a Regex wildcard for the event name, because this way any event name will be accepted.
Now set this trigger on any tag if you don't want it to fire when the defined query parameters are in the URL.
Conclusion
In this article, you have now learned how to stop page views in Google Analytics through multiple methods.
The most important thing in this tutorial is to learn how to use negative conditions in GTM triggers or trigger exceptions (blocking triggers).
If Regex was new to you, I recommend taking a closer look at it, as it is a great help for data filtering and is supported in a large number of tools 🤓.