1

I had to add some ugly links in my website that looks like: "/page/?utm_source=value&utm_source=value2&utm_source=value3...." and I would like to keep the URLs clean. I found this directives that I tried, it does redirect but the tracker doesn't work coz it's not passed to GA.

RewriteCond %{QUERY_STRING} ^((.*?)&|)utm_
RewriteRule ^(.*)$ /$1?%2 [R=301,NE,L]

Is there another solution for this? Thanks

Websphere
  • 563
  • 8
  • 24

2 Answers2

0

It seems there is no clear way to do this. I checked couple of big websites using utm_campain and it stays after clicking the link, so even big websites consider this as fine.

But if one really wants to remove it, it can be done manually. Assuming that you have canonical address (which you should have if you care about SEO), see some PHP-pseudocode:

function cleanupUtm($currentCanonicalUrl)
{
    $currentUrl = $_SERVER['REQUEST_URI'];
    // Check are there "utm_" parameters in current URL
    if (strpos($currentUrl, 'utm_') === false) {
        return;
    }
    redirect($currentCanonicalUrl);
}

function redirect($location)
{
    // Some helper redirect function, see https://stackoverflow.com/a/768472/1657819
    header("Location: " . $location);
    die();
}

But obviously it will trigger extra redirect and page reload.

Alternative

As an alternative, you may consider using Google Tag Manager to replace long boring URL with campaign to some neat URL like https://example.com/#instagram. See for example this article

While it looks neat (and much better than long utm... string), I feel that it gives you less flexibility (or you will end up with hundreds of tags for all the cases you need)

The Godfather
  • 4,235
  • 4
  • 39
  • 61
-1

Google analytics by default removes the UTM parameters from pages report but if you want to remove any other parameter you can go to admin > view setting > Exclude URL Query Parameters

You can learn a bit more about it here https://support.google.com/analytics/answer/1010249?hl=en

Hope this help

Analytics Ml
  • 588
  • 3
  • 5
  • 1
    it's actually not about removing and excluding from analytics, i would like to remove it from the URL to keep it short and clean! – Websphere Sep 02 '16 at 08:53
  • you can either check Google url builder for using utm_parameters https://support.google.com/analytics/answer/1033867?hl=en – Analytics Ml Sep 05 '16 at 08:20
  • 1
    I'm not looking to build the URL! I want to remove the query string from the URL but keep the tracking – Websphere Sep 06 '16 at 12:56
  • hi, if you remove the query strings you would not be able to track the parameters i.e source, campaign etc. Otherwise if you don't want to track anything you just check the source of the url and change the link to without embedding utm parameters. http://utm.io/ does embed the query parameters but keeps the url short – Analytics Ml Sep 07 '16 at 07:45
  • Hi, I've seen in some website that the link contains all the query string with _utm... but once landed on the page, there is a redirection to the same page without the querystring. so how do they do?? – Websphere Sep 07 '16 at 10:52