Skip to content

Commit 801c45d

Browse files
authored
Merge pull request #112 from amplitude/set-session-id
Set session
2 parents 673b76d + 8943424 commit 801c45d

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
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 method.
4+
35
### 3.4.1 (June 29, 2017)
46
* Handle SDK loading errors in the load snippet. Please update the load snippets on your website to [the latest version](https://amplitude.zendesk.com/hc/en-us/articles/115001361248-JavaScript-SDK-Installation#installation).
57
* Migrating setup instructions and SDK documentation in the README file to Zendesk articles.

amplitude.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,19 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
10651065
}
10661066
};
10671067

1068+
AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
1069+
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
1070+
return;
1071+
}
1072+
1073+
try {
1074+
this._sessionId = sessionId;
1075+
_saveCookieData(this);
1076+
} catch (e) {
1077+
utils.log(e);
1078+
}
1079+
};
1080+
10681081
/**
10691082
* Sets an identifier for the current user.
10701083
* @public

amplitude.min.js

Lines changed: 2 additions & 2 deletions
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
@@ -584,6 +584,19 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
584584
}
585585
};
586586

587+
AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
588+
if (!utils.validateInput(sessionId, 'sessionId', 'number')) {
589+
return;
590+
}
591+
592+
try {
593+
this._sessionId = sessionId;
594+
_saveCookieData(this);
595+
} catch (e) {
596+
utils.log(e);
597+
}
598+
};
599+
587600
/**
588601
* Sets an identifier for the current user.
589602
* @public

test/amplitude-client.js

Lines changed: 20 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,24 @@ describe('setVersionName', function() {
27222723
assert.equal(amplitude2.getSessionId(), timestamp);
27232724
assert.equal(amplitude2.getSessionId(), amplitude2._sessionId);
27242725
});
2726+
2727+
it('should let user override sessionId with setSessionId', function() {
2728+
var amplitude2 = new AmplitudeClient();
2729+
var cookieStorage = new CookieStorage().getStorage();
2730+
2731+
// set up initial session
2732+
var sessionId = 1000;
2733+
clock.tick(sessionId);
2734+
amplitude2.init(apiKey);
2735+
assert.equal(amplitude2._sessionId, sessionId);
2736+
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, sessionId);
2737+
2738+
// override sessionId with setSessionId
2739+
var newSessionId = 10000;
2740+
amplitude2.setSessionId(newSessionId);
2741+
assert.equal(amplitude2._sessionId, newSessionId);
2742+
// verify saved to cookie
2743+
assert.equal(cookieStorage.get(amplitude2.options.cookieName).sessionId, newSessionId);
2744+
});
27252745
});
27262746
});

0 commit comments

Comments
 (0)