Skip to content

Commit 6419416

Browse files
authored
Merge pull request #119 from amplitude/set-session-id
add setSessionId method
2 parents ab17812 + 0948130 commit 6419416

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* Add `setSessionId(sesionId)` method. Note this is not recommended unless you know what are you doing.
4+
35
### 3.6.0 (September 13, 2017)
46

57
* Supports clean NPM module loading eg: `const amplitude = require('amplitude');` or `import 'amplitude' from 'amplitude-js';`

amplitude-segment-snippet.min.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var i=function(){this._q=[];return this}
77
;return this}
88
;var u=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
99
;for(var c=0;c<u.length;c++){r(a,u[c])}n.Revenue=a
10-
;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups"]
10+
;var l=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId"]
1111
;function p(e){function t(t){e[t]=function(){
1212
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
1313
for(var n=0;n<l.length;n++){t(l[n])}}p(n);n.getInstance=function(e){

amplitude-snippet.min.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
e.amplitude.runQueuedFunctions()}else{
66
console.log("[Amplitude] Error: could not load SDK")}}
77
;var i=t.getElementsByTagName("script")[0];i.parentNode.insertBefore(r,i)
8-
;function o(e,t){e.prototype[t]=function(){
8+
;function s(e,t){e.prototype[t]=function(){
99
this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}
10-
var s=function(){this._q=[];return this}
10+
var o=function(){this._q=[];return this}
1111
;var a=["add","append","clearAll","prepend","set","setOnce","unset"]
12-
;for(var u=0;u<a.length;u++){o(s,a[u])}n.Identify=s;var c=function(){this._q=[]
12+
;for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[]
1313
;return this}
1414
;var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
15-
;for(var p=0;p<l.length;p++){o(c,l[p])}n.Revenue=c
16-
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups"]
15+
;for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c
16+
;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","logEventWithTimestamp","logEventWithGroups","setSessionId"]
1717
;function v(e){function t(t){e[t]=function(){
1818
e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
1919
for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){

amplitude.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6247,6 +6247,19 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
62476247
}
62486248
};
62496249

6250+
AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
6251+
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
6252+
return;
6253+
}
6254+
6255+
try {
6256+
this._sessionId = sessionId;
6257+
_saveCookieData(this);
6258+
} catch (e) {
6259+
utils.log(e);
6260+
}
6261+
};
6262+
62506263
/**
62516264
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
62526265
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.

amplitude.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,19 @@ AmplitudeClient.prototype.setOptOut = function setOptOut(enable) {
642642
}
643643
};
644644

645+
AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
646+
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
647+
return;
648+
}
649+
650+
try {
651+
this._sessionId = sessionId;
652+
_saveCookieData(this);
653+
} catch (e) {
654+
utils.log(e);
655+
}
656+
};
657+
645658
/**
646659
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
647660
* are doing. This can be used in conjunction with `setUserId(null)` to anonymize users after they log out.

src/amplitude-snippet.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId',
2525
'setGlobalUserProperties', 'identify', 'clearUserProperties',
2626
'setGroup', 'logRevenueV2', 'regenerateDeviceId',
27-
'logEventWithTimestamp', 'logEventWithGroups'];
27+
'logEventWithTimestamp', 'logEventWithGroups', 'setSessionId'];
2828
function setUpProxy(instance) {
2929
function proxyMain(fn) {
3030
instance[fn] = function() {

test/amplitude-client.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,7 @@ describe('setVersionName', function() {
26922692
describe('sessionId', function() {
26932693
var clock;
26942694
beforeEach(function() {
2695+
reset();
26952696
clock = sinon.useFakeTimers();
26962697
amplitude.init(apiKey);
26972698
});
@@ -2722,5 +2723,42 @@ describe('setVersionName', function() {
27222723
assert.equal(amplitude2.getSessionId(), timestamp);
27232724
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
27242725
});
2726+
2727+
it('should ignore bad session id values', function() {
2728+
var timestamp = 1000;
2729+
clock.tick(timestamp);
2730+
var amplitude2 = new AmplitudeClient();
2731+
amplitude2.init(apiKey);
2732+
assert.equal(amplitude2._sessionId, timestamp);
2733+
assert.equal(amplitude2.getSessionId(), timestamp);
2734+
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
2735+
2736+
amplitude2.setSessionId('invalid session id');
2737+
assert.equal(amplitude2._sessionId, timestamp);
2738+
assert.equal(amplitude2.getSessionId(), timestamp);
2739+
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
2740+
});
2741+
2742+
it('should let user override sessionId with setSessionId', function() {
2743+
var amplitude2 = new AmplitudeClient();
2744+
var cookieStorage = new CookieStorage().getStorage();
2745+
2746+
// set up initial session
2747+
var sessionId = 1000;
2748+
clock.tick(sessionId);
2749+
amplitude2.init(apiKey);
2750+
assert.equal(amplitude2._sessionId, sessionId);
2751+
assert.equal(amplitude2.getSessionId(), sessionId);
2752+
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
2753+
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, sessionId);
2754+
2755+
// override sessionId with setSessionId
2756+
var newSessionId = 10000;
2757+
amplitude2.setSessionId(newSessionId);
2758+
assert.equal(amplitude2._sessionId, newSessionId);
2759+
assert.equal(amplitude2.getSessionId(), newSessionId);
2760+
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
2761+
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, newSessionId);
2762+
});
27252763
});
27262764
});

0 commit comments

Comments
 (0)