Skip to content

Commit d1f65f2

Browse files
authored
[Sdk 756] Device ID order (#194)
* device id order * changelog and ascii table * added comments * changed device ID priority * added comments and minimized the checks * added type to one test * index comment erased * fixed utm bug * utm bug fixed and commmetns * changelog and minify
1 parent ab7cfeb commit d1f65f2

File tree

7 files changed

+808
-191
lines changed

7 files changed

+808
-191
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## 22.02.0
2+
- !! Major breaking change !! Device ID provided during the init will be ignored if a device ID was provided previously
3+
- Added a new init time flag which erases the previously stored device ID. This allows to set new device ID during init
24
- Added a new functionality with which it is possible to check the device ID type
35
- Now it appends the device ID type with each request
46
- Fixed a bug where custom utm tags were being overridden

cypress/integration/device_id.js

+573-21
Large diffs are not rendered by default.

cypress/support/commands.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "./index";
2+
import "cypress-localstorage-commands";
23

34
var hp = require("./helper");
45

cypress/support/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
import "./commands";
22
import "../../lib/countly";
3-
import "cypress-localstorage-commands";

lib/countly.js

+116-57
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
this.getSearchQuery = ob.getSearchQuery || Countly.getSearchQuery;
232232
this.DeviceIdType = Countly.DeviceIdType;
233233
this.namespace = getConfig("namespace", ob, "");
234+
this.clearStoredId = getConfig("clear_stored_id", ob, false);
234235
this.app_key = getConfig("app_key", ob, null);
235236
this.onload = getConfig("onload", ob, []);
236237
this.utm = getConfig("utm", ob, { source: true, medium: true, campaign: true, term: true, content: true });
@@ -287,20 +288,10 @@
287288

288289
migrate();
289290

290-
if (!offlineMode) {
291-
this.device_id = getConfig("device_id", ob, getStoredIdOrGenerateId());
292-
if (ob && Object.keys(ob).length) {
293-
if (ob.device_id !== undefined) {
294-
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
295-
}
296-
}
297-
else if (Countly.device_id !== undefined) {
298-
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
299-
}
300-
}
301-
else if (!this.device_id) {
302-
this.device_id = "[CLY]_temp_id";
303-
deviceIdType = DeviceIdTypeInternalEnums.TEMPORARY_ID;
291+
if (this.clearStoredId) {
292+
log(logLevelEnums.INFO, "initialize, Clearing the device ID storage");
293+
localStorage.removeItem(this.app_key + "/cly_id");
294+
localStorage.removeItem(this.app_key + "/cly_id_type");
304295
}
305296

306297
requestQueue = setGetValueInStorage("cly_queue") || [];
@@ -341,59 +332,126 @@
341332
}
342333
}
343334

344-
if (!this.ignore_visitor) {
345-
log(logLevelEnums.INFO, "initialize, Countly initialized");
346-
var searchQuery = self.getSearchQuery();
347-
if (searchQuery) {
348-
var parts = searchQuery.substring(1).split("&");
349-
var utms = {};
350-
var hasUTM = false;
351-
for (var i = 0; i < parts.length; i++) {
352-
var nv = parts[i].split("=");
353-
if (nv[0] === "cly_id") {
354-
setGetValueInStorage("cly_cmp_id", nv[1]);
355-
}
356-
else if (nv[0] === "cly_uid") {
357-
setGetValueInStorage("cly_cmp_uid", nv[1]);
358-
}
359-
else if (nv[0] === "cly_device_id") {
360-
this.device_id = nv[1];
361-
deviceIdType = DeviceIdTypeInternalEnums.URL_PROVIDED;
362-
}
363-
else if ((nv[0] + "").indexOf("utm_") === 0 && this.utm[nv[0].replace("utm_", "")]) {
364-
utms[nv[0].replace("utm_", "")] = nv[1];
365-
hasUTM = true;
366-
}
335+
if (this.ignore_visitor) {
336+
log(logLevelEnums.WARNING, "initialize, Ignoring this visitor");
337+
return;
338+
}
339+
340+
var deviceIdParamValue = null;
341+
log(logLevelEnums.INFO, "initialize, Countly initialized");
342+
var searchQuery = self.getSearchQuery();
343+
var hasUTM = false;
344+
var utms = {};
345+
if (searchQuery) {
346+
var parts = searchQuery.substring(1).split("&");
347+
for (var i = 0; i < parts.length; i++) {
348+
var nv = parts[i].split("=");
349+
if (nv[0] === "cly_id") {
350+
setGetValueInStorage("cly_cmp_id", nv[1]);
367351
}
368-
if (hasUTM) {
369-
for (var tag in this.utm) {
370-
if (utms[tag]) {
371-
this.userData.set("utm_" + tag, utms[tag]);
372-
}
373-
else {
374-
this.userData.unset("utm_" + tag);
375-
}
376-
}
377-
this.userData.save();
352+
else if (nv[0] === "cly_uid") {
353+
setGetValueInStorage("cly_cmp_uid", nv[1]);
354+
}
355+
else if (nv[0] === "cly_device_id") {
356+
deviceIdParamValue = nv[1];
357+
}
358+
else if ((nv[0] + "").indexOf("utm_") === 0 && this.utm[nv[0].replace("utm_", "")]) {
359+
utms[nv[0].replace("utm_", "")] = nv[1];
360+
hasUTM = true;
378361
}
379362
}
363+
}
364+
365+
// flag that indicates that the offline mode was enabled at the end of the previous app session
366+
var tempIdModeWasEnabled = (setGetValueInStorage("cly_id") === "[CLY]_temp_id");
367+
var developerSetDeviceId = getConfig("device_id", ob, undefined);
380368

381-
if (!offlineMode) {
382-
if (this.device_id !== setGetValueInStorage("cly_id")) {
383-
setGetValueInStorage("cly_id", this.device_id);
384-
setGetValueInStorage("cly_id_type", deviceIdType);
369+
// check if there wqs stored ID
370+
if (setGetValueInStorage("cly_id") && !tempIdModeWasEnabled) {
371+
this.device_id = setGetValueInStorage("cly_id");
372+
log(logLevelEnums.INFO, "initialize, Set the stored device ID");
373+
deviceIdType = setGetValueInStorage("cly_id_type");
374+
if (!deviceIdType) {
375+
log(logLevelEnums.INFO, "initialize, No device ID type info from the previous session, falling back to DEVELOPER_SUPPLIED");
376+
// there is a device ID saved but there is no device ID information saved
377+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
378+
}
379+
}
380+
// if not check if device ID was provided with URL
381+
else if (deviceIdParamValue !== null) {
382+
log(logLevelEnums.INFO, "initialize, Device ID set by URL");
383+
this.device_id = deviceIdParamValue;
384+
deviceIdType = DeviceIdTypeInternalEnums.URL_PROVIDED;
385+
}
386+
// if not check if developer provided any ID
387+
else if (developerSetDeviceId) {
388+
log(logLevelEnums.INFO, "initialize, Device ID set by developer");
389+
this.device_id = developerSetDeviceId;
390+
if (ob && Object.keys(ob).length) {
391+
if (ob.device_id !== undefined) {
392+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
393+
}
394+
}
395+
else if (Countly.device_id !== undefined) {
396+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
397+
}
398+
}
399+
// if not check if offline mode is on
400+
else if (offlineMode || tempIdModeWasEnabled) {
401+
this.device_id = "[CLY]_temp_id";
402+
deviceIdType = DeviceIdTypeInternalEnums.TEMPORARY_ID;
403+
if (offlineMode && tempIdModeWasEnabled) {
404+
log(logLevelEnums.INFO, "initialize, Temp ID set, continuing offline mode from previous app session");
405+
}
406+
else if (offlineMode && !tempIdModeWasEnabled) {
407+
// this if we get here then it means either first init we enter offline mode or we cleared the device ID during the init and still user entered the offline mode
408+
log(logLevelEnums.INFO, "initialize, Temp ID set, entering offline mode");
409+
}
410+
else {
411+
// no device ID was provided, no offline mode flag was provided, in the previous app session we entered offline mode and now we carry on
412+
offlineMode = true;
413+
log(logLevelEnums.INFO, "initialize, Temp ID set, enabling offline mode");
414+
}
415+
}
416+
// if all fails generate an ID
417+
else {
418+
log(logLevelEnums.INFO, "initialize, Generating the device ID");
419+
this.device_id = getConfig("device_id", ob, getStoredIdOrGenerateId());
420+
if (ob && Object.keys(ob).length) {
421+
if (ob.device_id !== undefined) {
422+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
385423
}
386424
}
425+
else if (Countly.device_id !== undefined) {
426+
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
427+
}
428+
}
387429

388-
notifyLoaders();
430+
// Store the device ID and device ID type
431+
setGetValueInStorage("cly_id", this.device_id);
432+
setGetValueInStorage("cly_id_type", deviceIdType);
389433

390-
setTimeout(function() {
391-
heartBeat();
392-
if (self.remote_config) {
393-
self.fetch_remote_config(self.remote_config);
434+
// as we have assigned the device ID now we can save the tags
435+
if (hasUTM) {
436+
for (var tag in this.utm) {
437+
if (utms[tag]) {
438+
this.userData.set("utm_" + tag, utms[tag]);
394439
}
395-
}, 1);
440+
else {
441+
this.userData.unset("utm_" + tag);
442+
}
443+
}
444+
this.userData.save();
396445
}
446+
447+
notifyLoaders();
448+
449+
setTimeout(function() {
450+
heartBeat();
451+
if (self.remote_config) {
452+
self.fetch_remote_config(self.remote_config);
453+
}
454+
}, 1);
397455
document.documentElement.setAttribute("data-countly-useragent", navigator.userAgent);
398456
};
399457

@@ -3754,6 +3812,7 @@
37543812
* @param {string} conf.app_key - app key for your app created in Countly
37553813
* @param {string} conf.device_id - to identify a visitor, will be auto generated if not provided
37563814
* @param {string} conf.url - your Countly server url, you can use your server URL or IP here
3815+
* @param {boolean} [conf.clear_stored_id=false] - set it to true if you want to erase previously stored device ID from the local storage
37573816
* @param {string} [conf.app_version=0.0] - the version of your app or website
37583817
* @param {string=} conf.country_code - country code for your visitor
37593818
* @param {string=} conf.city - name of the city of your visitor

0 commit comments

Comments
 (0)