Skip to content

Commit cf5c572

Browse files
authored
Merge pull request #16 from Countly/set_id-addition
Set id addition
2 parents 271f385 + f19851e commit cf5c572

File tree

5 files changed

+130
-43
lines changed

5 files changed

+130
-43
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 24.4.1
2+
3+
* Added a new method `set_id(newDeviceId)` for managing device id changes according to the device ID Type.
4+
15
## 24.4.0
26

37
! Minor breaking change ! For implementations using `salt` the browser compatability is tied to SubtleCrypto's `digest` method support

cypress/e2e/device_id_change.cy.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ describe("Device ID change tests ", ()=>{
145145
});
146146
});
147147
});
148+
it("Check init time temp mode with set_id", () => {
149+
hp.haltAndClearStorage(() => {
150+
initMain(true); // init in offline mode
151+
testDeviceIdInReqs(() => {
152+
Countly.set_id("new ID");
153+
});
154+
});
155+
});
148156

149157
// ========================================
150158
// default init configuration tests
@@ -209,4 +217,61 @@ describe("Device ID change tests ", ()=>{
209217
});
210218
});
211219
});
212-
});
220+
});
221+
222+
describe("Set ID change tests ", () => {
223+
it('set_id should be non merge as there was dev provided id', () => {
224+
hp.haltAndClearStorage(() => {
225+
Countly.init({
226+
app_key: "YOUR_APP_KEY",
227+
url: "https://your.domain.count.ly",
228+
test_mode: true,
229+
debug: true,
230+
device_id: "old ID"
231+
});
232+
Countly.add_event(eventObj("1")); // record an event.
233+
cy.wait(500); // wait for the request to be sent
234+
cy.fetch_local_request_queue().then((eq) => {
235+
expect(eq[0].device_id).to.equal("old ID");
236+
Countly.set_id("new ID");
237+
Countly.add_event(eventObj("2")); // record another event
238+
cy.wait(500); // wait for the request to be sent
239+
cy.fetch_local_request_queue().then((eq2) => {
240+
expect(eq2.length).to.equal(3); // no merge request
241+
expect(eq2[0].device_id).to.equal("old ID");
242+
expect(eq2[0].events).to.contains('"key\":\"1\"');
243+
expect(eq2[1].device_id).to.equal("new ID");
244+
expect(eq2[1].begin_session).to.equal(1);
245+
expect(eq2[2].device_id).to.equal("new ID");
246+
expect(eq2[2].events).to.contains('"key\":\"2\"');
247+
});
248+
});
249+
});
250+
});
251+
it('set_id should be merge as there was sdk generated id', () => {
252+
hp.haltAndClearStorage(() => {
253+
initMain(false); // init normally
254+
Countly.add_event(eventObj("1")); // record an event.
255+
cy.wait(500); // wait for the request to be sent
256+
let generatedID;
257+
cy.fetch_local_request_queue().then((eq) => {
258+
cy.log(eq);
259+
generatedID = eq[0].device_id; // get the new id from first item in the queue
260+
Countly.set_id("new ID");
261+
Countly.add_event(eventObj("2")); // record another event
262+
cy.wait(500); // wait for the request to be sent
263+
cy.fetch_local_request_queue().then((eq2) => {
264+
cy.log(eq2);
265+
expect(eq2.length).to.equal(3); // merge request
266+
expect(eq2[0].device_id).to.equal(generatedID);
267+
expect(eq2[0].events).to.contains('"key\":\"1\"');
268+
expect(eq2[1].device_id).to.equal("new ID");
269+
expect(eq2[1].old_device_id).to.equal(generatedID);
270+
expect(eq2[2].device_id).to.equal("new ID");
271+
expect(eq2[2].events).to.contains('"key\":\"2\"');
272+
});
273+
});
274+
});
275+
});
276+
277+
});

modules/Constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var healthCheckCounterEnum = Object.freeze({
104104
errorMessage: "cly_hc_error_message",
105105
});
106106

107-
var SDK_VERSION = "24.4.0";
107+
var SDK_VERSION = "24.4.1";
108108
var SDK_NAME = "javascript_native_web";
109109

110110
// Using this on document.referrer would return an array with 17 elements in it. The 12th element (array[11]) would be the path we are looking for. Others would be things like password and such (use https://regex101.com/ to check more)

modules/CountlyClass.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -984,17 +984,33 @@ class CountlyClass {
984984
};
985985

