In some cases, you want to capture http header information into a custom field. Here’s an example to save the referring URL.

If embedded this will be the URL of the landing page where the Decision Tree is embedded.

Say you have 3 URL’s with one Decision Tree. In this case you would have the need to determine which URL was accessed.

This can be accomplished with some JavaScript

Important: DT.setField function cannot be used on the last node for V2 DTs.

Step-by-Step Instructions:

Step 1: First create a text custom field. We can call this ‘referrer’

Step 2: Next add this code to the first node of the Decision Tree. It should fire on enter of the 1st node.

<script>
var x = document.referrer;
console.log(x);
DT.setField('referrer', x);
</script>

Step 3: Now you can print the custom field like so: {referrer}

How to Save the Referrer & URL Parameters into a Custom Field

Example: Say you want to save your referrer with url params like utm parameters…

https://www.mysite.com/?utm_source=FaceBook&utm_campaign=NewLeadGenCampaign

Please note: When testing be sure to click a button’link on another page with url parameters so you can capture the referrer. Directly testing your decision tree does not contain a referrer.

Then use the script below the image in your first node…

<script>
//Get the query string
const queryString = window.location.search;

//Print the whole query string
//console.log(queryString);

//Split the name/value pairs into an array 
var params = queryString.split('&');

//Remove the leading ? from the first element in the array
var firstElement = params[0];
params[0] = firstElement.replace("?","");

//Print the key/value pairs ( parameters ) in the array
for ( i=0; i< params.length; i++){
  console.log(params[i]);
}
  var referrerParams = document.referrer+queryString;
  console.log(referrerParams);
  DT.setField('referrer_w_params', referrerParams);
  
</script>