Skip to content

Commit

Permalink
Merge pull request #109 from Countly/set_id
Browse files Browse the repository at this point in the history
feat: set_id method for the node
  • Loading branch information
turtledreams authored Nov 11, 2024
2 parents c966016 + 9d03126 commit 51b1032
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## XX.XX.XX
- Added a new method `set_id(newDeviceId)` for managing device ID changes according to the device ID Type
- Added `DeviceIdType` enums to be used to evaluate the device ID type.

## 24.10.0
- Default max segmentation value count changed from 30 to 100
- Mitigated an issue where SDK could create an unintended dump file
Expand Down
21 changes: 21 additions & 0 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var CountlyStorage = require("./countly-storage");

var Countly = {};
Countly.StorageTypes = cc.storageTypeEnums;
Countly.DeviceIdType = cc.deviceIdTypeEnums;
Countly.Bulk = Bulk;
(function() {
var SDK_VERSION = "24.10.0";
Expand Down Expand Up @@ -627,6 +628,26 @@ Countly.Bulk = Bulk;
return Countly.device_id;
};

/**
* Changes the current device ID according to the device ID type (the preffered method)
* @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
* */
Countly.set_id = function(newId) {
cc.log(cc.logLevelEnums.INFO, `set_id, Changing the device ID to: [${newId}]`);
if (newId === null || newId === undefined || newId === "" || typeof newId !== "string") {
cc.log(cc.logLevelEnums.WARNING, "set_id, The provided id is not a valid ID");
return;
}
if (Countly.get_device_id_type() === cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED) {
// change ID without merge as current ID is Dev supplied, so not first login
Countly.change_id(newId, false);
}
else {
// change ID with merge as current ID is not Dev supplied*/
Countly.change_id(newId, true);
}
};

/**
* Change current user/device id
* @param {string} newId - new user/device ID to use
Expand Down
1 change: 1 addition & 0 deletions test/helpers/helper_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,5 @@ module.exports = {
validateUserDetails,
viewEventValidator,
doesFileStoragePathsExist,
requestBaseParamValidator,
};
102 changes: 102 additions & 0 deletions test/tests_device_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* eslint-disable no-console */
var assert = require("assert");
var Countly = require("../lib/countly");
var cc = require("../lib/countly-common");
var hp = require("./helpers/helper_functions");

function initMain(deviceId, eraseID) {
Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
device_id: deviceId,
max_events: -1,
// debug: true,
clear_stored_device_id: eraseID,
});
}
function validateSdkGeneratedId(providedDeviceId) {
assert.ok(providedDeviceId);
assert.equal(providedDeviceId.length, 36);
assert.ok(cc.isUUID(providedDeviceId));
assert.equal(Countly.get_device_id(), providedDeviceId);
assert.equal(Countly.get_device_id_type(), Countly.DeviceIdType.SDK_GENERATED);
}

function validateDeveloperSuppliedId(providedDeviceId) {
assert.equal(Countly.get_device_id_type(), Countly.DeviceIdType.DEVELOPER_SUPPLIED);
assert.equal(Countly.get_device_id(), providedDeviceId);
}