986986
/**
987-
* Change current user/device id
987+
* Changes the current device ID according to the device ID type (the preffered method)
988+
* @param {string} newId - new user/device ID to use. Must be a non-empty string value. Invalid values (like null, empty string or undefined) will be rejected
989+
* */
990+
this.set_id = function (newId) {
991+
log(logLevelEnums.INFO, "set_id, Changing the device ID to:[" + newId + "]");
992+
if (newId == null || newId === "") {
993+
log(logLevelEnums.WARNING, "set_id, The provided device is not a valid ID");
994+
return;
995+
}
996+
if (deviceIdType === DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED) {
997+
/*change ID without merge as current ID is Dev supplied, so not first login*/
998+
this.change_id(newId, false);
999+
} else {
1000+
/*change ID with merge as current ID is not Dev supplied*/
1001+
this.change_id(newId, true);
1002+
}
1003+
}
1004+
1005+
/**
1006+
* Change current user/device id (use set_id instead if you are not sure about the merge operation)
9881007
* @param {string} newId - new user/device ID to use. Must be a non-empty string value. Invalid values (like null, empty string or undefined) will be rejected
9891008
* @param {boolean} merge - move data from old ID to new ID on server
9901009
* */
9911010
this.change_id = function (newId, merge) {
992-
log(logLevelEnums.INFO, "change_id, Changing the ID");
993-
if (merge) {
994-
log(logLevelEnums.INFO, "change_id, Will merge the IDs");
995-
}
1011+
log(logLevelEnums.INFO, "change_id, Changing the device ID to: [" + newId + "] with merge:[" + merge + "]");
9961012
if (!newId || typeof newId !== "string" || newId.length === 0) {
997-
log(logLevelEnums.ERROR, "change_id, The provided ID: [" + newId + "] is not a valid ID");
1013+
log(logLevelEnums.WARNING, "change_id, The provided device ID is not a valid ID");
9981014
return;
9991015
}
10001016
if (offlineMode) {
@@ -1004,40 +1020,42 @@ class CountlyClass {
10041020
}
10051021
// eqeq is used here since we want to catch number to string checks too. type conversion might happen at a new init
10061022
// eslint-disable-next-line eqeqeq
1007-
if (this.device_id != newId) {
1008-
if (!merge) {
1009-
// process async queue before sending events
1010-
processAsyncQueue();
1011-
// empty event queue
1012-
sendEventsForced();
1013-
// end current session
1014-
this.end_session(null, true);
1015-
// clear timed events
1016-
timedEvents = {};
1017-
// clear all consents
1018-
this.remove_consent_internal(Countly.features, false);
1019-
}
1020-
var oldId = this.device_id;
1021-
this.device_id = newId;
1022-
self.device_id = this.device_id;
1023-
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
1024-
setValueInStorage("cly_id", this.device_id);
1025-
setValueInStorage("cly_id_type", DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED);
1026-
log(logLevelEnums.INFO, "change_id, Changing ID from:[" + oldId + "] to [" + newId + "]");
1027-
if (merge) {
1028-
// no consent check here since 21.11.0
1029-
toRequestQueue({ old_device_id: oldId });
1030-
}
1031-
else {
1032-
// start new session for new ID
1033-
this.begin_session(!autoExtend, true);
1034-
}
1035-
// if init time remote config was enabled with a callback function, remove currently stored remote configs and fetch remote config again
1036-
if (this.remote_config) {
1037-
remoteConfigs = {};
1038-
setValueInStorage("cly_remote_configs", remoteConfigs);
1039-
this.fetch_remote_config(this.remote_config);
1040-
}
1023+
if (this.device_id == newId) {
1024+
log(logLevelEnums.DEBUG, "change_id, Provided device ID is equal to the current device ID. Aborting.");
1025+
return;
1026+
}
1027+
if (!merge) {
1028+
// process async queue before sending events
1029+
processAsyncQueue();
1030+
// empty event queue
1031+
sendEventsForced();
1032+
// end current session
1033+
this.end_session(null, true);
1034+
// clear timed events
1035+
timedEvents = {};
1036+
// clear all consents
1037+
this.remove_consent_internal(Countly.features, false);
1038+
}
1039+
var oldId = this.device_id;
1040+
this.device_id = newId;
1041+
self.device_id = this.device_id;
1042+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
1043+
setValueInStorage("cly_id", this.device_id);
1044+
setValueInStorage("cly_id_type", DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED);
1045+
log(logLevelEnums.INFO, "change_id, Changing ID from:[" + oldId + "] to [" + newId + "]");
1046+
if (merge) {
1047+
// no consent check here since 21.11.0
1048+
toRequestQueue({ old_device_id: oldId });
1049+
}
1050+
else {
1051+
// start new session for new ID TODO: check this when no session tracking is enabled
1052+
this.begin_session(!autoExtend, true);
1053+
}
1054+
// if init time remote config was enabled with a callback function, remove currently stored remote configs and fetch remote config again
1055+
if (this.remote_config) {
1056+
remoteConfigs = {};
1057+
setValueInStorage("cly_remote_configs", remoteConfigs);
1058+
this.fetch_remote_config(this.remote_config);
10411059
}
10421060
};
10431061

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "countly-sdk-js",
3-
"version": "24.4.0",
3+
"version": "24.4.1",
44
"description": "Countly JavaScript SDK",
55
"type": "module",
66
"main": "Countly.js",

0 commit comments

Comments
 (0)