1/* global document: true */ 2/* global window: true */ 3/* global console: true */ 4/* exported smileTracker */ 5 6var smileTracker = (function () { 7 8 "use strict"; 9 10 var guid = (function() { 11 function s4() { 12 return Math.floor((1 + Math.random()) * 0x10000) 13 .toString(16) 14 .substring(1); 15 } 16 return function() { 17 return s4() + s4() + '-' + s4() + '-' + s4() + '-' + 18 s4() + '-' + s4() + s4() + s4(); 19 }; 20 }()); 21 22 function getCookie(cookieName) { 23 var name = cookieName + "="; 24 var ca = document.cookie.split(';'); 25 for (var i = 0; i < ca.length; i++) { 26 var c = ca[i]; 27 while (c.charAt(0) === ' ') c = c.substring(1); 28 if (c.indexOf(name) !== -1) return c.substring(name.length, c.length); 29 } 30 return null; 31 } 32 33 function setCookie(cookieName, cookieValue, expiresAt, path) { 34 var expires = "expires=" + expiresAt.toUTCString(); 35 document.cookie = cookieName + "=" + cookieValue + "; " + expires + "; path=" + path; 36 } 37 38 // Retrieve values for a param into URL 39 function getQueryStringParameterByName(name) { 40 var results = null; 41 42 if (name && name.replace) { 43 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); 44 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"); 45 results = regex.exec(window.location.search); 46 } 47 48 return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 49 } 50 51 // Add page title and page URL to the tracked variables 52 function addStandardPageVars() { 53 // Website base url tracking (eg. mydomain.com) 54 this.addPageVar("site", window.location.hostname); 55 56 // Page URL tracking (eg. home.html) 57 this.addPageVar("url", window.location.pathname); 58 59 // Page title tracking 60 this.addPageVar("title", document.title); 61 } 62 63 // Append GA campaign variable to the tracked variables 64 // Following URL variables are used : 65 // - utm_source 66 // - utm_campaign 67 // - utm_medium 68 function addCampaignVars() { 69 70 // GA variables to be fetched from URI 71 var urlParams = ['utm_source', 'utm_campaign', 'utm_medium', 'utm_term']; 72 73 urlParams.forEach(function (element) { 74 var paramName = element; 75 var paramValue = getQueryStringParameterByName(paramName); 76 if (paramValue) { 77 // Append the GA param to the tracker 78 this.addPageVar(paramName, paramValue); 79 } 80 }.bind(this)); 81 } 82 83 function addReferrerVars() { 84 if (document.referrer) { 85 var parser = document.createElement('a'); 86 parser.href = document.referrer; 87 this.addPageVar('referrer.domain', parser.hostname); 88 this.addPageVar('referrer.page', parser.pathname); 89 } 90 } 91 92 function addResolutionVars() { 93 this.addPageVar('resolution.x', window.screen.availWidth); 94 this.addPageVar('resolution.y', window.screen.availHeight); 95 return this; 96 } 97 98 function addMetaPageVars() { 99 var metaTags = document.getElementsByTagName('meta'); 100 for (var tagIndex = 0; tagIndex < metaTags.length; tagIndex++) { 101 if (metaTags[tagIndex].getAttribute('name')) { 102 var components = metaTags[tagIndex].getAttribute('name').split(':'); 103 if (components.length === 2 && components[0] === 'sct') { 104 var varName = components[1]; 105 this.addPageVar(varName, metaTags[tagIndex].getAttribute('content')); 106 } 107 } 108 } 109 } 110 111 function getTrackerUrl() { 112 113 initSession.bind(this)(); 114 115 if (this.trackerSent === false) { 116 addStandardPageVars.bind(this)(); 117 addReferrerVars.bind(this)(); 118 addCampaignVars.bind(this)(); 119 addMetaPageVars.bind(this)(); 120 addResolutionVars.bind(this)(); 121 } 122 123 var urlParams = []; 124 125 for (var currentVar in this.vars) { 126 if ({}.hasOwnProperty.call(this.vars, currentVar)) {
127 urlParams.push(currentVar + "=" + this.vars[currentVar]); 128 } 129 } 130 131 return this.baseUrl + "?" + urlParams.join('&'); 132 } 133 134 function setTrackerStyle(imgNode) { 135 imgNode.setAttribute('style', 'position: absolute; top: 0; left: 0; visibility: hidden;'); 136 } 137 138 // Send the tag to the remote server 139 // Append a transparent pixel to the body 140 function sendTag(forceCollect) { 141 if (this.trackerSent === false || forceCollect === true) { 142 var bodyNode = document.getElementsByTagName('body')[0]; 143 144 if (this.config && this.config.hasOwnProperty('sessionConfig')) { 145 var trackingUrl = getTrackerUrl.bind(this)(); 146 var imgNode = document.createElement('img'); 147 imgNode.setAttribute('src', trackingUrl); 148 imgNode.setAttribute('alt', ''); 149 setTrackerStyle(imgNode); 150 bodyNode.appendChild(imgNode); 151 this.trackerSent = true; 152 this.vars = {}; 153 } 154 } 155 } 156 157 // Append a variable to the page 158 function addVariable(varName, value) { 159 this.vars[varName] = encodeURIComponent(value); 160 return this; 161 } 162 163 function addSessionVar(varName, value) { 164 addVariable.bind(this)(transformVarName.bind(this)(varName, 'session'), value); 165 } 166 167 function addPageVar(varName, value) { 168 addVariable.bind(this)(transformVarName.bind(this)(varName , 'page'), value); 169 } 170 171 function transformVarName(varName, prefix) { 172 return prefix + "[" + varName.replace(/[.]/g, "][") + "]"; 173 } 174 175 function initSession() { 176 if (this.config && this.config.hasOwnProperty('sessionConfig')) { 177 var config = this.config.sessionConfig; 178 var expireAt = new Date(); 179 var path = config['path'] || '/'; 180 181 if (getCookie(config['visit_cookie_name']) === null) { 182 expireAt.setSeconds(expireAt.getSeconds() + parseInt(config['visit_cookie_lifetime'], 10)); 183 setCookie(config['visit_cookie_name'], guid(), expireAt, path); 184 } else { 185 expireAt.setSeconds(expireAt.getSeconds() + parseInt(config['visit_cookie_lifetime'], 10)); 186 setCookie(config['visit_cookie_name'], getCookie(config['visit_cookie_name']), expireAt, path); 187 } 188 189 if (getCookie(config['visitor_cookie_name']) === null) { 190 expireAt.setDate(expireAt.getDate() + parseInt(config['visitor_cookie_lifetime'], 10)); 191 setCookie(config['visitor_cookie_name'], guid(), expireAt, path); 192 } 193 194 addSessionVar.bind(this)('uid', getCookie(config['visit_cookie_name'])); 195 addSessionVar.bind(this)('vid', getCookie(config['visitor_cookie_name'])); 196 } 197 } 198 199 // Implementation of the tracker 200 var SmileTrackerImpl = function() { 201 this.vars = {}; 202 this.trackerSent = false; 203 }; 204 205 SmileTrackerImpl.prototype.sendTag = function () { 206 require(['domReady'], function(domReady) { domReady(sendTag.bind(this)); }.bind(this)); 207 } 208 209 SmileTrackerImpl.prototype.setConfig = function (config) { 210 this.config = config; 211 this.baseUrl = config.beaconUrl; 212 } 213 214 SmileTrackerImpl.prototype.addPageVar = function (varName, value) { 215 addPageVar.bind(this)(varName, value); 216 } 217 218 SmileTrackerImpl.prototype.addSessionVar = function (varName, value) { 219 addSessionVar.bind(this)(varName, value); 220 } 221 222 return new SmileTrackerImpl(); 223}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.