Why

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.

Goal

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.

Howto

<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);     
}
</script>

utm1

 

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

function()
{
return localStorage.getItem('utm_source');
}

utm2

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.

 

utm3

The conversion tag is created

Result

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

Tags: