SHARE
TWEET

Untitled

a guest Nov 17th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setCookie(cookieName, cookieValue, expirationTime) {
  2.     expirationTime = expirationTime * 1000;
  3.     var date = new Date();
  4.     var dateTimeNow = date.getTime();
  5.  
  6.     date.setTime(dateTimeNow + expirationTime);
  7.     var date = date.toUTCString();
  8.     document.cookie =
  9.       cookieName +
  10.       "=" +
  11.       cookieValue +
  12.       "; expires=" +
  13.       date +
  14.       "; path=/; domain=." +
  15.       domain;
  16. }
  17.  
  18. function getCookie(name) {
  19.     var value = "; " + document.cookie;
  20.     var parts = value.split("; " + name + "=");
  21.     if (parts.length == 2)
  22.       return parts
  23.         .pop()
  24.         .split(";")
  25.         .shift();
  26.     return null;
  27. }
  28.  
  29. function getParameterByName(name) {
  30.   var url = window.location.href;
  31.   name = name.replace(/[\[\]]/g, "\\$&");
  32.   var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  33.       results = regex.exec(url);
  34.   if (!results) return null;
  35.   if (!results[2]) return "";
  36.   return decodeURIComponent(results[2].replace(/\+/g, " ")) || null;
  37. }
  38.  
  39. var httpReferrer = document.referrer || null;
  40.  
  41. var finalSource;
  42. var utm_source = getParameterByName("utm_source");
  43. var utm_medium = getParameterByName("utm_medium");
  44. var gclid = getParameterByName("gclid");
  45. var domain = "bird-office.com";
  46. var EXPIRATE_DATE_COOKIE = 5184000;
  47.  
  48. function getEmailSource(utm_medium, utm_source) {
  49.   if (
  50.     utm_medium === "email" ||
  51.     utm_source === "email" ||
  52.     utm_source === "hs_email"
  53.   ) {
  54.     return "Email marketing";
  55.   }
  56. }
  57.  
  58. function getReferrerSource(referrer) {
  59.   var source = null;
  60.   if (referrer) {
  61.     if (referrer.indexOf("google") !== -1) {
  62.       source = "Google Organic search";
  63.     } else if (referrer.indexOf("bing") !== -1) {
  64.       source = "Microsoft Organic Search";
  65.     } else if (referrer.indexOf("twitter") !== -1) {
  66.       source = "Twitter Organic";
  67.     } else if (referrer.indexOf("facebook") !== -1) {
  68.       source = "Facebook Organic";
  69.     } else if (referrer.indexOf("linkedin") !== -1) {
  70.       source = "Linkedin Organic";
  71.     } else if (referrer.indexOf("blog.bird-office.com") !== -1) {
  72.       source = "Blog";
  73.       var regexBlogArticle = /https:\/\/blog\.bird-office\.com\/blog\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([a-z\-]*)\/?/gm;
  74.       if (regexBlogArticle.test(referrer)) {
  75.         var blogToken = referrer.split("/");
  76.         setCookie(
  77.           "leadBlogArticle",
  78.           blogToken[blogToken.length - 2],
  79.           EXPIRATE_DATE_COOKIE
  80.         );
  81.       }
  82.     } else {
  83.       source = "Referrals";
  84.       setCookie("leadReferrer", httpReferrer, EXPIRATE_DATE_COOKIE);
  85.     }
  86.   }
  87.   return source;
  88. }
  89.  
  90. function getEventSource(utm_medium) {
  91.   var source = null;
  92.   if (utm_medium === "event") {
  93.     source = "Event";
  94.   }
  95.   return source;
  96. }
  97.  
  98. function getPaidSource(utm_medium, utm_source, gclid) {
  99.   var source;
  100.   if (utm_medium === "display") {
  101.     return "Google Display";
  102.   }
  103.   if (gclid) {
  104.     return "Google Paid search";
  105.   }
  106.   if (
  107.     utm_medium === "ppc" ||
  108.     utm_medium === "ad" ||
  109.     utm_medium === "cpc" ||
  110.     utm_medium === "paid"
  111.   ) {
  112.     switch (utm_source) {
  113.       case "adwords":
  114.       case "google":
  115.         source = "Google Paid search";
  116.         break;
  117.       case "microsoft":
  118.       case "bing":
  119.         source = "Microsoft Paid search";
  120.         break;
  121.       case "facebook":
  122.         source = "Facebook Ad";
  123.         break;
  124.       case "linkedin":
  125.         source = "Linkedin Ad";
  126.         break;
  127.       case "twitter":
  128.         source = "Twitter Ad";
  129.         break;
  130.     }
  131.     return source;
  132.   }
  133.   return null;
  134. }
  135. var paidSource = getPaidSource(utm_medium, utm_source, gclid);
  136. var referrerSource = getReferrerSource(httpReferrer);
  137. var emailSource = getEmailSource(utm_medium, utm_source);
  138. var eventSource = getEventSource(utm_medium);
  139.  
  140. finalSource =
  141.   paidSource ||
  142.   emailSource ||
  143.   eventSource ||
  144.   referrerSource ||
  145.   "Direct traffic";
  146.  
  147.  
  148. setCookie("leadLastSourceAttribution", finalSource, EXPIRATE_DATE_COOKIE);
  149.  
  150. var firstSourceAttribution = getCookie("leadFirstSourceAttribution");
  151. if (!firstSourceAttribution) {
  152.   setCookie("leadFirstSourceAttribution", finalSource, EXPIRATE_DATE_COOKIE);
  153. }
  154. var getMultiSourceLastUpdate = getCookie("leadMultiSourceLastUpdate");
  155. var multiSourceAttribution = getCookie("leadMultiSourceAttribution");
  156.  
  157. if (finalSource !== "Direct traffic") {
  158.   //No Cookie Multi Touch
  159.   if (!multiSourceAttribution) {
  160.     setCookie("leadMultiSourceLastUpdate", Date.now());
  161.     setCookie("leadMultiSourceAttribution", finalSource, EXPIRATE_DATE_COOKIE);
  162.     //Cookie Multi Touch And Multi Touch Timer is past
  163.   } else if (
  164.     multiSourceAttribution &&
  165.     Date.now() >= Number(getMultiSourceLastUpdate) + 60 * 30 * 1000
  166.   ) {
  167.     var splitMultiSource = multiSourceAttribution.split("|");
  168.     if (splitMultiSource.length < 10) {
  169.       setCookie(
  170.         "leadMultiSourceAttribution",
  171.         multiSourceAttribution + " | " + finalSource,
  172.         EXPIRATE_DATE_COOKIE
  173.       );
  174.       setCookie("leadMultiSourceLastUpdate", Date.now());
  175.     }
  176.   }
  177. }
  178.  
  179. if (getParameterByName("utm_term")) {
  180.   setCookie(
  181.     "leadUtmTerm",
  182.     getParameterByName("utm_term"),
  183.     EXPIRATE_DATE_COOKIE
  184.   );
  185. }
  186.  
  187. if (getParameterByName("utm_campaign")) {
  188.   setCookie(
  189.     "leadUtmCampaign",
  190.     getParameterByName("utm_campaign"),
  191.     EXPIRATE_DATE_COOKIE
  192.   );
  193. }
  194.  
  195. if (getParameterByName("utm_content")) {
  196.   setCookie(
  197.     "leadUtmContent",
  198.     getParameterByName("utm_content"),
  199.     EXPIRATE_DATE_COOKIE
  200.   );
  201. }
  202.  
  203. /* Inject variables */
  204. window.addEventListener('message', function(event) {
  205.   if(event.data.type === 'hsFormCallback' && event.data.eventName === 'onFormReady') {
  206.  
  207.     console.log("ready");
  208.     var event = new Event('change');
  209.     if(getCookie("leadUtmTerm")){
  210.       var utmTerm = document.getElementsByName("utm_term");
  211.       if(utmTerm && utmTerm.length > 0){
  212.         utmTerm[0].value = getCookie("leadUtmTerm");
  213.         utmTerm[0].dispatchEvent(event);
  214.       }
  215.     }
  216.  
  217.     if(getCookie("leadUtmContent")){
  218.       var utmContent = document.getElementsByName("utm_content");
  219.       if(utmContent && utmContent.length > 0){
  220.         utmContent[0].value = getCookie("leadUtmContent");
  221.         utmContent[0].dispatchEvent(event);
  222.       }
  223.     }
  224.  
  225.     if(getCookie("leadUtmCampaign")){
  226.       var utmCampaign = document.getElementsByName("utm_campaign");
  227.       if(utmCampaign && utmCampaign.length > 0){
  228.         utmCampaign[0].value = getCookie("leadUtmCampaign");
  229.         utmCampaign[0].dispatchEvent(event);
  230.       }
  231.     }
  232.  
  233.     if(getCookie("leadBlogArticle")){
  234.       var blogArticle = document.getElementsByName("lead_blog_article");
  235.       if(blogArticle && blogArticle.length > 0){
  236.         blogArticle[0].value = getCookie("leadBlogArticle");
  237.         blogArticle[0].dispatchEvent(event);
  238.       }
  239.     }
  240.  
  241.     if(getCookie("leadReferrer")){
  242.       var lastReferrer = document.getElementsByName("lead_source_referrer");
  243.       if(lastReferrer && lastReferrer.length > 0){
  244.         lastReferrer[0].value = getCookie("leadReferrer");
  245.         lastReferrer[0].dispatchEvent(event);
  246.       }
  247.     }
  248.  
  249.     if(getCookie("leadLastSourceAttribution")){
  250.       var leadLastSourceAttribution = document.getElementsByName("marketing_lead_source");
  251.       if(leadLastSourceAttribution && leadLastSourceAttribution.length > 0){
  252.         leadLastSourceAttribution[0].value = getCookie("leadLastSourceAttribution");
  253.         leadLastSourceAttribution[0].dispatchEvent(event);
  254.       }
  255.     }
  256.  
  257.  
  258.     if(getCookie("leadFirstSourceAttribution")){
  259.       var leadLastSourceAttributionFirst = document.getElementsByName("marketing_lead_source_first");
  260.       if(leadLastSourceAttributionFirst && leadLastSourceAttributionFirst.length > 0){
  261.         leadLastSourceAttributionFirst[0].value = getCookie("leadLastSourceAttribution");
  262.         leadLastSourceAttributionFirst[0].dispatchEvent(event);
  263.       }
  264.     }
  265.  
  266.     if(getCookie("leadMultiSourceAttribution")){
  267.       var leadLastSourceAttributionMulti = document.getElementsByName("marketing_lead_source_multi");
  268.       if(leadLastSourceAttributionMulti && leadLastSourceAttributionMulti.length > 0){
  269.         leadLastSourceAttributionMulti[0].value = getCookie("leadLastSourceAttribution");
  270.         leadLastSourceAttributionMulti[0].dispatchEvent(event);
  271.       }
  272.     }
  273.   }
  274. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top