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.

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]
</IfModule>
# END WordPress

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

Sources

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