|
231 | 231 | this.getSearchQuery = ob.getSearchQuery || Countly.getSearchQuery;
|
232 | 232 | this.DeviceIdType = Countly.DeviceIdType;
|
233 | 233 | this.namespace = getConfig("namespace", ob, "");
|
| 234 | + this.clearStoredId = getConfig("clear_stored_id", ob, false); |
234 | 235 | this.app_key = getConfig("app_key", ob, null);
|
235 | 236 | this.onload = getConfig("onload", ob, []);
|
236 | 237 | this.utm = getConfig("utm", ob, { source: true, medium: true, campaign: true, term: true, content: true });
|
|
287 | 288 |
|
288 | 289 | migrate();
|
289 | 290 |
|
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"); |
304 | 295 | }
|
305 | 296 |
|
306 | 297 | requestQueue = setGetValueInStorage("cly_queue") || [];
|
|
341 | 332 | }
|
342 | 333 | }
|
343 | 334 |
|
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]); |
367 | 351 | }
|
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; |
378 | 361 | }
|
379 | 362 | }
|
| 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); |
380 | 368 |
|
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; |
385 | 423 | }
|
386 | 424 | }
|
| 425 | + else if (Countly.device_id !== undefined) { |
| 426 | + deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED; |
| 427 | + } |
| 428 | + } |
387 | 429 |
|
388 |
| - notifyLoaders(); |
| 430 | + // Store the device ID and device ID type |
| 431 | + setGetValueInStorage("cly_id", this.device_id); |
| 432 | + setGetValueInStorage("cly_id_type", deviceIdType); |
389 | 433 |
|
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]); |
394 | 439 | }
|
395 |
| - }, 1); |
| 440 | + else { |
| 441 | + this.userData.unset("utm_" + tag); |
| 442 | + } |
| 443 | + } |
| 444 | + this.userData.save(); |
396 | 445 | }
|
| 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); |
397 | 455 | document.documentElement.setAttribute("data-countly-useragent", navigator.userAgent);
|
398 | 456 | };
|
399 | 457 |
|
|
3754 | 3812 | * @param {string} conf.app_key - app key for your app created in Countly
|
3755 | 3813 | * @param {string} conf.device_id - to identify a visitor, will be auto generated if not provided
|
3756 | 3814 | * @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 |
3757 | 3816 | * @param {string} [conf.app_version=0.0] - the version of your app or website
|
3758 | 3817 | * @param {string=} conf.country_code - country code for your visitor
|
3759 | 3818 | * @param {string=} conf.city - name of the city of your visitor
|
|
0 commit comments