Linus Larsson

Create custom UTM parameter names

Tired of having to type the entire UTM string each time you want to track a link? With this setup in Google Tag Manager you can create your own names for the UTM tracking parameters without losing the tracking of the standard ones. I'm gonna show you an example of why this is worth the time setting it up. With UTM tracking you need to type in the following when tracking a link:

https://lynuhs.com/?utm_source=linkedin&utm_medium=share&utm_campaign=new+article&utm_content=create+custom+utm+parameters

With this setup the same link would instead look as below:

https://lynuhs.com/?src=linkedin&md=share&cmp=new+article&con=create+custom+utm+parameters

Step 1

First off, we're gonna have to create some new variables. The following list of variables need to be created:

  • Query - All
  • Query - UTM - Source
  • Query - UTM - Medium
  • Query - UTM - Campaign
  • Query - UTM - Content
  • Query - UTM - Term
  • Query - Custom Source
  • Query - Custom Medium
  • Query - Custom Campaign
  • Query - Custom Content
  • Query - Custom Term

The first variable should include the entire query string as it is, i.e. create a query cariable and leave it empty. The next queries are the UTM queries. Create new query variables and type in the different UTM parameters in them (UTM source is shown below).

Now create the variables with the queries you want to use as UTM paramaters (the equivalent to utm_source is shown below).

Step 2

Now we have to create yet another set of variables. The reason for this is that we want to make sure that the UTM tracking works with BOTH the new parameters and the standard ones. To make sure of this we will create lookup tables for all the UTM parameters.

The lookup table uses the standard UTM parameter as input and checks if it exists or not. If it doesn't exist the returned value will be "undefined". As you can see in the image above the output in this case will be the value of our own custom UTM parameter. If this variable returns "undefined" as well then the UTM function will not be used, i.e. Google Analytics will pick the acquisition data. If the standard UTM parameter exists then the value of that parameter will be used, as you can see in the "Set Default Value" field.

Step 3

Now you have to add these newly created lookup tables to your Google Analytics tags. Hopefully you have created a Google Analytics settings variable and in that case you only have to change that variable for this change to apply.

Step 4

As you can see in the image above, the page field has been altered with in order to remove the custom UTM parameters from the page dimension in Google Analytics. All you have to do to create this function is to create a custom JavaScript variable with the following code and apply it as shown in the image above.

function(){
 var q = {{Query - All}};
    var remove = ["src","md","cmp","con","trm"]; //change to your own UTM parameters
 
    var queries = q.split('&');
    var newQueries = [];
    
    for (n=0;n<queries.length;n++){
     var keep = true;
        for (i=0;i<remove.length;i++){
        var pos = queries[n].indexOf('=');
        if(queries[n].substring(0,pos) == remove[i]){
            keep = false;
           break;
        }
      } 
      if (keep){
       newQueries.push(queries[n]);
      }
    }
    
    if (q == '' || newQueries.length == 0){
     return "";
    }
    else{
       return "?" + newQueries.join('&').toLowerCase();
    }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Cookie Settings

© Copyright - Lynuhs.com - 2018-2024