Remove Google Analytics utm_source Query String in .htaccess

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 Apache and .htaccess, see the nginx version here, the Varnish version will be published in the future.

Speed Tests with and without Google Analytics Query String

Using pingdom, the front page of this site loads in 632 ms without the Google Analytics query string

When repeating the speed test adding the ?utm_source=mailchimp Google Analytics tracking query string loads in 1.01 seconds because the cache is bypassed.

Read on to learn how to remove the Google Analytics query string using Apache’s .htaccess file for WordPress.

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

Remove Google Analytics Query String in .htaccess

Make this the first rule in your .htaccess file. It captures any query string containing utm and removes it (the trailing ? after $1 along with QSD)

# BEGIN Google Analytics utm query string remove
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "utm" [NC]
RewriteRule (.*) /$1? [R=301,L,QSD]
<IfModule mod_rewrite.c>
# END Google Analytics utm query string remove

Add the previous rule before your WordPress rules which usually look like this

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Test by going to any of your landing pages and adding the suffix query string ?utm_source=va134asf81q81 and you will see it get removed.


Is Google Analytics executed server side or client side?
Rewrite URL to Remove gclid Query
Removing Query Strings from End of URL
Strip Adwords with Varnish
Remove Query String htaccess with QSA

1 thought on “Remove Google Analytics utm_source Query String in .htaccess”

  1. > Test by going to any of your landing pages and adding the suffix query string ?gclid=va134asf81q81 and you will see it get removed.

    This doesn’t seem quite right. The htaccess is only removing `utm` and not `glicd`. Anyway, how does this even work? The utm is being removed before GA is loaded on the page and can even ingest the utm parameters. If GA was setup up server side (which is possible) wouldn’t this make more sense?

    I was trying to do this with WooCommerce coupons that I get from the query string and apply programmatically but it seems the server is too fast. Is there a way to delay the removal? If that makes sense..

Comments are closed.