Integrating with Google Tag Manager

NOTE: This article specifically relates to Distil's Bot Defense for Web product. For information specific to Distil Bot Discovery, please refer to Deploying The Distil Bot Discovery Tag To Your Site.

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. Log in 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;





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:


window.onbeforeunload = function(e) {

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






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?
1 out of 1 found this helpful
Have more questions? Submit a request