Shelley Bassett



Use htaccess to edit a query in a url

Removing MailChimp tags from a URL

May 14, 2018

MailChimp tags are great for some tracking, but when it's recording each visitor as a new page and making a mess of your analytics, it's time to strip the tags from your urls.

MailChimp tags have been giving me a headache for the last week. So I don’t spam you, my mailing list is broken into segments, and subscribers can opt in or out of what they receive. And this was working really well… Until the Fire Nation (here, MailChimp) attacked.

I like MailChimp. Their UI is intuitive, they’re easy to set up, and they let me send new blogs out automatically to subsections of subscribers. But this last feature is really buggy, and the only help there is for it was “the devs are working on this”.

The devs are working to remove the MailChimp tags... sometime
The devs are working to remove the MailChimp tags… sometime

And I’m normally okay with this. But the way they are adding tracking tags into my emails is ruining my Google Analytics. And for a data-nerd like me, this was devastating. Each visitor from an email was showing up as a new page, so I had no way of working out how many views each blog was receiving.

google analytics pages report
GA pages report, showing multiple pages for the same blog

If you’re looking for a quick solution to this for an Apache server, it can be found here.

Good Tags and Bad Tags

For email tracking, Google recognises UTM tags, which allow me to pass parameters like my campaign name, or your specific user ID. Because these are standard tags, I can leave them in a url query and Google Analytics will ignore them when processing my data.

MailChimp also uses tracking tags to follow what a user does on a site. These are meant to be used exclusively for ecommerce, but due to some technical fault they are being added to all RSS campaigns too.

The MailChimp tags causing a mess in my analytics are mc_cid and mc_eid. Both of these will need to be removed, but I want to leave the UTM tags so that Google Analytics can accurately report on my traffic.

Removing MailChimp Tags

Following these instructions will prevent all MailChimp tags from loading on your site. To continue using them for ecommerce campaigns, it might be best to also check for a page name and only remove them from specific pages (ie; /blog/).

Solving this issue requires some coding knowledge and access to the .htaccess file. This is a Apache server file, and we’re using it to control redirects using regular expressions (regex). Regular expressions can be tricky, so if you’re not familiar with them, either use the code as-is, or ask someone with a little bit of experience to help.

For my website, the following code in my .htacess file removed the MailChimp tags, but left the UTM tracking tags.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} /?(.*)&mc_cid.*$ 
RewriteRule (.*) $1?%1 [R=301,L]

If you already have lines in your .htaccess file, this section goes at the top. If the mod_rewrite tag exists, place this inside the existing tag, again at the top.

The Code Explained

So. This little snippet is actually quite simple. Using the mod_rewrite  function, first check that we can rewrite things (RewriteEngine On). Then, set the base to our current url (RewriteBase /). Then come the tricky part.

Because tracking tags are passed as a query (come after a “?” in a URL), we can simple grab the entire string with RewriteCond %{QUERY_STRING}. The string edit is reasonably simple, because we only need to remove one tag, then everything after it. /?(.*)  means every character in the query. &mc_cid  is the tracking tag we’re looking for. .*$  is everything from this tag to the end of the string. The first part is in parenthesis so we can call this in a later line.

The RewriteRule  is the line that makes the new URL. This line is only executed if the RewriteCond  is met, meaning that it won’t redirect traffic that doesn’t have the mc_cid  parameter.

My RewriteRule  takes every character in the URI and saves it using (.*) . Then, we can rewrite the URL. Using the existing URI and adding the previously saved query give us $1?%1. $1  is the first variable on this line. %1  is the first variable on the previous line.

Finally, [R=301,L] tells the browser this is a permanent (301) redirection and the old URL doesn’t exist. The L tag tells Apache to stop editing the URL.

Redirecting Traffic

That’s it! Anything with a MailChimp tag will have the tag removed before Google Analytics can register the traffic. Its not ideal, and if you want ecommerce tracking on shop pages you’ll need to custom remove them from the rewrite rules. But until MailChimp fixes the bug, this is the solution I’m using.

Got a neater method? Leave a comment with your thoughts.