{"id":4125,"date":"2021-06-14T14:26:45","date_gmt":"2021-06-14T14:26:45","guid":{"rendered":"https:\/\/www.leadshook.com\/help\/?p=4125"},"modified":"2024-02-14T11:40:12","modified_gmt":"2024-02-14T11:40:12","slug":"complete-guide-to-scripts","status":"publish","type":"post","link":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/","title":{"rendered":"Complete Guide to Scripts"},"content":{"rendered":"\n<p>In this article you&#8217;ll learn about the scripting power of LeadsHook<\/p>\n\n\n\n<p>Your biggest gains in marketing often come from testing new or even what appears as a &#8216;dumb&#8217; idea.  Sometimes the standard features won&#8217;t allow you to execute your test ideas.<\/p>\n\n\n\n<p>This is where you are use scripting capability of LeadsHook to implement pretty much anything you want.  <\/p>\n\n\n\n<p>You can add scripts in a number of different places inside LeadsHook<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Global Scripts: This is where you want to place scripts that you are going to reuse on other Decision Trees.<\/li>\n\n\n\n<li>Decision Tree Scripts: This is where you want to place scripts that you only want to apply to a certain Decision Tree.<\/li>\n\n\n\n<li>Node Scripts: This is where you want to place scripts that you only want to trigger on a specific Node.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<p>Further details coming soon.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background\"><strong>Important:<\/strong> DT.setField function cannot be used on the last node for V2 DTs.<\/p>\n\n\n\n<p>Here are a few examples of script usage:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>EVENT_TIME<\/strong>: Add Node Level Script<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nvar t = Math.floor(Date.now()\/1000); \/\/ Unix timestamp in Seconds\nDT.setField('event_time',t); \/\/ epoch time in custom field\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">EVENT_ID: Add Node Level Script<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nvar t = Math.floor(Date.now()); \/\/ Unix timestamp in milliseconds\nDT.setField('event_id',t); \/\/ epoch time in custom field\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">EVENT_SOURCE_URL: Add Node Level Script<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nvar dtUrl = location.href.split('?')&#91;0]\nDT.setField('event_url', dtUrl)\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">CLIENT_USER_AGENT: Add Node Level Script<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nDT.setField('user_agent', navigator.userAgent)\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>ADD COOKIE VALUES INTO CUSTOM FIELDS<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n\/\/ A custom function to get cookies\nfunction getCookie (name) {\n  \/\/ Split cookie string and get all individual name=value pairs in an array\n  var cookieArr = document.cookie.split(';')\n  \/\/ Loop through the array elements\n  for (var i = 0; i &lt; cookieArr.length; i++) {\n    var cookiePair = cookieArr&#91;i].split('=')\n    \/* Removing whitespace at the beginning of the cookie name\n            and compare it with the given string *\/\n    if (name == cookiePair&#91;0].trim()) {\n      \/\/ Decode the cookie value and return\n      return decodeURIComponent(cookiePair&#91;1])\n    }\n  }\n  \/\/ Return null if not found\n  return null\n}\nif ( getCookie('_fbc') !== null ) {\n  DT.setField('fbc', getCookie('_fbc'))\n}\n                                       \n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p><strong>PLEASE NOTE: Your phone number field you\u2019re using in your decision tree will almost always be type:mobile but the cleaned phone field, phone_number_clean is TEXT.&nbsp;Phone number formatting is getting removed so you only need a text field.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n var number = '{phone_number|raw}';\n \n\/\/Print the initial value on the browser console\n console.log(number);\n \n\/\/Remove white spaces\n number = number.replace(\/\\s\/g, '');\n \n\/\/Remove brackets\n number = number.replace(\/&#91;()]\/g, '');\n \n\/\/Remove hyphens\n number = number.replace(\/-\/g, '');\n  \n\/\/Remove special characters\n number = number.replace(\/&#91;^a-zA-Z0-9 ]\/g, '');\n  \n\/\/Set custom field {variable} with the new number\n DT.setField('phone_number_clean', number);\n \n\/\/Print the initial value on the browser console\n console.log(number);\n &lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">HASHING SCRIPT<\/h3>\n\n\n\n<p><em><strong>Credit<\/strong>: Code below adopted from this page &gt;&gt;&nbsp;<a href=\"https:\/\/stackoverflow.com\/a\/48161723\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/a\/48161723<\/a><\/em><\/p>\n\n\n\n<p><strong>TIP! Use this testing tool to confirm your hashed values:&nbsp;<a href=\"https:\/\/emn178.github.io\/online-tools\/sha256.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/emn178.github.io\/online-tools\/sha256.html<\/a><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\nasync function sha256(message) {\n    \/\/ encode as UTF-8\n    const msgBuffer = new TextEncoder().encode(message);                    \n    \/\/ hash the message\n    const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);\n\nasync function sha256(message) {\n  \/\/ encode as UTF-8\n  const msgBuffer = new TextEncoder().encode(message);\n  \/\/ hash the message\n  const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);\n  \/\/ convert ArrayBuffer to Array\n  const hashArray = Array.from(new Uint8Array(hashBuffer));\n  \/\/ convert bytes to hex string\n  const hashHex = hashArray.map(b =&gt; b.toString(16).padStart(2, '0')).join('');\n  return hashHex;\n}\n\nasync function setVars(){\n  var hash = await sha256(\"{first_name:lowercase}\");\n  DT.setField('first_name_hashed', hash)\n  var hash = await sha256(\"{last_name:lowercase}\");\n  DT.setField('last_name_hashed', hash)\n  var hash = await sha256(\"{email:lowercase}\");\n  DT.setField('email_hashed', hash)\n  var hash = await sha256(\"{phone_number_clean}\");\n  DT.setField('phone_number_hashed', hash)\n}\nsetVars()\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article you&#8217;ll learn about the scripting power of LeadsHook Your biggest gains in marketing often come from testing new or even what appears as a &#8216;dumb&#8217; idea. Sometimes the standard features won&#8217;t allow you to execute your test ideas. This is where you are use scripting capability of LeadsHook to implement pretty much [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,620],"tags":[],"class_list":["post-4125","post","type-post","status-publish","format-standard","hentry","category-decision-tree","category-scripts"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Complete Guide to Scripts - LeadsHook Knowledge Base<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Complete Guide to Scripts - LeadsHook Knowledge Base\" \/>\n<meta property=\"og:description\" content=\"In this article you&#8217;ll learn about the scripting power of LeadsHook Your biggest gains in marketing often come from testing new or even what appears as a &#8216;dumb&#8217; idea. Sometimes the standard features won&#8217;t allow you to execute your test ideas. This is where you are use scripting capability of LeadsHook to implement pretty much [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/\" \/>\n<meta property=\"og:site_name\" content=\"LeadsHook Knowledge Base\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/leadshook\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-14T14:26:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-14T11:40:12+00:00\" \/>\n<meta name=\"author\" content=\"Nik T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@leadshook\" \/>\n<meta name=\"twitter:site\" content=\"@leadshook\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nik T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/\"},\"author\":{\"name\":\"Nik T\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/person\\\/4bfe37d814563cc729828b7055313f4d\"},\"headline\":\"Complete Guide to Scripts\",\"datePublished\":\"2021-06-14T14:26:45+00:00\",\"dateModified\":\"2024-02-14T11:40:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/\"},\"wordCount\":298,\"publisher\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\"},\"articleSection\":[\"Decision Tree\",\"Scripts\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/\",\"name\":\"Complete Guide to Scripts - LeadsHook Knowledge Base\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#website\"},\"datePublished\":\"2021-06-14T14:26:45+00:00\",\"dateModified\":\"2024-02-14T11:40:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/complete-guide-to-scripts\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Complete Guide to Scripts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#website\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\",\"name\":\"LeadsHook Knowledge Base\",\"description\":\"LeadsHook Training &amp; Technical Help\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#organization\",\"name\":\"LeadsHook\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/leadshook-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/wp-content\\\/uploads\\\/2018\\\/12\\\/leadshook-logo.png\",\"width\":350,\"height\":83,\"caption\":\"LeadsHook\"},\"image\":{\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/leadshook\\\/\",\"https:\\\/\\\/x.com\\\/leadshook\",\"https:\\\/\\\/www.linkedin.com\\\/showcase\\\/leadshook\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/#\\\/schema\\\/person\\\/4bfe37d814563cc729828b7055313f4d\",\"name\":\"Nik T\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g\",\"caption\":\"Nik T\"},\"sameAs\":[\"http:\\\/\\\/www.LeadsHook.com\"],\"url\":\"https:\\\/\\\/www.leadshook.com\\\/help\\\/author\\\/nik\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Complete Guide to Scripts - LeadsHook Knowledge Base","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/","og_locale":"en_US","og_type":"article","og_title":"Complete Guide to Scripts - LeadsHook Knowledge Base","og_description":"In this article you&#8217;ll learn about the scripting power of LeadsHook Your biggest gains in marketing often come from testing new or even what appears as a &#8216;dumb&#8217; idea. Sometimes the standard features won&#8217;t allow you to execute your test ideas. This is where you are use scripting capability of LeadsHook to implement pretty much [&hellip;]","og_url":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/","og_site_name":"LeadsHook Knowledge Base","article_publisher":"https:\/\/www.facebook.com\/leadshook\/","article_published_time":"2021-06-14T14:26:45+00:00","article_modified_time":"2024-02-14T11:40:12+00:00","author":"Nik T","twitter_card":"summary_large_image","twitter_creator":"@leadshook","twitter_site":"@leadshook","twitter_misc":{"Written by":"Nik T","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/#article","isPartOf":{"@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/"},"author":{"name":"Nik T","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/person\/4bfe37d814563cc729828b7055313f4d"},"headline":"Complete Guide to Scripts","datePublished":"2021-06-14T14:26:45+00:00","dateModified":"2024-02-14T11:40:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/"},"wordCount":298,"publisher":{"@id":"https:\/\/www.leadshook.com\/help\/#organization"},"articleSection":["Decision Tree","Scripts"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/","url":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/","name":"Complete Guide to Scripts - LeadsHook Knowledge Base","isPartOf":{"@id":"https:\/\/www.leadshook.com\/help\/#website"},"datePublished":"2021-06-14T14:26:45+00:00","dateModified":"2024-02-14T11:40:12+00:00","breadcrumb":{"@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.leadshook.com\/help\/complete-guide-to-scripts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.leadshook.com\/help\/"},{"@type":"ListItem","position":2,"name":"Complete Guide to Scripts"}]},{"@type":"WebSite","@id":"https:\/\/www.leadshook.com\/help\/#website","url":"https:\/\/www.leadshook.com\/help\/","name":"LeadsHook Knowledge Base","description":"LeadsHook Training &amp; Technical Help","publisher":{"@id":"https:\/\/www.leadshook.com\/help\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.leadshook.com\/help\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.leadshook.com\/help\/#organization","name":"LeadsHook","url":"https:\/\/www.leadshook.com\/help\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/logo\/image\/","url":"https:\/\/www.leadshook.com\/help\/wp-content\/uploads\/2018\/12\/leadshook-logo.png","contentUrl":"https:\/\/www.leadshook.com\/help\/wp-content\/uploads\/2018\/12\/leadshook-logo.png","width":350,"height":83,"caption":"LeadsHook"},"image":{"@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/leadshook\/","https:\/\/x.com\/leadshook","https:\/\/www.linkedin.com\/showcase\/leadshook\/"]},{"@type":"Person","@id":"https:\/\/www.leadshook.com\/help\/#\/schema\/person\/4bfe37d814563cc729828b7055313f4d","name":"Nik T","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/48d53e68db0b1db1cd5c54a750130fb2ee0c663306ff1bd434373f1a368ef7a0?s=96&d=mm&r=g","caption":"Nik T"},"sameAs":["http:\/\/www.LeadsHook.com"],"url":"https:\/\/www.leadshook.com\/help\/author\/nik\/"}]}},"_links":{"self":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/comments?post=4125"}],"version-history":[{"count":3,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4125\/revisions"}],"predecessor-version":[{"id":5942,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/posts\/4125\/revisions\/5942"}],"wp:attachment":[{"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/media?parent=4125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/categories?post=4125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.leadshook.com\/help\/wp-json\/wp\/v2\/tags?post=4125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}