Skip to content

Commit 5339b6d

Browse files
djihblazzy
authored andcommitted
add resetSessionId method (#140)
* add resetSessionId method * Add tests and update snippet
1 parent 0407209 commit 5339b6d

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
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 `resetSessionId` method that sets the sessionId to the current time.
4+
35
### 4.1.1 (March 22, 2018)
46

57
* Fix bug where cookie data such as device id from older releases were not migrated

src/amplitude-client.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ AmplitudeClient.prototype.setSessionId = function setSessionId(sessionId) {
707707
}
708708
};
709709

710+
AmplitudeClient.prototype.resetSessionId = function resetSessionId() {
711+
this.setSessionId(new Date().getTime());
712+
};
713+
710714
/**
711715
* Regenerates a new random deviceId for current user. Note: this is not recommended unless you know what you
712716
* 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', 'setSessionId'];
27+
'logEventWithTimestamp', 'logEventWithGroups', 'setSessionId', 'resetSessionId'];
2828
function setUpProxy(instance) {
2929
function proxyMain(fn) {
3030
instance[fn] = function() {

test/amplitude-client.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,31 @@ describe('setVersionName', function() {
927927
});
928928
});
929929

930+
describe('resetSessionId', function() {
931+
let clock;
932+
933+
beforeEach(function() {
934+
clock = sinon.useFakeTimers();
935+
});
936+
937+
afterEach(function() {
938+
reset();
939+
clock.restore();
940+
});
941+
942+
it('should reset the session Id', function() {
943+
clock.tick(10);
944+
amplitude.init(apiKey);
945+
946+
clock.tick(100);
947+
amplitude.resetSessionId();
948+
949+
clock.tick(200);
950+
951+
assert.equal(amplitude._sessionId, 110);
952+
});
953+
});
954+
930955
describe('identify', function() {
931956
let clock;
932957

test/snippet-tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ describe('Snippet', function() {
5353
assert.deepEqual(amplitude._iq['instance2']._q[0], ['init', 'API_KEY2']);
5454
assert.deepEqual(amplitude._iq['instance2']._q[1], ['logEvent', 'Event']);
5555
});
56+
57+
it('amplitude object should proxy resetSessionId', function() {
58+
amplitude.getInstance('reset_session_id_instance').init('API_KEY');
59+
amplitude.getInstance('reset_session_id_instance').resetSessionId();
60+
assert.deepEqual(amplitude._iq['reset_session_id_instance']._q[1], ['resetSessionId']);
61+
});
5662
});

0 commit comments

Comments
 (0)