In this article you’ll learn about the scripting power of LeadsHook

Your biggest gains in marketing often come from testing new or even what appears as a ‘dumb’ idea. Sometimes the standard features won’t allow you to execute your test ideas.

This is where you are use scripting capability of LeadsHook to implement pretty much anything you want.

You can add scripts in a number of different places inside LeadsHook

  1. Global Scripts: This is where you want to place scripts that you are going to reuse on other Decision Trees.
  2. Decision Tree Scripts: This is where you want to place scripts that you only want to apply to a certain Decision Tree.
  3. Node Scripts: This is where you want to place scripts that you only want to trigger on a specific Node.
  4. Custom Pages: This works the same way as Node level scripts, however you do have the flexibility of fully customizing this Node along with HTML and CSS.

Further details coming soon.

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

Here are a few examples of script usage:

EVENT_TIME: Add Node Level Script

<script>
var t = Math.floor(Date.now()/1000); // Unix timestamp in Seconds
DT.setField('event_time',t); // epoch time in custom field
</script>

EVENT_ID: Add Node Level Script

<script>
var t = Math.floor(Date.now()); // Unix timestamp in milliseconds
DT.setField('event_id',t); // epoch time in custom field
</script>

EVENT_SOURCE_URL: Add Node Level Script

<script>
var dtUrl = location.href.split('?')[0]
DT.setField('event_url', dtUrl)
</script>

CLIENT_USER_AGENT: Add Node Level Script

<script>
DT.setField('user_agent', navigator.userAgent)
</script>

ADD COOKIE VALUES INTO CUSTOM FIELDS

<script>
// A custom function to get cookies
function getCookie (name) {
  // Split cookie string and get all individual name=value pairs in an array
  var cookieArr = document.cookie.split(';')
  // Loop through the array elements
  for (var i = 0; i < cookieArr.length; i++) {
    var cookiePair = cookieArr[i].split('=')
    /* Removing whitespace at the beginning of the cookie name
            and compare it with the given string */
    if (name == cookiePair[0].trim()) {
      // Decode the cookie value and return
      return decodeURIComponent(cookiePair[1])
    }
  }
  // Return null if not found
  return null
}
if ( getCookie('_fbc') !== null ) {
  DT.setField('fbc', getCookie('_fbc'))
}
                                       
</script>

PLEASE NOTE: Your phone number field you’re using in your decision tree will almost always be type:mobile but the cleaned phone field, phone_number_clean is TEXT. Phone number formatting is getting removed so you only need a text field.

<script>
 var number = '{phone_number|raw}';
 
//Print the initial value on the browser console
 console.log(number);
 
//Remove white spaces
 number = number.replace(/\s/g, '');
 
//Remove brackets
 number = number.replace(/[()]/g, '');
 
//Remove hyphens
 number = number.replace(/-/g, '');
  
//Remove special characters
 number = number.replace(/[^a-zA-Z0-9 ]/g, '');
  
//Set custom field {variable} with the new number
 DT.setField('phone_number_clean', number);
 
//Print the initial value on the browser console
 console.log(number);
 </script>

HASHING SCRIPT

Credit: Code below adopted from this page >> https://stackoverflow.com/a/48161723

TIP! Use this testing tool to confirm your hashed values: https://emn178.github.io/online-tools/sha256.html

<script>
async function sha256(message) {
    // encode as UTF-8
    const msgBuffer = new TextEncoder().encode(message);                    
    // hash the message
    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);

async function sha256(message) {
  // encode as UTF-8
  const msgBuffer = new TextEncoder().encode(message);
  // hash the message
  const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
  // convert ArrayBuffer to Array
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  // convert bytes to hex string
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  return hashHex;
}

async function setVars(){
  var hash = await sha256("{first_name:lowercase}");
  DT.setField('first_name_hashed', hash)
  var hash = await sha256("{last_name:lowercase}");
  DT.setField('last_name_hashed', hash)
  var hash = await sha256("{email:lowercase}");
  DT.setField('email_hashed', hash)
  var hash = await sha256("{phone_number_clean}");
  DT.setField('phone_number_hashed', hash)
}
setVars()
</script>