Follow

Integrating with Google Tag Manager

Some customers use Google Tag Manager (GTM) to control the deployment of analytics tools. For example, it’s useful in fetching and customizing the Google Universal Analytics code. Customers onboarding with Distil should configure their GTM to preserve the initial referrer value.

Before you begin, make sure you’ve integrated Google Analytics with GTM.

  1. Login to GTM.
  2. Create a new GTM variable. You will use the value returned from the custom JavaScript variable, Set-Distil-Referrer, to replace the default referer value.
    1. Click Variables from the left menu.

    2. Under User-Defined Variables, click NEW.

    3. Select Custom JavaScript from the Choose variable type menu.

    4. Name the variable Set-Distil-Referrer.

    5. Paste the following code in the Custom Javascript field:

function() {

if (typeof sessionStorage !== 'undefined'){

   var distilReferrer = sessionStorage.getItem('distil_referrer');

   if (distilReferrer) {

       return distilReferrer;

   }

   // session variable 'distil_referrer' gets deleted on unload

}

var referrer = document.referrer;

// prevent self referrals

if (document.referrer) {

   var regex = new URL(document.referrer).hostname;

   if ("{{Page Hostname}}".match(regex)) {

       referrer = null;

   }

}

}

Example:

6. Set Firing Triggers to All Pages.

7. Click Save.

3. Set the referrer to {{Set-Distil-Referrer}} (defined in step 2) in your Universal Analytics tag.

4. Create a new tag to remove the distil_referrer object from sessionStorage:

    1. Click Tags from the left menu.

    2. Select Custom HTML.
    3. Paste the following code into the HTML field:

<script>

window.onbeforeunload = function(e) {

if (typeof sessionStorage !== 'undefined'){

   sessionStorage.removeItem('distil_referrer');

   }

};

</script>

Example:

4. Set Firing Triggers to All Pages.

5. Click Save and preview your changes. If 

6. Preview your changes. If there are no errors or other issues, proceed to step 7. If there are errors or other issues, return to draft any changes and click Save again. 

7. Click Publish.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments