Remove Google Analytics utm Query String with nginx

Using Google Analytics to gain traffic insights for your WordPress or WooCommerce site can track visitors and conversion rates. If you use tracking for email blasts when you publish new posts or product deals, you may find visitors get a slower experience. Google Analytics adds a query string that looks something like ?utm_source=mailchimp which bypasses any caching you may have enabled. Google uses this query string for tracking, however, Google tracks using client side javascript which means we can remove this Google Analytics query string, deliver faster pages to clients and still track using Google Analytics. This will fix your website being slow when you send out email campaigns with Analytics tracking enabled since your cache will actually be used instead.

This guide is for removing Google Analytics query strings with nginx, the Apache .htaccess version is here. The Varnish version will be published in the future.

If you use Google Adwords then see the guide about removing the gclid query string too.

Speed Tests with and without Google utm Query String

Using pingdom, the front page of this site loads in 339 ms without the query string

When repeating the speed test adding the ?utm=source=mailchimp query string loads in 1.01 seconds because the cache is bypassed.

Read on to learn how to remove the Google Analytics tracking query string using nginx rewrites for WordPress.

This could affect any marketing tracking scripts you are using, if you need tracking scripts to work with your nginx setup please get in touch with me here to discuss possibilities

Remove Google Analytics utm Query String for nginx

In your nginx virtual host file, usually in /etc/nginx/sites-available or /etc/nginx/vhosts and add this snippet

#remove utm query string
if ($args ~* "utm_") {
    #? in uri? drops the utm query string
    rewrite ^(.*)$ $uri? permanent;

Save the virtual host and then verify the nginx configuration syntax is OK.

sudo nginx -t

Reload the nginx service if there were no syntax errors.

sudo service nginx reload

Now when you test a URL with the ?utm_source query string you will see it is removed.


Is Google Analytics executed server side or client side?
nginx Strip Query String Rewrite
Remove Parameters with nginx rewrites
nginx http rewrite module

3 thoughts on “Remove Google Analytics utm Query String with nginx”

  1. Hi Mike,
    Doesn’t removing the query string on the server side beat the purpose of using campaign tracking in the first place?

    Yes GA will track the session but not the campaign details, eg which email the user came from, what link they clicked on etc. For example, it will make it impossible to track and A/B test email campaigns.

    Same goes for tracking of ad campaigns.

    Am I missing something?

    Is seems to be possible to build and serve cached pages, ignoring the query strings, yet maintaining the query string to be used for javascript tracking. example 1, example 2

    Wouldn’t that be a better but admittedly more complicated solution?

    • Hi Kostas, the analytics script communication goes from client browser to Google’s servers. Unless there is some sort of more in-depth tracking requiring the server’s involvement then there should be no ill effects. I have used Varnish before to generalize gclid query strings rather than random hashes, his way you can substitute the gclid random hash with a standard string per ad campaign which is cached 🙂

Comments are closed.