Skip to content

Commit

Permalink
Merge pull request #119 from amplitude/set-session-id
Browse files Browse the repository at this point in the history
add setSessionId method
  • Loading branch information
djih authored Sep 14, 2017
2 parents ab17812 + 0948130 commit 6419416
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Add `setSessionId(sesionId)` method. Note this is not recommended unless you know what are you doing.

### 3.6.0 (September 13, 2017)

* Supports clean NPM module loading eg: `const amplitude = require('amplitude');` or `import 'amplitude' from 'amplitude-js';`
Expand Down
2 changes: 1 addition & 1 deletion amplitude-segment-snippet.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var i=function(){this._q=[];return this}
;return this}
;var u=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
;for(var c=0;c<u.length;c++){r(a,u[c])}n.Revenue=a
;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups"]
;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId"]
;function p(e){function t(t){e[t]=function(){
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
for(var n=0;n<l.length;n++){t(l[n])}}p(n);n.getInstance=function(e){
Expand Down
10 changes: 5 additions & 5 deletions amplitude-snippet.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
e.amplitude.runQueuedFunctions()}else{
console.log("[Amplitude] Error: could not load SDK")}}
;var i=t.getElementsByTagName("script")[0];i.parentNode.insertBefore(r,i)
;function o(e,t){e.prototype[t]=function(){
;function s(e,t){e.prototype[t]=function(){
this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}
var s=function(){this._q=[];return this}
var o=function(){this._q=[];return this}
;var a=["add","append","clearAll","prepend","set","setOnce","unset"]
;for(var u=0;u<a.length;u++){o(s,a[u])}n.Identify=s;var c=function(){this._q=[]
;for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[]
;return this}
;var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
;for(var p=0;p<l.length;p++){o(c,l[p])}n.Revenue=c
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups"]
;for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId"]
;function v(e){function t(t){e[t]=function(){
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){
Expand Down
13 changes: 13 additions & 0 deletions amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -6247,6 +6247,19 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
}
};

AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
return;
}

try {
this._sessionId = sessionId;
_saveCookieData(this);
} catch (e) {
utils.log(e);
}
};

/**
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
Expand Down
2 changes: 1 addition & 1 deletion amplitude.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,19 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
}
};

AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
return;
}

try {
this._sessionId = sessionId;
_saveCookieData(this);
} catch (e) {
utils.log(e);
}
};

/**
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.
Expand Down
2 changes: 1 addition & 1 deletion src/amplitude-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
'setGlobalUserProperties', 'identify', 'clearUserProperties',
'setGroup', 'logRevenueV2', 'regenerateDeviceId',
'logEventWithTimestamp', 'logEventWithGroups'];
'logEventWithTimestamp', 'logEventWithGroups', 'setSessionId'];
function setUpProxy(instance) {
function proxyMain(fn) {
instance[fn] = function() {
Expand Down
38 changes: 38 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2692,6 +2692,7 @@ describe('setVersionName', function() {
describe('sessionId', function() {
var clock;
beforeEach(function() {
reset();
clock = sinon.useFakeTimers();
amplitude.init(apiKey);
});
Expand Down Expand Up @@ -2722,5 +2723,42 @@ describe('setVersionName', function() {
assert.equal(amplitude2.getSessionId(), timestamp);
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
});

it('should ignore bad session id values', function() {
var timestamp = 1000;
clock.tick(timestamp);
var amplitude2 = new AmplitudeClient();
amplitude2.init(apiKey);
assert.equal(amplitude2._sessionId, timestamp);
assert.equal(amplitude2.getSessionId(), timestamp);
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);

amplitude2.setSessionId('invalid session id');
assert.equal(amplitude2._sessionId, timestamp);
assert.equal(amplitude2.getSessionId(), timestamp);
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
});

it('should let user override sessionId with setSessionId', function() {
var amplitude2 = new AmplitudeClient();
var cookieStorage = new CookieStorage().getStorage();

// set up initial session
var sessionId = 1000;
clock.tick(sessionId);
amplitude2.init(apiKey);
assert.equal(amplitude2._sessionId, sessionId);
assert.equal(amplitude2.getSessionId(), sessionId);
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, sessionId);

// override sessionId with setSessionId
var newSessionId = 10000;
amplitude2.setSessionId(newSessionId);
assert.equal(amplitude2._sessionId, newSessionId);
assert.equal(amplitude2.getSessionId(), newSessionId);
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, newSessionId);
});
});
});

0 comments on commit 6419416

Please sign in to comment.