Skip to content

Commit 51b1032

Browse files
authored
Merge pull request #109 from Countly/set_id
feat: set_id method for the node
2 parents c966016 + 9d03126 commit 51b1032

File tree

5 files changed

+157
-12
lines changed

5 files changed

+157
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## XX.XX.XX
2+
- Added a new method `set_id(newDeviceId)` for managing device ID changes according to the device ID Type
3+
- Added `DeviceIdType` enums to be used to evaluate the device ID type.
4+
15
## 24.10.0
26
- Default max segmentation value count changed from 30 to 100
37
- Mitigated an issue where SDK could create an unintended dump file

lib/countly.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var CountlyStorage = require("./countly-storage");
3030

3131
var Countly = {};
3232
Countly.StorageTypes = cc.storageTypeEnums;
33+
Countly.DeviceIdType = cc.deviceIdTypeEnums;
3334
Countly.Bulk = Bulk;
3435
(function() {
3536
var SDK_VERSION = "24.10.0";
@@ -627,6 +628,26 @@ Countly.Bulk = Bulk;
627628
return Countly.device_id;
628629
};
629630

631+
/**
632+
* Changes the current device ID according to the device ID type (the preffered method)
633+
* @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
634+
* */
635+
Countly.set_id = function(newId) {
636+
cc.log(cc.logLevelEnums.INFO, `set_id, Changing the device ID to: [${newId}]`);
637+
if (newId === null || newId === undefined || newId === "" || typeof newId !== "string") {
638+
cc.log(cc.logLevelEnums.WARNING, "set_id, The provided id is not a valid ID");
639+
return;
640+
}
641+
if (Countly.get_device_id_type() === cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED) {
642+
// change ID without merge as current ID is Dev supplied, so not first login
643+
Countly.change_id(newId, false);
644+
}
645+
else {
646+
// change ID with merge as current ID is not Dev supplied*/
647+
Countly.change_id(newId, true);
648+
}
649+
};
650+
630651
/**
631652
* Change current user/device id
632653
* @param {string} newId - new user/device ID to use

test/helpers/helper_functions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,5 @@ module.exports = {
284284
validateUserDetails,
285285
viewEventValidator,
286286
doesFileStoragePathsExist,
287+
requestBaseParamValidator,
287288
};

test/tests_device_id.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* eslint-disable no-console */
2+
var assert = require("assert");
3+
var Countly = require("../lib/countly");
4+
var cc = require("../lib/countly-common");
5+
var hp = require("./helpers/helper_functions");
6+
7+
function initMain(deviceId, eraseID) {
8+
Countly.init({
9+
app_key: "YOUR_APP_KEY",
10+
url: "https://try.count.ly",
11+
device_id: deviceId,
12+
max_events: -1,
13+
// debug: true,
14+
clear_stored_device_id: eraseID,
15+
});
16+
}
17+
function validateSdkGeneratedId(providedDeviceId) {
18+
assert.ok(providedDeviceId);
19+
assert.equal(providedDeviceId.length, 36);
20+
assert.ok(cc.isUUID(providedDeviceId));
21+
assert.equal(Countly.get_device_id(), providedDeviceId);
22+
assert.equal(Countly.get_device_id_type(), Countly.DeviceIdType.SDK_GENERATED);
23+
}
24+
25+
function validateDeveloperSuppliedId(providedDeviceId) {
26+
assert.equal(Countly.get_device_id_type(), Countly.DeviceIdType.DEVELOPER_SUPPLIED);
27+
assert.equal(Countly.get_device_id(), providedDeviceId);
28+
}
29+
30+
describe("Device ID tests", () => {
31+
beforeEach(async() => {
32+
await hp.clearStorage();
33+
});
34+
35+
it("1- set_id with SDK generated to developer supplied", (done) => {
36+
// initialize SDK
37+
initMain(undefined);
38+
validateSdkGeneratedId(Countly.get_device_id());
39+
var oldId = Countly.get_device_id();
40+
Countly.set_id("ID");
41+
validateDeveloperSuppliedId("ID");
42+
setTimeout(() => {
43+
// validate that merge request is generated
44+
var RQ = hp.readRequestQueue();
45+
assert.equal(RQ.length, 1);
46+
hp.requestBaseParamValidator(RQ[0]);
47+
assert.equal(RQ[0].old_device_id, oldId);
48+
done();
49+
}, hp.sWait);
50+
});
51+
52+
it("2- set_id with developer supplied to developer supplied", (done) => {
53+
// initialize SDK
54+
initMain("ID2");
55+
validateDeveloperSuppliedId("ID2");
56+
Countly.set_id("ID");
57+
validateDeveloperSuppliedId("ID");
58+
setTimeout(() => {
59+
// validate that no merge request is generated and the existing request is begin session
60+
var RQ = hp.readRequestQueue();
61+
assert.equal(RQ.length, 1);
62+
hp.sessionRequestValidator(RQ[0]);
63+
done();
64+
}, hp.sWait);
65+
});
66+
67+
it("3- set_id with same custom id", (done) => {
68+
// initialize SDK
69+
initMain("ID");
70+
validateDeveloperSuppliedId("ID");
71+
Countly.set_id("ID");
72+
validateDeveloperSuppliedId("ID");
73+
done();
74+
});
75+
76+
it("4- set_id with same sdk generated id", (done) => {
77+
// initialize SDK
78+
initMain(undefined);
79+
var id = Countly.get_device_id();
80+
validateSdkGeneratedId(id);
81+
Countly.set_id(id);
82+
// so that the type is not converted to developer_supplied
83+
validateSdkGeneratedId(id);
84+
done();
85+
});
86+
87+
it("5- set_id with invalid ids", (done) => {
88+
// initialize SDK
89+
initMain(undefined);
90+
var id = Countly.get_device_id();
91+
validateSdkGeneratedId(id);
92+
Countly.set_id(undefined);
93+
validateSdkGeneratedId(id);
94+
95+
Countly.set_id(null);
96+
validateSdkGeneratedId(id);
97+
98+
Countly.set_id("");
99+
validateSdkGeneratedId(id);
100+
done();
101+
});
102+
});

test/tests_device_id_type.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("Device ID type tests", () => {
5858
beforeEach(async() => {
5959
await hp.clearStorage();
6060
});
61-
it("1.Generated device ID", (done) => {
61+
it("1- Generated device ID", (done) => {
6262
// initialize SDK
6363
initMain(undefined);
6464
Countly.begin_session();
@@ -71,7 +71,8 @@ describe("Device ID type tests", () => {
7171
done();
7272
}, hp.sWait);
7373
});
74-
it("2.Developer supplied device ID", (done) => {
74+
75+
it("2- Developer supplied device ID", (done) => {
7576
// initialize SDK
7677
initMain("ID");
7778
Countly.begin_session();
@@ -84,7 +85,8 @@ describe("Device ID type tests", () => {
8485
done();
8586
}, hp.sWait);
8687
});
87-
it("3.With stored dev ID and no new ID", (done) => {
88+
89+
it("3- With stored dev ID and no new ID", (done) => {
8890
// initialize SDK
8991
initMain("ID");
9092
Countly.begin_session();
@@ -105,7 +107,8 @@ describe("Device ID type tests", () => {
105107
}, hp.sWait);
106108
}, hp.sWait);
107109
});
108-
it("4.With stored dev ID and with new ID", (done) => {
110+
111+
it("4- With stored dev ID and with new ID", (done) => {
109112
// initialize SDK
110113
initMain("ID");
111114
Countly.begin_session();
@@ -126,7 +129,8 @@ describe("Device ID type tests", () => {
126129
}, hp.sWait);
127130
}, hp.sWait);
128131
});
129-
it("5.With stored generated ID and no new ID", (done) => {
132+
133+
it("5- With stored generated ID and no new ID", (done) => {
130134
// initialize SDK
131135
initMain(undefined);
132136
Countly.begin_session();
@@ -149,7 +153,8 @@ describe("Device ID type tests", () => {
149153
}, hp.sWait);
150154
}, hp.sWait);
151155
});
152-
it("6.With stored generated ID and with new ID", (done) => {
156+
157+
it("6- With stored generated ID and with new ID", (done) => {
153158
// initialize SDK
154159
initMain(undefined);
155160
Countly.begin_session();
@@ -172,7 +177,8 @@ describe("Device ID type tests", () => {
172177
}, hp.sWait);
173178
}, hp.sWait);
174179
});
175-
it("7.With stored dev ID and no new ID, flag set", (done) => {
180+
181+
it("7- With stored dev ID and no new ID, flag set", (done) => {
176182
// initialize SDK
177183
initMain("ID");
178184
Countly.begin_session();
@@ -194,7 +200,7 @@ describe("Device ID type tests", () => {
194200
}, hp.sWait);
195201
});
196202

197-
it("8.With stored dev ID and with new ID, flag set", (done) => {
203+
it("8- With stored dev ID and with new ID, flag set", (done) => {
198204
setTimeout(() => {
199205
// initialize SDK
200206
initMain("ID");
@@ -217,7 +223,8 @@ describe("Device ID type tests", () => {
217223
}, hp.lWait);
218224
}, hp.lWait);
219225
});
220-
it("9.With stored sdk ID and no new ID, flag set", (done) => {
226+
227+
it("9- With stored sdk ID and no new ID, flag set", (done) => {
221228
// initialize SDK
222229
initMain(undefined);
223230
Countly.begin_session();
@@ -241,7 +248,7 @@ describe("Device ID type tests", () => {
241248
}, hp.sWait);
242249
});
243250

244-
it("10.With stored sdk ID and with new ID, flag set", (done) => {
251+
it("10- With stored sdk ID and with new ID, flag set", (done) => {
245252
// initialize SDK
246253
initMain(undefined);
247254
Countly.begin_session();
@@ -264,7 +271,8 @@ describe("Device ID type tests", () => {
264271
}, hp.sWait);
265272
}, hp.sWait);
266273
});
267-
it("11.Change generated device ID", (done) => {
274+
275+
it("11- Change generated device ID", (done) => {
268276
// initialize SDK
269277
initMain(undefined);
270278
Countly.change_id("changedID");
@@ -280,7 +288,8 @@ describe("Device ID type tests", () => {
280288
}, hp.sWait);
281289
}, hp.sWait);
282290
});
283-
it("12.Change developer supplied device ID", (done) => {
291+
292+
it("12- Change developer supplied device ID", (done) => {
284293
// initialize SDK
285294
initMain("ID");
286295
Countly.change_id("changedID");
@@ -296,4 +305,12 @@ describe("Device ID type tests", () => {
296305
}, hp.sWait);
297306
}, hp.sWait);
298307
});
308+
309+
it("13- Check new DeviceIdType interface is equal to common interface", (done) => {
310+
setTimeout(() => {
311+
assert.equal(Countly.DeviceIdType.DEVELOPER_SUPPLIED, cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED);
312+
assert.equal(Countly.DeviceIdType.SDK_GENERATED, cc.deviceIdTypeEnums.SDK_GENERATED);
313+
done();
314+
});
315+
});
299316
});

0 commit comments

Comments
 (0)