describe("Device ID tests", () => {
beforeEach(async() => {
await hp.clearStorage();
});

it("1- set_id with SDK generated to developer supplied", (done) => {
// initialize SDK
initMain(undefined);
validateSdkGeneratedId(Countly.get_device_id());
var oldId = Countly.get_device_id();
Countly.set_id("ID");
validateDeveloperSuppliedId("ID");
setTimeout(() => {
// validate that merge request is generated
var RQ = hp.readRequestQueue();
assert.equal(RQ.length, 1);
hp.requestBaseParamValidator(RQ[0]);
assert.equal(RQ[0].old_device_id, oldId);
done();
}, hp.sWait);
});

it("2- set_id with developer supplied to developer supplied", (done) => {
// initialize SDK
initMain("ID2");
validateDeveloperSuppliedId("ID2");
Countly.set_id("ID");
validateDeveloperSuppliedId("ID");
setTimeout(() => {
// validate that no merge request is generated and the existing request is begin session
var RQ = hp.readRequestQueue();
assert.equal(RQ.length, 1);
hp.sessionRequestValidator(RQ[0]);
done();
}, hp.sWait);
});

it("3- set_id with same custom id", (done) => {
// initialize SDK
initMain("ID");
validateDeveloperSuppliedId("ID");
Countly.set_id("ID");
validateDeveloperSuppliedId("ID");
done();
});

it("4- set_id with same sdk generated id", (done) => {
// initialize SDK
initMain(undefined);
var id = Countly.get_device_id();
validateSdkGeneratedId(id);
Countly.set_id(id);
// so that the type is not converted to developer_supplied
validateSdkGeneratedId(id);
done();
});

it("5- set_id with invalid ids", (done) => {
// initialize SDK
initMain(undefined);
var id = Countly.get_device_id();
validateSdkGeneratedId(id);
Countly.set_id(undefined);
validateSdkGeneratedId(id);

Countly.set_id(null);
validateSdkGeneratedId(id);

Countly.set_id("");
validateSdkGeneratedId(id);
done();
});
});
41 changes: 29 additions & 12 deletions test/tests_device_id_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("Device ID type tests", () => {
beforeEach(async() => {
await hp.clearStorage();
});
it("1.Generated device ID", (done) => {
it("1- Generated device ID", (done) => {
// initialize SDK
initMain(undefined);
Countly.begin_session();
Expand All @@ -71,7 +71,8 @@ describe("Device ID type tests", () => {
done();
}, hp.sWait);
});
it("2.Developer supplied device ID", (done) => {

it("2- Developer supplied device ID", (done) => {
// initialize SDK
initMain("ID");
Countly.begin_session();
Expand All @@ -84,7 +85,8 @@ describe("Device ID type tests", () => {
done();
}, hp.sWait);
});
it("3.With stored dev ID and no new ID", (done) => {

it("3- With stored dev ID and no new ID", (done) => {
// initialize SDK
initMain("ID");
Countly.begin_session();
Expand All @@ -105,7 +107,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("4.With stored dev ID and with new ID", (done) => {

it("4- With stored dev ID and with new ID", (done) => {
// initialize SDK
initMain("ID");
Countly.begin_session();
Expand All @@ -126,7 +129,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("5.With stored generated ID and no new ID", (done) => {

it("5- With stored generated ID and no new ID", (done) => {
// initialize SDK
initMain(undefined);
Countly.begin_session();
Expand All @@ -149,7 +153,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("6.With stored generated ID and with new ID", (done) => {

it("6- With stored generated ID and with new ID", (done) => {
// initialize SDK
initMain(undefined);
Countly.begin_session();
Expand All @@ -172,7 +177,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("7.With stored dev ID and no new ID, flag set", (done) => {

it("7- With stored dev ID and no new ID, flag set", (done) => {
// initialize SDK
initMain("ID");
Countly.begin_session();
Expand All @@ -194,7 +200,7 @@ describe("Device ID type tests", () => {
}, hp.sWait);
});

it("8.With stored dev ID and with new ID, flag set", (done) => {
it("8- With stored dev ID and with new ID, flag set", (done) => {
setTimeout(() => {
// initialize SDK
initMain("ID");
Expand All @@ -217,7 +223,8 @@ describe("Device ID type tests", () => {
}, hp.lWait);
}, hp.lWait);
});
it("9.With stored sdk ID and no new ID, flag set", (done) => {

it("9- With stored sdk ID and no new ID, flag set", (done) => {
// initialize SDK
initMain(undefined);
Countly.begin_session();
Expand All @@ -241,7 +248,7 @@ describe("Device ID type tests", () => {
}, hp.sWait);
});

it("10.With stored sdk ID and with new ID, flag set", (done) => {
it("10- With stored sdk ID and with new ID, flag set", (done) => {
// initialize SDK
initMain(undefined);
Countly.begin_session();
Expand All @@ -264,7 +271,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("11.Change generated device ID", (done) => {

it("11- Change generated device ID", (done) => {
// initialize SDK
initMain(undefined);
Countly.change_id("changedID");
Expand All @@ -280,7 +288,8 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});
it("12.Change developer supplied device ID", (done) => {

it("12- Change developer supplied device ID", (done) => {
// initialize SDK
initMain("ID");
Countly.change_id("changedID");
Expand All @@ -296,4 +305,12 @@ describe("Device ID type tests", () => {
}, hp.sWait);
}, hp.sWait);
});

it("13- Check new DeviceIdType interface is equal to common interface", (done) => {
setTimeout(() => {
assert.equal(Countly.DeviceIdType.DEVELOPER_SUPPLIED, cc.deviceIdTypeEnums.DEVELOPER_SUPPLIED);
assert.equal(Countly.DeviceIdType.SDK_GENERATED, cc.deviceIdTypeEnums.SDK_GENERATED);
done();
});
});
});

0 comments on commit 51b1032

Please sign in to comment.