/** * Indicative.js - v0.0.1 - Build Date: 2017-10-16 * https://www.indicative.com * * Copyright (c) 2017 Indicative * Indicative JavaScript library includes code from Google's json-sans-eval project * to decode a JSON string into a JavaScript object. * The json-sans-eval library is Copyright (c) 2008 Google Inc. and is available * at https://code.google.com/p/json-sans-eval/ */ !function(a,b){"function"==typeof define&&define.amd?define(b):a.Indicative=b()}(window,function(){"use strict";var a,b=!1,c="//api.indicative.com/service",d="/event",e="/alias",f=!1,g=!1,h=30,i=!1,j={cookiesOnMainDomain:"cookiesOnMainDomain",recordSessions:"recordSessions",sessionsThreshold:"sessionsThreshold"},k={encode:function(a){var b=[],c="[object Array]"===Object.prototype.toString.apply(a);for(var d in a){var e=a[d];if("object"==typeof e)b.push(c?this.encode(e):'"'+d+'":'+this.encode(e));else{var f="",g=d.replace(/[\\"']/g,"\\$&");if(c||(f='"'+g+'":'),"number"==typeof e)f+=e;else if(e===!1)f+="false";else if(e===!0)f+="true";else{var h=e.replace(/[\\"']/g,"\\$&");f+='"'+h+'"'}b.push(f)}}var i=b.join(",");return c?"["+i+"]":"{"+i+"}"},decode:function(){function a(a,b,c){return b?g[b]:String.fromCharCode(parseInt(c,16))}var b="(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)",c='(?:[^\\0-\\x08\\x0a-\\x1f"\\\\]|\\\\(?:["/\\\\bfnrt]|u[0-9A-Fa-f]{4}))',d='(?:"'+c+'*")',e=new RegExp("(?:false|true|null|[\\{\\}\\[\\]]|"+b+"|"+d+")","g"),f=new RegExp("\\\\(?:([^u])|u(.{4}))","g"),g={'"':'"',"/":"/","\\":"\\",b:"\b",f:"\f",n:"\n",r:"\r",t:" "},h="",i="\\";return function(b){var c,d=b.match(e),g=d[0];if("{"===g)c={};else{if("["!==g)throw new Error(g);c=[]}for(var j,k=[c],l=1,m=d.length;m>l;++l){g=d[l];var n;switch(g.charCodeAt(0)){default:n=k[0],n[j||n.length]=+g,j=void 0;break;case 34:if(g=g.substring(1,g.length-1),-1!==g.indexOf(i)&&(g=g.replace(f,a)),n=k[0],!j){if(!(n instanceof Array)){j=g||h;break}j=n.length}n[j]=g,j=void 0;break;case 91:n=k[0],k.unshift(n[j||n.length]=[]),j=void 0;break;case 93:k.shift();break;case 102:n=k[0],n[j||n.length]=!1,j=void 0;break;case 110:n=k[0],n[j||n.length]=null,j=void 0;break;case 116:n=k[0],n[j||n.length]=!0,j=void 0;break;case 123:n=k[0],k.unshift(n[j||n.length]={}),j=void 0;break;case 125:k.shift()}}if(k.length)throw new Error;return c}}()},l=window.console,m={log:function(){if(b&&l)try{l.log.apply(l,arguments)}catch(a){}},debug:function(){if(b&&l)try{l.debug.apply(l,arguments)}catch(a){}},info:function(){if(b&&l)try{l.info.apply(l,arguments)}catch(a){}},error:function(){if(b&&l)try{l.error.apply(l,arguments)}catch(a){}}},n={isString:function(a){return"string"==typeof a},isNumber:function(a){return"number"==typeof a},isBoolean:function(a){return"boolean"==typeof a},objectSize:function(a){var b,c=0;for(b in a)a.hasOwnProperty(b)&&c++;return c},contains:function(a,b){return b.indexOf(a)>=0},clone:function(a){if(null==a||"object"!=typeof a)return a;var b=a.constructor();for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b},trimString:function(a){return"function"!=typeof String.prototype.trim?a.replace(/^\s+|\s+$/g,""):a.trim()},buildGetQuery:function(a){var b=[];for(var c in a)this.isString(c)&&this.isString(a[c])&&b.push(encodeURIComponent(c)+"="+encodeURIComponent(a[c]));return"?"+b.join("&")},buildPropertyQueryParams:function(a){var b=[];for(var c in a)this.isString(c)&&this.isString(a[c])&&(b.push("propertyNames="+encodeURIComponent(c)),b.push("propertyValues="+encodeURIComponent(a[c])));return"&"+b.join("&")},extend:function(a,b){var c;if(null!=b)for(c in b)a[c]=b[c];return a},getCookie:function(){if(document.cookie)for(var b=document.cookie.split(";"),c=0;c-1&&(d.push("; domain="),d.push([".",this.getDomainName()].join("")))}var f=d.join("");m.log("setting cookie to be : ",f),this._setCookie(f)}},generateUUID:function(){var a=(new Date).getTime(),b="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(b){var c=(a+16*Math.random())%16|0;return a=Math.floor(a/16),("x"==b?c:7&c|8).toString(16)});return b},_getFullHostName:function(){return window.location.host},getDomainName:function(){var a=this._getFullHostName(),b=a.substring(a.lastIndexOf(".",a.lastIndexOf(".")-1)+1),c=b.split(":");if(c.length>1){var d=c[0],e="",f=c[1].split(".");f.length>1&&(e=[".",f[1]].join("")),b=[d,e].join("")}return b}},o=function(a){if(this.clearProperties(),void 0===a&&(a=!0),this._useCookie=a,a){var b=n.getCookie();b&&b.props&&(this.properties=b.props,m.info("Loaded in properties from cookie!",this.properties))}};o.prototype._setPropertiesCookie=function(){if(this._useCookie){var a=n.getCookie();a.props=this.properties,n.setCookie(a),m.info("saved cookie!",a)}},o.prototype.getProperties=function(){return this.properties},o.prototype.addProperties=function(a){m.info("IndicativePropertyManager.addProperties()",this.properties),m.info("adding props",a);var b;if(a instanceof Array)for(var c=0;c=1&&(h=b)}var c={};return c[j.cookiesOnMainDomain]=i,c[j.recordSessions]=g,c[j.sessionsThreshold]=h,c},p.prototype.initialize=function(b,c){if(!this.initalized){this.initalized=!0,a="Indicative_"+b,this._handleInitParams(c),this.apiKey=b,this.properties=new o;var d=n.getCookie();d&&d.uniqueID?(this.uniqueID=d.uniqueID,m.info("Loaded in uniqueID from cookie!",this.uniqueID)):d&&this._resetUniqueIDToDefault(d),m.info("What is uniqueiD? ",this.uniqueID),f&&this.buildAutomaticEvent("Page View (Automatic)")}},p.prototype.setUniqueID=function(a,b,c){a?(this.uniqueID=a,this._setUniqueIDCookie(),b&&this.sendAlias(c)):this.clearUniqueID()},p.prototype.clearUniqueID=function(a){delete this.uniqueID,this._setUniqueIDCookie(),this._resetUniqueIDToDefault(null,a)},p.prototype.getDefaultUniqueID=function(){var a=n.getCookie(),b=a.defaultUniqueID;return b},p.prototype.getProperties=function(){return this.properties?this.properties.getProperties():void 0},p.prototype.addProperty=function(a,b){this.properties.addProperty(a,b)},p.prototype.incrementProperty=function(a,b){this.properties.incrementProperty(a,b||1)},p.prototype.addProperties=function(a){this.properties.addProperties(a)},p.prototype.removeProperty=function(a){this.properties.removeProperty(a)},p.prototype.clearProperties=function(){this.properties.clearProperties()},p.prototype.sendAlias=function(a){var b=n.getCookie();if(b){var c=b.defaultUniqueID,d=b.uniqueID;if(c&&d){var e={apiKey:this.apiKey,previousId:c,newId:d};this._sendAlias(e,a)}}},p.prototype.buildEvent=function(a,b,c,d,e,f,g){if(m.info("Building event ",a,b,c,d),!this.apiKey)return m.info("missing something"),m.error("Cannot send an event without an api key"),!1;if(!a)return m.info("missing something name "),m.error("Cannot send an event without an event name"),!1;if(!this.uniqueID)return m.info("missing uniqueID"),m.error("Cannot send an event without a uniqueID"),!1;var h,i,j=this.uniqueID,k=n.clone(this.properties.getProperties()),l=function(a){m.info("eventPropsOrCallback this ",a),a&&("function"==typeof a?(m.info("It's a callback!"),i=a):"object"==typeof a&&(m.info("It's eventProperties!"),h=a))};if(b&&(n.isString(b)?(j=b,l(c),l(d)):(l(b),l(c))),h){var p=new o(!1);p.addProperties(h),n.extend(k,p.getProperties())}f||n.extend(k,this._buildStaticProps());var q={apiKey:this.apiKey,eventName:a};return j&&(q.eventUniqueId=j),k&&(q.properties=k),e?(m.info("returning event"),q):void this._sendEvent(q,i,g)},p.prototype._updateSession=function(){if(g&&h>=1){var a=(new Date).getTime(),b=n.getCookie(),c=b.lastSessionTime;(!c||a>=c+6e4*h)&&this.buildEvent("Web Session (Automatic)",void 0,void 0,void 0,void 0,!1,!0),b.lastSessionTime=a,n.setCookie(b)}},p.prototype.buildAutomaticEvent=function(a){var b=this.buildEvent(a,void 0,void 0,void 0,!0);b.automatic=!0,this._sendEvent(b)},p.prototype._sendEvent=function(a,b,c){c||this._updateSession(),this._postObjToEndpoint(a,d,b)},p.prototype._sendAlias=function(a,b){this._postObjToEndpoint(a,e,b)},p.prototype._postObjToEndpoint=function(a,b,d){m.info("Recording event ",a),m.info("Endpoint suffix ",b);var e=c+b,f=!1;if(window.XMLHttpRequest){var g=new XMLHttpRequest;if(void 0!==g.withCredentials){f=!0,g.onreadystatechange=function(){if(4==g.readyState)if(200===g.status)m.info("Successful stat post!"),m.info("What is callback? ",d),d&&d();else{var a="Error HTTP status: "+g.status+" "+g.statusText;if(m.error(a),422===g.status)return m.error("posting unprocessable object"),!1;d&&d()}},g.open("POST",e,!0),g.setRequestHeader("Content-Type","application/json"),g.setRequestHeader("Indicative-Client","javascript");try{g.send(k.encode(a))}catch(h){m.info("exception",h)}}}if(!f){var i;i=a.properties?[e,n.buildGetQuery(a),n.buildPropertyQueryParams(a.properties),"&breakcache="+Math.random()].join(""):[e,n.buildGetQuery(a),"&breakcache="+Math.random()].join(""),m.info("what is url?"),m.info(i),setTimeout(function(){var a=document.createElement("script");a.setAttribute("src",i),a.setAttribute("type","application/json"),a.setAttribute("async","true"),a.setAttribute("defer","true");var b=!1;a.onload=a.onreadystatechange=function(){m.info("loaded!!!!");var a=this.readyState;a&&"complete"!=a&&"loaded"!=a||!b&&d&&(b=!0,d())};var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(a,c)},100)}};var q={browser:function(a){return window.opera?n.contains("Opera Mini",a)?"OperaMini":n.contains("Opera Mobi")?"OperaMobile":"Opera":n.contains("BlackBerry",a)?"BlackBerry":n.contains("Blazer",a)?"Blazer":n.contains("IEMobile",a)?"IEMobile":n.contains("MSIE",a)?"IE":n.contains("GoBrowser",a)?"GoBrowser":n.contains("Chrome",a)?"Chrome":n.contains("Apple",a)?n.contains("Mobile",a)?"SafariMobile":"Safari":n.contains("Firefox",a)?"Firefox":n.contains("Android",a)?"Android":n.contains("Gecko",a)?"Mozilla":null},os:function(a){return n.contains("Android",a)?"Android":n.contains("iPhone",a)||n.contains("iPad",a)||n.contains("iPod",a)?"iOS":n.contains("BlackBerry",a)?"BlackBerry":n.contains("Mac",a)?"MacOS":n.contains("Linux",a)?"Linux":n.contains("Windows",a)?"Windows":null},device:function(a){return n.contains("Android",a)||n.contains("webOS",a)||n.contains("iPhone",a)||n.contains("iPad",a)||n.contains("iPod",a)||n.contains("BlackBerry",a)||n.contains("Windows Phone",a)?"Mobile":"Web"}};return p.prototype._mapSearchParams=function(a){var b={};a=decodeURIComponent(a);for(var c=a.substring(1),d=c.split("&"),e=0;e255&&(a.page_url=a.page_url.substring(0,255));var k=p.prototype._mapSearchParams(c.location.search),l=p.prototype._utm_parse(k);return l.source&&(a.campaign_source=l.source,a.$indicative_utm_source=l.source),l.medium&&(a.campaign_medium=l.medium,a.$indicative_utm_medium=l.medium),l.term&&(a.campaign_term=l.term,a.$indicative_utm_term=l.term),l.content&&(a.campaign_content=l.content,a.$indicative_utm_content=l.content),l.campaign&&(a.campaign_name=l.campaign,a.$indicative_utm_campaign=l.campaign),a},p.prototype.getAPIKey=function(){return this.apiKey},p.prototype.getUniqueID=function(){return this.uniqueID},new p});