Google Tag Manager Power Tip: Getting The UTM Source | Articles


Sometimes a conversion tag only needs to be fired if a visitor was referred by a certain affiliate website or other source. For example: we run an affiliate program at Tradedoubler and we need to be sure that only the conversions made by visitors referred by Tradedoubler are attributed as a conversion in Google AdWords.

This data used to be available in Google Analytics’ UTMZ cookie. Since Google Universal Analytics was introduced, the UTMZ cookie is no longer available. We can solve this by creating a custom tag that saves the utm source in the browser’s local storage and a custom variable that makes the saved utm source accessible in our triggers.


The goal is to create a custom script that checks if there’s a utm_source attribute in the URL on every page load. If so, the script saves the value in the browser’s local storage. We also need to create a custom javascript variable that will contain the value we stored so we can use this variable in the trigger for our conversion tag to check if the visitor was referred by Tradedoubler.


  • In Google Tag Manager, go to Tags and click New
  • Choose the Custom HTML Tag type
  • Give the tag a name like UTM Source Helper
  • In the Configure Tag field, paste the following script

<script type="text/javascript">
/// get the url and the key to find
var url = window.location.href;
var keyToFind = 'utm_source';
/// is there a utm source ?
if(url.indexOf(keyToFind + '=') > -1)
var valueFound = url.substr(url.indexOf(keyToFind + '=') + keyToFind.length + 1).split('&')[0];
localStorage.setItem(keyToFind, valueFound);     


  • Click Continue
  • Choose All Pages as trigger
  • Click Create Tag


Next we need to create the variable that will contain the value of UTM Source

  • Go to Variables and click New
  • Select Custom JavaScript as the new tag’s type
  • Name the variable UTM Source
  • Paste the following script

return localStorage.getItem('utm_source');

  • Click Create Variable


The variable UTM Source is now available and will contain the last UTM Source value. Now we will be able to create a conversion tag that only fires on the thank you page and only when the visitor came through Tradedoubler.


  • Go to Tags and click New
  • Click Google AdWords
  • Choose AdWords Conversion Tracking
  • Set up the conversion tag as you normally would
  • In Fire On, choose More
  • Click New to create a new trigger
  • For Event choose Page View
  • Choose Page View as a trigger type and click Continue
  • Select Some Page Views
  • Select the rules that define your thank you page
  • Add a rule for the UTM Source variable to equal tradedoubler


  • Click Create Trigger
  • Click Create Tag

The conversion tag is created


You’re all done. Don’t forget to publish the changes. The conversion tag should only fire when the last UTM Source was Tradedoubler.


Author: David Peeters


Get in touch

Semetis | Rue de l'Escaut 122, 1080 Brussels - Belgium

Connect with us

Cookie Policy

This website uses cookies that are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By accepting this OR scrolling this page OR continuing to browse, you agree to our privacy policy.