Skip to content

Commit ecef8eb

Browse files
Julio FarahNawaz Uddin
andauthored
Fix/moengage id reset handle (#635)
* fix: handled reset on user id change * fix: removed unused codes * bump minor v Co-authored-by: Nawaz Uddin <nawazuddin@Nawazs-MacBook-Pro.local>
1 parent d6f9738 commit ecef8eb

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

integrations/moengage/lib/index.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ MoEngage.prototype.loaded = function() {
104104

105105
MoEngage.prototype.identify = function(identify) {
106106
var self = this;
107-
// Important: MoEngage require you to manually call reset to wipe the unique id for the session
108-
// if you don't do this and call add_unique_user_id w/ a different userId, it will overwrite the previous user's
109-
// unique id and all their traits so we need to manually check if it's a new user since `analytics.reset()` is not
110-
// mapped for ajs integrations
111-
// analytics.js regenerates anonymousId if you call `.identify()` with a unique userId value different from the cache
112-
if (this.initializedAnonymousId !== identify.anonymousId()) this.reset();
113107
if (identify.userId()) this._client.add_unique_user_id(identify.userId());
114108

115109
// send common traits
@@ -160,22 +154,9 @@ MoEngage.prototype.identify = function(identify) {
160154
*/
161155

162156
MoEngage.prototype.track = function(track) {
163-
// Important: MoEngage require you to manually call reset to wipe the unique id for the session
164-
// if you don't do this and call add_unique_user_id w/ a different userId, it will overwrite the previous user's
165-
// unique id and all their traits so we need to manually check if it's a new user since `analytics.reset()` is not
166-
// mapped for ajs integrations
167-
// analytics.js regenerates anonymousId if you call `.identify()` with a unique userId value different from the cache
168-
if (this.initializedAnonymousId !== track.anonymousId()) this.reset();
169157
this._client.track_event(track.event(), track.properties());
170158
};
171159

172-
/**
173-
* Reset
174-
*
175-
* @api public
176-
*/
177-
178-
MoEngage.prototype.reset = function() {
179-
this.initializedAnonymousId = this.analytics.user().anonymousId();
180-
this._client.destroy_session();
160+
MoEngage.prototype.alias = function(alias) {
161+
if (alias.to()) this._client.update_unique_user_id(alias.to());
181162
};

integrations/moengage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-integration-moengage",
33
"description": "The MoEngage analytics.js integration.",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"keywords": [
66
"analytics.js",
77
"analytics.js-integration",

integrations/moengage/test/index.test.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,7 @@ describe('MoEngage', function() {
150150
'The anonymous ID should be different after an identify call'
151151
);
152152
}
153-
analytics.called(moengage._client.destroy_session);
154153
analytics.called(moengage._client.add_unique_user_id, 'night king');
155-
if (moengage.initializedAnonymousId !== nightKingAnonId) {
156-
throw new Error(
157-
'MoEngange anonymous ID should be equal after an identify call'
158-
);
159-
}
160154
});
161155

162156
it('should not call destroy session if identify is called for a existing user', function() {
@@ -197,15 +191,6 @@ describe('MoEngage', function() {
197191
// Logout
198192
analytics.reset();
199193
analytics.track('The Song', properties);
200-
if (
201-
moengage.initializedAnonymousId !== analytics.user().anonymousId()
202-
) {
203-
throw new Error(
204-
'MoEngange anonymous ID should be equal after an identify call'
205-
);
206-
}
207-
1;
208-
analytics.called(moengage._client.destroy_session);
209194
});
210195

211196
it('should not call destroy session if track is called for a existing user', function() {
@@ -223,16 +208,27 @@ describe('MoEngage', function() {
223208
});
224209
});
225210

226-
describe('#reset', function() {
211+
describe('#alias', function() {
227212
beforeEach(function() {
228-
analytics.stub(moengage._client, 'destroy_session');
213+
analytics.stub(moengage._client, 'update_unique_user_id');
229214
});
230-
231-
it('should destroy session upon reset', function() {
232-
analytics.identify('justin');
233-
moengage.reset();
234-
analytics.called(moengage._client.destroy_session);
215+
it('should called update_unique_user_id on calling alias', function() {
216+
analytics.alias(123);
217+
analytics.called(moengage._client.update_unique_user_id);
218+
});
219+
it('should not called update_unique_user_id on calling alias without new ID', function() {
220+
analytics.alias();
221+
analytics.didNotCall(moengage._client.update_unique_user_id);
222+
});
223+
it('should called update_unique_user_id on calling alias with ID', function() {
224+
analytics.alias(123);
225+
analytics.called(moengage._client.update_unique_user_id, 123);
226+
});
227+
it('should not called update_unique_user_id on calling alias with wrong ID', function() {
228+
analytics.alias(123);
229+
analytics.didNotCall(moengage._client.update_unique_user_id, 1234);
235230
});
236231
});
232+
237233
});
238234
});

0 commit comments

Comments
 (0)