From 86f5b467dc42d5541fea8193e40149307336dd93 Mon Sep 17 00:00:00 2001 From: Curtis Liu Date: Fri, 19 Dec 2014 17:01:21 -0800 Subject: [PATCH] Fix bug where session ids weren't stored when a session timed out. --- amplitude.js | 14 ++++++-------- amplitude.min.js | 2 +- src/amplitude.js | 14 ++++++-------- test/amplitude.js | 27 +++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/amplitude.js b/amplitude.js index 425b4aa4..776fc5da 100644 --- a/amplitude.js +++ b/amplitude.js @@ -324,15 +324,13 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) { try { var eventTime = new Date().getTime(); var eventId = this.nextEventId(); - var sessionId = this._sessionId; - var lastEventTime = this._lastEventTime; var ua = this._ua; - if (!sessionId || !lastEventTime || eventTime - lastEventTime > this.options.sessionTimeout) { - sessionId = eventTime; - localStorage.setItem(LocalStorageKeys.SESSION_ID, sessionId); + if (!this._sessionId || !this._lastEventTime || eventTime - this._lastEventTime > this.options.sessionTimeout) { + this._sessionId = eventTime; + localStorage.setItem(LocalStorageKeys.SESSION_ID, this._sessionId); } - lastEventTime = eventTime; - localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, lastEventTime); + this._lastEventTime = eventTime; + localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime); localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId); eventProperties = eventProperties || {}; @@ -341,7 +339,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) { user_id: this.options.userId || this.options.deviceId, timestamp: eventTime, event_id: eventId, - session_id: sessionId || -1, + session_id: this._sessionId || -1, event_type: eventType, version_name: this.options.versionName || null, platform: this.options.platform, diff --git a/amplitude.min.js b/amplitude.min.js index 765daf21..44e324d5 100644 --- a/amplitude.min.js +++ b/amplitude.min.js @@ -1,2 +1,2 @@ -(function umd(require){if("object"==typeof exports){module.exports=require("1")}else if("function"==typeof define&&define.amd){define(function(){return require("1")})}else{this["amplitude"]=require("1")}})(function outer(modules,cache,entries){var global=function(){return this}();function require(name,jumped){if(cache[name])return cache[name].exports;if(modules[name])return call(name,require);throw new Error('cannot find module "'+name+'"')}function call(id,require){var m=cache[id]={exports:{}};var mod=modules[id];var name=mod[2];var fn=mod[0];fn.call(m.exports,function(req){var dep=modules[id][1][req];return require(dep?dep:req)},m,m.exports,outer,modules,cache,entries);if(name)cache[name]=cache[id];return cache[id].exports}for(var id in entries){if(entries[id]){global[entries[id]]=require(id)}else{require(id)}}require.duo=true;require.cache=cache;require.modules=modules;return require}({1:[function(require,module,exports){var Amplitude=require("./amplitude");var old=window.amplitude||{};var q=old._q||[];var instance=new Amplitude;for(var i=0;i0){this.sendEvents()}this._lastEventTime=parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME))||null;this._sessionId=parseInt(localStorage.getItem(LocalStorageKeys.SESSION_ID))||null;this._eventId=localStorage.getItem(LocalStorageKeys.LAST_EVENT_ID)||0;var now=(new Date).getTime();if(!this._sessionId||!this._lastEventTime||now-this._lastEventTime>this.options.sessionTimeout){this._sessionId=now;localStorage.setItem(LocalStorageKeys.SESSION_ID,this._sessionId)}this._lastEventTime=now;localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME,this._lastEventTime)}catch(e){log(e)}};Amplitude.prototype.nextEventId=function(){this._eventId++;return this._eventId};var _loadCookieData=function(scope){var cookieData=Cookie.get(scope.options.cookieName);if(cookieData){if(cookieData.deviceId){scope.options.deviceId=cookieData.deviceId}if(cookieData.userId){scope.options.userId=cookieData.userId}if(cookieData.globalUserProperties){scope.options.userProperties=cookieData.globalUserProperties}}};var _saveCookieData=function(scope){Cookie.set(scope.options.cookieName,{deviceId:scope.options.deviceId,userId:scope.options.userId,globalUserProperties:scope.options.userProperties})};Amplitude.prototype.saveEvents=function(){try{localStorage.setItem(this.options.unsentKey,JSON.stringify(this._unsentEvents))}catch(e){}};Amplitude.prototype.setDomain=function(domain){try{Cookie.options({domain:domain});this.options.domain=Cookie.options().domain;_loadCookieData(this);_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setUserId=function(userId){try{this.options.userId=userId!==undefined&&userId!==null&&""+userId||null;_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setDeviceId=function(deviceId){try{if(deviceId){this.options.deviceId=""+deviceId;_saveCookieData(this)}}catch(e){log(e)}};Amplitude.prototype.setUserProperties=function(userProperties,opt_replace){try{if(opt_replace){this.options.userProperties=userProperties}else{this.options.userProperties=object.merge(this.options.userProperties||{},userProperties)}_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setVersionName=function(versionName){try{this.options.versionName=versionName}catch(e){log(e)}};Amplitude.prototype.logEvent=function(eventType,eventProperties){if(!eventType){return}try{var eventTime=(new Date).getTime();var eventId=this.nextEventId();var sessionId=this._sessionId;var lastEventTime=this._lastEventTime;var ua=this._ua;if(!sessionId||!lastEventTime||eventTime-lastEventTime>this.options.sessionTimeout){sessionId=eventTime;localStorage.setItem(LocalStorageKeys.SESSION_ID,sessionId)}lastEventTime=eventTime;localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME,lastEventTime);localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID,eventId);eventProperties=eventProperties||{};var event={device_id:this.options.deviceId,user_id:this.options.userId||this.options.deviceId,timestamp:eventTime,event_id:eventId,session_id:sessionId||-1,event_type:eventType,version_name:this.options.versionName||null,platform:this.options.platform,os_name:ua.browser.family,os_version:ua.browser.version,device_model:ua.os.family,event_properties:eventProperties,user_properties:this.options.userProperties||{},uuid:UUID(),library:{name:"amplitude-js",version:this.__VERSION__}};this._unsentEvents.push(event);if(this.options.saveEvents){this.saveEvents()}this.sendEvents()}catch(e){log(e)}};Amplitude.prototype.sendEvents=function(){if(!this._sending){this._sending=true;var url=("https:"==window.location.protocol?"https":"http")+"://"+this.options.apiEndpoint+"/";var events=JSON.stringify(this._unsentEvents);var uploadTime=(new Date).getTime();var data={client:this.options.apiKey,e:events,v:API_VERSION,upload_time:uploadTime,checksum:md5(API_VERSION+this.options.apiKey+events+uploadTime)};var numEvents=this._unsentEvents.length;var scope=this;new Request(url,data).send(function(response){scope._sending=false;try{if(response=="success"){scope._unsentEvents.splice(0,numEvents);if(scope.options.saveEvents){scope.saveEvents()}if(scope._unsentEvents.length>0){scope.sendEvents()}}}catch(e){}})}};Amplitude.prototype.setGlobalUserProperties=Amplitude.prototype.setUserProperties;Amplitude.prototype.__VERSION__=version;module.exports=Amplitude},{"./base64":3,"./cookie":4,json:5,"./xhr":6,"./utf8":7,"./uuid":8,"./md5":9,"./localstorage":10,"./detect":11,"./version":12,object:13}],3:[function(require,module,exports){var UTF8=require("./utf8");var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(input){try{if(window.btoa&&window.atob){return window.btoa(unescape(encodeURIComponent(input)))}}catch(e){}return Base64._encode(input)},_encode:function(input){var output="";var chr1,chr2,chr3,enc1,enc2,enc3,enc4;var i=0;input=UTF8.encode(input);while(i>2;enc2=(chr1&3)<<4|chr2>>4;enc3=(chr2&15)<<2|chr3>>6;enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64}else if(isNaN(chr3)){enc4=64}output=output+Base64._keyStr.charAt(enc1)+Base64._keyStr.charAt(enc2)+Base64._keyStr.charAt(enc3)+Base64._keyStr.charAt(enc4)}return output},decode:function(input){try{if(window.btoa&&window.atob){return decodeURIComponent(escape(window.atob(input)))}}catch(e){}return Base64._decode(input)},_decode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i>4;chr2=(enc2&15)<<4|enc3>>2;chr3=(enc3&3)<<6|enc4;output=output+String.fromCharCode(chr1);if(enc3!=64){output=output+String.fromCharCode(chr2)}if(enc4!=64){output=output+String.fromCharCode(chr3)}}output=UTF8.decode(output);return output}};module.exports=Base64},{"./utf8":7}],7:[function(require,module,exports){var UTF8={encode:function(s){s=s.replace(/\r\n/g,"\n");var utftext="";for(var n=0;n127&&c<2048){utftext+=String.fromCharCode(c>>6|192);utftext+=String.fromCharCode(c&63|128)}else{utftext+=String.fromCharCode(c>>12|224);utftext+=String.fromCharCode(c>>6&63|128);utftext+=String.fromCharCode(c&63|128)}}return utftext},decode:function(utftext){var s="";var i=0;var c=0,c1=0,c2=0;while(i191&&c<224){c1=utftext.charCodeAt(i+1);s+=String.fromCharCode((c&31)<<6|c1&63);i+=2}else{c1=utftext.charCodeAt(i+1);c2=utftext.charCodeAt(i+2);s+=String.fromCharCode((c&15)<<12|(c1&63)<<6|c2&63);i+=3}}return s}};module.exports=UTF8},{}],4:[function(require,module,exports){var Base64=require("./base64");var JSON=require("json");var topDomain=require("top-domain");var _options={expirationDays:undefined,domain:undefined};var reset=function(){_options={}};var options=function(opts){if(arguments.length===0){return _options}opts=opts||{};_options.expirationDays=opts.expirationDays;var domain=opts.domain!==undefined?opts.domain:"."+topDomain(window.location.href);var token=Math.random();_options.domain=domain;set("amplitude_test",token);var stored=get("amplitude_test");if(!stored||stored!=token){domain=null}remove("amplitude_test");_options.domain=domain};var _domainSpecific=function(name){var suffix="";if(_options.domain){suffix=_options.domain.charAt(0)=="."?_options.domain.substring(1):_options.domain}return name+suffix};var get=function(name){try{var nameEq=_domainSpecific(name)+"=";var ca=document.cookie.split(";");var value=null;for(var i=0;i>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,uuid)};module.exports=uuid},{}],9:[function(require,module,exports){var UTF8=require("./utf8");var md5cycle=function(x,k){var a=x[0],b=x[1],c=x[2],d=x[3];a=ff(a,b,c,d,k[0],7,-680876936);d=ff(d,a,b,c,k[1],12,-389564586);c=ff(c,d,a,b,k[2],17,606105819);b=ff(b,c,d,a,k[3],22,-1044525330);a=ff(a,b,c,d,k[4],7,-176418897);d=ff(d,a,b,c,k[5],12,1200080426);c=ff(c,d,a,b,k[6],17,-1473231341);b=ff(b,c,d,a,k[7],22,-45705983);a=ff(a,b,c,d,k[8],7,1770035416);d=ff(d,a,b,c,k[9],12,-1958414417);c=ff(c,d,a,b,k[10],17,-42063);b=ff(b,c,d,a,k[11],22,-1990404162);a=ff(a,b,c,d,k[12],7,1804603682);d=ff(d,a,b,c,k[13],12,-40341101);c=ff(c,d,a,b,k[14],17,-1502002290);b=ff(b,c,d,a,k[15],22,1236535329);a=gg(a,b,c,d,k[1],5,-165796510);d=gg(d,a,b,c,k[6],9,-1069501632);c=gg(c,d,a,b,k[11],14,643717713);b=gg(b,c,d,a,k[0],20,-373897302);a=gg(a,b,c,d,k[5],5,-701558691);d=gg(d,a,b,c,k[10],9,38016083);c=gg(c,d,a,b,k[15],14,-660478335);b=gg(b,c,d,a,k[4],20,-405537848);a=gg(a,b,c,d,k[9],5,568446438);d=gg(d,a,b,c,k[14],9,-1019803690);c=gg(c,d,a,b,k[3],14,-187363961);b=gg(b,c,d,a,k[8],20,1163531501);a=gg(a,b,c,d,k[13],5,-1444681467);d=gg(d,a,b,c,k[2],9,-51403784);c=gg(c,d,a,b,k[7],14,1735328473);b=gg(b,c,d,a,k[12],20,-1926607734);a=hh(a,b,c,d,k[5],4,-378558);d=hh(d,a,b,c,k[8],11,-2022574463);c=hh(c,d,a,b,k[11],16,1839030562);b=hh(b,c,d,a,k[14],23,-35309556);a=hh(a,b,c,d,k[1],4,-1530992060);d=hh(d,a,b,c,k[4],11,1272893353);c=hh(c,d,a,b,k[7],16,-155497632);b=hh(b,c,d,a,k[10],23,-1094730640);a=hh(a,b,c,d,k[13],4,681279174);d=hh(d,a,b,c,k[0],11,-358537222);c=hh(c,d,a,b,k[3],16,-722521979);b=hh(b,c,d,a,k[6],23,76029189);a=hh(a,b,c,d,k[9],4,-640364487);d=hh(d,a,b,c,k[12],11,-421815835);c=hh(c,d,a,b,k[15],16,530742520);b=hh(b,c,d,a,k[2],23,-995338651);a=ii(a,b,c,d,k[0],6,-198630844);d=ii(d,a,b,c,k[7],10,1126891415);c=ii(c,d,a,b,k[14],15,-1416354905);b=ii(b,c,d,a,k[5],21,-57434055);a=ii(a,b,c,d,k[12],6,1700485571);d=ii(d,a,b,c,k[3],10,-1894986606);c=ii(c,d,a,b,k[10],15,-1051523);b=ii(b,c,d,a,k[1],21,-2054922799);a=ii(a,b,c,d,k[8],6,1873313359);d=ii(d,a,b,c,k[15],10,-30611744);c=ii(c,d,a,b,k[6],15,-1560198380);b=ii(b,c,d,a,k[13],21,1309151649);a=ii(a,b,c,d,k[4],6,-145523070);d=ii(d,a,b,c,k[11],10,-1120210379);c=ii(c,d,a,b,k[2],15,718787259);b=ii(b,c,d,a,k[9],21,-343485551);x[0]=add32(a,x[0]);x[1]=add32(b,x[1]);x[2]=add32(c,x[2]);x[3]=add32(d,x[3])};var cmn=function(q,a,b,x,s,t){a=add32(add32(a,q),add32(x,t));return add32(a<>>32-s,b)};var ff=function(a,b,c,d,x,s,t){return cmn(b&c|~b&d,a,b,x,s,t)};var gg=function(a,b,c,d,x,s,t){return cmn(b&d|c&~d,a,b,x,s,t)};var hh=function(a,b,c,d,x,s,t){return cmn(b^c^d,a,b,x,s,t)};var ii=function(a,b,c,d,x,s,t){return cmn(c^(b|~d),a,b,x,s,t)};var md51=function(s){s=UTF8.encode(s);var n=s.length,state=[1732584193,-271733879,-1732584194,271733878],i;for(i=64;i<=s.length;i+=64){md5cycle(state,md5blk(s.substring(i-64,i)))}s=s.substring(i-64);var tail=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(i=0;i>2]|=s.charCodeAt(i)<<(i%4<<3);tail[i>>2]|=128<<(i%4<<3);if(i>55){md5cycle(state,tail);for(i=0;i<16;i++)tail[i]=0}tail[14]=n*8;md5cycle(state,tail);return state};var md5blk=function(s){var md5blks=[],i;for(i=0;i<64;i+=4){md5blks[i>>2]=s.charCodeAt(i)+(s.charCodeAt(i+1)<<8)+(s.charCodeAt(i+2)<<16)+(s.charCodeAt(i+3)<<24)}return md5blks};var hex_chr="0123456789abcdef".split("");var rhex=function(n){var s="",j=0;for(;j<4;j++)s+=hex_chr[n>>j*8+4&15]+hex_chr[n>>j*8&15];return s};var hex=function(x){for(var i=0;i>16)+(y>>16)+(lsw>>16);return msw<<16|lsw&65535}}module.exports=md5},{"./utf8":7}],10:[function(require,module,exports){var localStorage;if(window.localStorage){localStorage=window.localStorage}else if(window.globalStorage){try{localStorage=window.globalStorage[window.location.hostname]}catch(e){}}else{var div=document.createElement("div"),attrKey="localStorage";div.style.display="none";document.getElementsByTagName("head")[0].appendChild(div);if(div.addBehavior){div.addBehavior("#default#userdata");localStorage={length:0,setItem:function(k,v){div.load(attrKey);if(!div.getAttribute(k)){this.length++}div.setAttribute(k,v);div.save(attrKey)},getItem:function(k){div.load(attrKey);return div.getAttribute(k)},removeItem:function(k){div.load(attrKey);if(div.getAttribute(k)){this.length--}div.removeAttribute(k);div.save(attrKey)},clear:function(){div.load(attrKey);var i=0;var attr;while(attr=div.XMLDocument.documentElement.attributes[i++]){div.removeAttribute(attr.name)}div.save(attrKey);this.length=0},key:function(k){div.load(attrKey);return div.XMLDocument.documentElement.attributes[k]}};div.load(attrKey);localStorage.length=div.XMLDocument.documentElement.attributes.length}else{}}if(!localStorage){localStorage={length:0,setItem:function(k,v){},getItem:function(k){},removeItem:function(k){},clear:function(){},key:function(k){}}}module.exports=localStorage},{}],11:[function(require,module,exports){(function(root,undefined){if(!Array.prototype.map){Array.prototype.map=function(callback,thisArg){var T,A,k;if(this==null){throw new TypeError(" this is null or not defined")}var O=Object(this);var len=O.length>>>0;if(typeof callback!=="function"){throw new TypeError(callback+" is not a function")}if(thisArg){T=thisArg}A=new Array(len);k=0;while(k0){this.sendEvents()}this._lastEventTime=parseInt(localStorage.getItem(LocalStorageKeys.LAST_EVENT_TIME))||null;this._sessionId=parseInt(localStorage.getItem(LocalStorageKeys.SESSION_ID))||null;this._eventId=localStorage.getItem(LocalStorageKeys.LAST_EVENT_ID)||0;var now=(new Date).getTime();if(!this._sessionId||!this._lastEventTime||now-this._lastEventTime>this.options.sessionTimeout){this._sessionId=now;localStorage.setItem(LocalStorageKeys.SESSION_ID,this._sessionId)}this._lastEventTime=now;localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME,this._lastEventTime)}catch(e){log(e)}};Amplitude.prototype.nextEventId=function(){this._eventId++;return this._eventId};var _loadCookieData=function(scope){var cookieData=Cookie.get(scope.options.cookieName);if(cookieData){if(cookieData.deviceId){scope.options.deviceId=cookieData.deviceId}if(cookieData.userId){scope.options.userId=cookieData.userId}if(cookieData.globalUserProperties){scope.options.userProperties=cookieData.globalUserProperties}}};var _saveCookieData=function(scope){Cookie.set(scope.options.cookieName,{deviceId:scope.options.deviceId,userId:scope.options.userId,globalUserProperties:scope.options.userProperties})};Amplitude.prototype.saveEvents=function(){try{localStorage.setItem(this.options.unsentKey,JSON.stringify(this._unsentEvents))}catch(e){}};Amplitude.prototype.setDomain=function(domain){try{Cookie.options({domain:domain});this.options.domain=Cookie.options().domain;_loadCookieData(this);_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setUserId=function(userId){try{this.options.userId=userId!==undefined&&userId!==null&&""+userId||null;_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setDeviceId=function(deviceId){try{if(deviceId){this.options.deviceId=""+deviceId;_saveCookieData(this)}}catch(e){log(e)}};Amplitude.prototype.setUserProperties=function(userProperties,opt_replace){try{if(opt_replace){this.options.userProperties=userProperties}else{this.options.userProperties=object.merge(this.options.userProperties||{},userProperties)}_saveCookieData(this)}catch(e){log(e)}};Amplitude.prototype.setVersionName=function(versionName){try{this.options.versionName=versionName}catch(e){log(e)}};Amplitude.prototype.logEvent=function(eventType,eventProperties){if(!eventType){return}try{var eventTime=(new Date).getTime();var eventId=this.nextEventId();var ua=this._ua;if(!this._sessionId||!this._lastEventTime||eventTime-this._lastEventTime>this.options.sessionTimeout){this._sessionId=eventTime;localStorage.setItem(LocalStorageKeys.SESSION_ID,this._sessionId)}this._lastEventTime=eventTime;localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME,this._lastEventTime);localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID,eventId);eventProperties=eventProperties||{};var event={device_id:this.options.deviceId,user_id:this.options.userId||this.options.deviceId,timestamp:eventTime,event_id:eventId,session_id:this._sessionId||-1,event_type:eventType,version_name:this.options.versionName||null,platform:this.options.platform,os_name:ua.browser.family,os_version:ua.browser.version,device_model:ua.os.family,event_properties:eventProperties,user_properties:this.options.userProperties||{},uuid:UUID(),library:{name:"amplitude-js",version:this.__VERSION__}};this._unsentEvents.push(event);if(this.options.saveEvents){this.saveEvents()}this.sendEvents()}catch(e){log(e)}};Amplitude.prototype.sendEvents=function(){if(!this._sending){this._sending=true;var url=("https:"==window.location.protocol?"https":"http")+"://"+this.options.apiEndpoint+"/";var events=JSON.stringify(this._unsentEvents);var uploadTime=(new Date).getTime();var data={client:this.options.apiKey,e:events,v:API_VERSION,upload_time:uploadTime,checksum:md5(API_VERSION+this.options.apiKey+events+uploadTime)};var numEvents=this._unsentEvents.length;var scope=this;new Request(url,data).send(function(response){scope._sending=false;try{if(response=="success"){scope._unsentEvents.splice(0,numEvents);if(scope.options.saveEvents){scope.saveEvents()}if(scope._unsentEvents.length>0){scope.sendEvents()}}}catch(e){}})}};Amplitude.prototype.setGlobalUserProperties=Amplitude.prototype.setUserProperties;Amplitude.prototype.__VERSION__=version;module.exports=Amplitude},{"./base64":3,"./cookie":4,json:5,"./xhr":6,"./utf8":7,"./uuid":8,"./md5":9,"./localstorage":10,"./detect":11,"./version":12,object:13}],3:[function(require,module,exports){var UTF8=require("./utf8");var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(input){try{if(window.btoa&&window.atob){return window.btoa(unescape(encodeURIComponent(input)))}}catch(e){}return Base64._encode(input)},_encode:function(input){var output="";var chr1,chr2,chr3,enc1,enc2,enc3,enc4;var i=0;input=UTF8.encode(input);while(i>2;enc2=(chr1&3)<<4|chr2>>4;enc3=(chr2&15)<<2|chr3>>6;enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64}else if(isNaN(chr3)){enc4=64}output=output+Base64._keyStr.charAt(enc1)+Base64._keyStr.charAt(enc2)+Base64._keyStr.charAt(enc3)+Base64._keyStr.charAt(enc4)}return output},decode:function(input){try{if(window.btoa&&window.atob){return decodeURIComponent(escape(window.atob(input)))}}catch(e){}return Base64._decode(input)},_decode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(i>4;chr2=(enc2&15)<<4|enc3>>2;chr3=(enc3&3)<<6|enc4;output=output+String.fromCharCode(chr1);if(enc3!=64){output=output+String.fromCharCode(chr2)}if(enc4!=64){output=output+String.fromCharCode(chr3)}}output=UTF8.decode(output);return output}};module.exports=Base64},{"./utf8":7}],7:[function(require,module,exports){var UTF8={encode:function(s){s=s.replace(/\r\n/g,"\n");var utftext="";for(var n=0;n127&&c<2048){utftext+=String.fromCharCode(c>>6|192);utftext+=String.fromCharCode(c&63|128)}else{utftext+=String.fromCharCode(c>>12|224);utftext+=String.fromCharCode(c>>6&63|128);utftext+=String.fromCharCode(c&63|128)}}return utftext},decode:function(utftext){var s="";var i=0;var c=0,c1=0,c2=0;while(i191&&c<224){c1=utftext.charCodeAt(i+1);s+=String.fromCharCode((c&31)<<6|c1&63);i+=2}else{c1=utftext.charCodeAt(i+1);c2=utftext.charCodeAt(i+2);s+=String.fromCharCode((c&15)<<12|(c1&63)<<6|c2&63);i+=3}}return s}};module.exports=UTF8},{}],4:[function(require,module,exports){var Base64=require("./base64");var JSON=require("json");var topDomain=require("top-domain");var _options={expirationDays:undefined,domain:undefined};var reset=function(){_options={}};var options=function(opts){if(arguments.length===0){return _options}opts=opts||{};_options.expirationDays=opts.expirationDays;var domain=opts.domain!==undefined?opts.domain:"."+topDomain(window.location.href);var token=Math.random();_options.domain=domain;set("amplitude_test",token);var stored=get("amplitude_test");if(!stored||stored!=token){domain=null}remove("amplitude_test");_options.domain=domain};var _domainSpecific=function(name){var suffix="";if(_options.domain){suffix=_options.domain.charAt(0)=="."?_options.domain.substring(1):_options.domain}return name+suffix};var get=function(name){try{var nameEq=_domainSpecific(name)+"=";var ca=document.cookie.split(";");var value=null;for(var i=0;i>a/4).toString(16):([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,uuid)};module.exports=uuid},{}],9:[function(require,module,exports){var UTF8=require("./utf8");var md5cycle=function(x,k){var a=x[0],b=x[1],c=x[2],d=x[3];a=ff(a,b,c,d,k[0],7,-680876936);d=ff(d,a,b,c,k[1],12,-389564586);c=ff(c,d,a,b,k[2],17,606105819);b=ff(b,c,d,a,k[3],22,-1044525330);a=ff(a,b,c,d,k[4],7,-176418897);d=ff(d,a,b,c,k[5],12,1200080426);c=ff(c,d,a,b,k[6],17,-1473231341);b=ff(b,c,d,a,k[7],22,-45705983);a=ff(a,b,c,d,k[8],7,1770035416);d=ff(d,a,b,c,k[9],12,-1958414417);c=ff(c,d,a,b,k[10],17,-42063);b=ff(b,c,d,a,k[11],22,-1990404162);a=ff(a,b,c,d,k[12],7,1804603682);d=ff(d,a,b,c,k[13],12,-40341101);c=ff(c,d,a,b,k[14],17,-1502002290);b=ff(b,c,d,a,k[15],22,1236535329);a=gg(a,b,c,d,k[1],5,-165796510);d=gg(d,a,b,c,k[6],9,-1069501632);c=gg(c,d,a,b,k[11],14,643717713);b=gg(b,c,d,a,k[0],20,-373897302);a=gg(a,b,c,d,k[5],5,-701558691);d=gg(d,a,b,c,k[10],9,38016083);c=gg(c,d,a,b,k[15],14,-660478335);b=gg(b,c,d,a,k[4],20,-405537848);a=gg(a,b,c,d,k[9],5,568446438);d=gg(d,a,b,c,k[14],9,-1019803690);c=gg(c,d,a,b,k[3],14,-187363961);b=gg(b,c,d,a,k[8],20,1163531501);a=gg(a,b,c,d,k[13],5,-1444681467);d=gg(d,a,b,c,k[2],9,-51403784);c=gg(c,d,a,b,k[7],14,1735328473);b=gg(b,c,d,a,k[12],20,-1926607734);a=hh(a,b,c,d,k[5],4,-378558);d=hh(d,a,b,c,k[8],11,-2022574463);c=hh(c,d,a,b,k[11],16,1839030562);b=hh(b,c,d,a,k[14],23,-35309556);a=hh(a,b,c,d,k[1],4,-1530992060);d=hh(d,a,b,c,k[4],11,1272893353);c=hh(c,d,a,b,k[7],16,-155497632);b=hh(b,c,d,a,k[10],23,-1094730640);a=hh(a,b,c,d,k[13],4,681279174);d=hh(d,a,b,c,k[0],11,-358537222);c=hh(c,d,a,b,k[3],16,-722521979);b=hh(b,c,d,a,k[6],23,76029189);a=hh(a,b,c,d,k[9],4,-640364487);d=hh(d,a,b,c,k[12],11,-421815835);c=hh(c,d,a,b,k[15],16,530742520);b=hh(b,c,d,a,k[2],23,-995338651);a=ii(a,b,c,d,k[0],6,-198630844);d=ii(d,a,b,c,k[7],10,1126891415);c=ii(c,d,a,b,k[14],15,-1416354905);b=ii(b,c,d,a,k[5],21,-57434055);a=ii(a,b,c,d,k[12],6,1700485571);d=ii(d,a,b,c,k[3],10,-1894986606);c=ii(c,d,a,b,k[10],15,-1051523);b=ii(b,c,d,a,k[1],21,-2054922799);a=ii(a,b,c,d,k[8],6,1873313359);d=ii(d,a,b,c,k[15],10,-30611744);c=ii(c,d,a,b,k[6],15,-1560198380);b=ii(b,c,d,a,k[13],21,1309151649);a=ii(a,b,c,d,k[4],6,-145523070);d=ii(d,a,b,c,k[11],10,-1120210379);c=ii(c,d,a,b,k[2],15,718787259);b=ii(b,c,d,a,k[9],21,-343485551);x[0]=add32(a,x[0]);x[1]=add32(b,x[1]);x[2]=add32(c,x[2]);x[3]=add32(d,x[3])};var cmn=function(q,a,b,x,s,t){a=add32(add32(a,q),add32(x,t));return add32(a<>>32-s,b)};var ff=function(a,b,c,d,x,s,t){return cmn(b&c|~b&d,a,b,x,s,t)};var gg=function(a,b,c,d,x,s,t){return cmn(b&d|c&~d,a,b,x,s,t)};var hh=function(a,b,c,d,x,s,t){return cmn(b^c^d,a,b,x,s,t)};var ii=function(a,b,c,d,x,s,t){return cmn(c^(b|~d),a,b,x,s,t)};var md51=function(s){s=UTF8.encode(s);var n=s.length,state=[1732584193,-271733879,-1732584194,271733878],i;for(i=64;i<=s.length;i+=64){md5cycle(state,md5blk(s.substring(i-64,i)))}s=s.substring(i-64);var tail=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(i=0;i>2]|=s.charCodeAt(i)<<(i%4<<3);tail[i>>2]|=128<<(i%4<<3);if(i>55){md5cycle(state,tail);for(i=0;i<16;i++)tail[i]=0}tail[14]=n*8;md5cycle(state,tail);return state};var md5blk=function(s){var md5blks=[],i;for(i=0;i<64;i+=4){md5blks[i>>2]=s.charCodeAt(i)+(s.charCodeAt(i+1)<<8)+(s.charCodeAt(i+2)<<16)+(s.charCodeAt(i+3)<<24)}return md5blks};var hex_chr="0123456789abcdef".split("");var rhex=function(n){var s="",j=0;for(;j<4;j++)s+=hex_chr[n>>j*8+4&15]+hex_chr[n>>j*8&15];return s};var hex=function(x){for(var i=0;i>16)+(y>>16)+(lsw>>16);return msw<<16|lsw&65535}}module.exports=md5},{"./utf8":7}],10:[function(require,module,exports){var localStorage;if(window.localStorage){localStorage=window.localStorage}else if(window.globalStorage){try{localStorage=window.globalStorage[window.location.hostname]}catch(e){}}else{var div=document.createElement("div"),attrKey="localStorage";div.style.display="none";document.getElementsByTagName("head")[0].appendChild(div);if(div.addBehavior){div.addBehavior("#default#userdata");localStorage={length:0,setItem:function(k,v){div.load(attrKey);if(!div.getAttribute(k)){this.length++}div.setAttribute(k,v);div.save(attrKey)},getItem:function(k){div.load(attrKey);return div.getAttribute(k)},removeItem:function(k){div.load(attrKey);if(div.getAttribute(k)){this.length--}div.removeAttribute(k);div.save(attrKey)},clear:function(){div.load(attrKey);var i=0;var attr;while(attr=div.XMLDocument.documentElement.attributes[i++]){div.removeAttribute(attr.name)}div.save(attrKey);this.length=0},key:function(k){div.load(attrKey);return div.XMLDocument.documentElement.attributes[k]}};div.load(attrKey);localStorage.length=div.XMLDocument.documentElement.attributes.length}else{}}if(!localStorage){localStorage={length:0,setItem:function(k,v){},getItem:function(k){},removeItem:function(k){},clear:function(){},key:function(k){}}}module.exports=localStorage},{}],11:[function(require,module,exports){(function(root,undefined){if(!Array.prototype.map){Array.prototype.map=function(callback,thisArg){var T,A,k;if(this==null){throw new TypeError(" this is null or not defined")}var O=Object(this);var len=O.length>>>0;if(typeof callback!=="function"){throw new TypeError(callback+" is not a function")}if(thisArg){T=thisArg}A=new Array(len);k=0;while(k this.options.sessionTimeout) { - sessionId = eventTime; - localStorage.setItem(LocalStorageKeys.SESSION_ID, sessionId); + if (!this._sessionId || !this._lastEventTime || eventTime - this._lastEventTime > this.options.sessionTimeout) { + this._sessionId = eventTime; + localStorage.setItem(LocalStorageKeys.SESSION_ID, this._sessionId); } - lastEventTime = eventTime; - localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, lastEventTime); + this._lastEventTime = eventTime; + localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime); localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId); eventProperties = eventProperties || {}; @@ -231,7 +229,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) { user_id: this.options.userId || this.options.deviceId, timestamp: eventTime, event_id: eventId, - session_id: sessionId || -1, + session_id: this._sessionId || -1, event_type: eventType, version_name: this.options.versionName || null, platform: this.options.platform, diff --git a/test/amplitude.js b/test/amplitude.js index 6132b34f..30ac23f9 100644 --- a/test/amplitude.js +++ b/test/amplitude.js @@ -167,4 +167,31 @@ describe('Amplitude', function() { assert.deepEqual(events[0].event_properties, {prop: true}); }); }); + + describe('sessionId', function() { + + var clock; + + beforeEach(function() { + clock = sinon.useFakeTimers(); + amplitude.init(apiKey); + }); + + afterEach(function() { + reset(); + clock.restore(); + }); + + it('should create new session IDs on timeout', function() { + var sessionId = amplitude._sessionId; + clock.tick(30 * 60 * 1000 + 1); + amplitude.logEvent('Event Type 1'); + assert.lengthOf(server.requests, 1); + var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e); + assert.equal(events.length, 1); + assert.notEqual(events[0].session_id, sessionId); + assert.notEqual(amplitude._sessionId, sessionId); + assert.equal(events[0].session_id, amplitude._sessionId); + }); + }); });