@reservamos/browser-analytics 1.1.7 → 1.1.8-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13427,7 +13427,7 @@ function observeIdentifyResult(stage, report, onTimeout) {
13427
13427
  }, 5e3);
13428
13428
  return { complete };
13429
13429
  }
13430
- const version = "1.1.7";
13430
+ const version = "1.1.8-alpha.0";
13431
13431
  const MIXPANEL_DISTINCT_ID_CACHE_KEY = "mp_distinct_id";
13432
13432
  const SDK_SUPER_PROPERTIES = {
13433
13433
  "Event Source": "sdk",
@@ -13448,11 +13448,7 @@ function init$2(mixpanelToken, debug = false, proxyUrl) {
13448
13448
  }
13449
13449
  function identify$1(userId, properties, report) {
13450
13450
  mixpanel.identify(userId);
13451
- try {
13452
- updateCachedDistinctId(userId);
13453
- } catch {
13454
- report == null ? void 0 : report("cache", "unavailable");
13455
- }
13451
+ if (!updateCachedDistinctId(userId)) report == null ? void 0 : report("cache", "unavailable");
13456
13452
  if (!report) {
13457
13453
  mixpanel.people.set(properties);
13458
13454
  return;
@@ -13491,18 +13487,25 @@ function track(eventName, properties) {
13491
13487
  mixpanel.track(eventName, { ...properties, "Reservamos Version": version });
13492
13488
  }
13493
13489
  const getMixpanelDistinctId = () => {
13494
- const cachedId = localStorage.getItem(MIXPANEL_DISTINCT_ID_CACHE_KEY);
13495
- if (mixpanel && mixpanel.get_distinct_id) {
13496
- const distinctId = mixpanel.get_distinct_id();
13490
+ var _a;
13491
+ const distinctId = (_a = mixpanel == null ? void 0 : mixpanel.get_distinct_id) == null ? void 0 : _a.call(mixpanel);
13492
+ if (distinctId) {
13497
13493
  updateCachedDistinctId(distinctId);
13498
13494
  return distinctId;
13499
- } else if (cachedId) {
13500
- return cachedId;
13501
13495
  }
13502
- return null;
13496
+ try {
13497
+ return localStorage.getItem(MIXPANEL_DISTINCT_ID_CACHE_KEY);
13498
+ } catch {
13499
+ return null;
13500
+ }
13503
13501
  };
13504
13502
  function updateCachedDistinctId(distinctId) {
13505
- localStorage.setItem(MIXPANEL_DISTINCT_ID_CACHE_KEY, distinctId);
13503
+ try {
13504
+ localStorage.setItem(MIXPANEL_DISTINCT_ID_CACHE_KEY, distinctId);
13505
+ return true;
13506
+ } catch {
13507
+ return false;
13508
+ }
13506
13509
  }
13507
13510
  const mixpanelService = {
13508
13511
  init: init$2,
@@ -13528,8 +13531,8 @@ const getCachedFingerprint = () => {
13528
13531
  return readCachedFingerprint();
13529
13532
  };
13530
13533
  function readCachedFingerprint(context) {
13531
- const { cacheName = DEFAULT_CACHE_NAME } = window.fingerprintConfig;
13532
13534
  try {
13535
+ const { cacheName = DEFAULT_CACHE_NAME } = window.fingerprintConfig ?? {};
13533
13536
  const cachedData = JSON.parse(localStorage.getItem(cacheName) || "null");
13534
13537
  if (cachedData && cachedData.expiresAt > Date.now()) {
13535
13538
  return cachedData.fingerprint;
@@ -13592,17 +13595,11 @@ function getFingerprintForIdentify(context) {
13592
13595
  return readFingerprint(false, context);
13593
13596
  }
13594
13597
  async function readFingerprint(cacheOnly, context) {
13595
- if (!window.fingerprintConfig) {
13596
- if (cacheOnly) {
13597
- return "";
13598
- }
13599
- throw new Error("Fingerprint configuration is not initialized.");
13600
- }
13601
13598
  const cachedFingerprint = readCachedFingerprint(context);
13602
13599
  if (cachedFingerprint) {
13603
13600
  return cachedFingerprint;
13604
13601
  }
13605
- if (cacheOnly || !isFingerprintReady()) {
13602
+ if (cacheOnly || !window.fingerprintConfig || !isFingerprintReady()) {
13606
13603
  return "";
13607
13604
  }
13608
13605
  const { visitorId } = await requestIdentification(false, context);
@@ -14089,21 +14086,37 @@ async function init$1(config) {
14089
14086
  appName: appName2
14090
14087
  } = config;
14091
14088
  await mixpanelService.init(mixpanelToken, debug, mixpanelProxyUrl);
14092
- try {
14093
- await fingerprintService.initFingerprint(
14094
- identificationKey,
14095
- identifyProxyUrl
14096
- );
14097
- } catch (error) {
14098
- console.error("Error initializing identification service:", error);
14099
- }
14100
- await geolocationService.init();
14101
14089
  configService.setEnvironment(isSandbox ? "sandbox" : "prod");
14102
14090
  if (appName2) {
14103
14091
  configService.setAppName(appName2);
14104
14092
  }
14105
14093
  a$1.setConfig(configService.getCoreAPIConfig());
14106
14094
  onLoaded();
14095
+ await Promise.all([
14096
+ initializeOptionalStage(
14097
+ "fingerprint",
14098
+ () => fingerprintService.initFingerprint(identificationKey, identifyProxyUrl)
14099
+ ),
14100
+ initializeOptionalStage("geolocation", () => geolocationService.init())
14101
+ ]);
14102
+ }
14103
+ function initializeOptionalStage(stage, initialize) {
14104
+ return new Promise((resolve) => {
14105
+ let active = true;
14106
+ const finish = (reason) => {
14107
+ if (!active) return;
14108
+ active = false;
14109
+ clearTimeout(timer);
14110
+ if (reason)
14111
+ console.error("SDK initialization diagnostic", { stage, reason });
14112
+ resolve();
14113
+ };
14114
+ const timer = setTimeout(() => finish("timeout"), 3e3);
14115
+ void Promise.resolve().then(initialize).then(
14116
+ () => finish(),
14117
+ () => finish("failed")
14118
+ );
14119
+ });
14107
14120
  }
14108
14121
  function isTrackerReady() {
14109
14122
  return mixpanelService.isReady();
@@ -14359,32 +14372,69 @@ function trackEventError(eventName, error, payload) {
14359
14372
  }
14360
14373
  });
14361
14374
  }
14362
- async function tryTrackEvent(trackEventCallback) {
14363
- if (mixpanelService.isReady()) {
14375
+ async function tryTrackEvent(trackEventCallback, eventName = "Track Event Error") {
14376
+ if (!mixpanelService.isReady()) {
14377
+ const ready = await new Promise((resolve) => {
14378
+ const finish = (isReady2) => {
14379
+ clearTimeout(timer);
14380
+ window.removeEventListener("Tracker Ready", onReady);
14381
+ resolve(isReady2);
14382
+ };
14383
+ const onReady = () => {
14384
+ if (mixpanelService.isReady()) finish(true);
14385
+ };
14386
+ const timer = setTimeout(() => finish(mixpanelService.isReady()), 5e3);
14387
+ window.addEventListener("Tracker Ready", onReady);
14388
+ onReady();
14389
+ });
14390
+ if (!ready) {
14391
+ console.error("Tracking diagnostic", {
14392
+ eventName,
14393
+ stage: "readiness",
14394
+ reason: "timeout"
14395
+ });
14396
+ return;
14397
+ }
14398
+ }
14399
+ try {
14364
14400
  await trackEventCallback();
14365
- } else {
14366
- window.addEventListener("Tracker Ready", async () => {
14367
- await trackEventCallback();
14401
+ } catch {
14402
+ console.error("Tracking diagnostic", {
14403
+ eventName,
14404
+ stage: "submission",
14405
+ reason: "failed"
14368
14406
  });
14369
14407
  }
14370
14408
  }
14371
- async function trackEvent(eventName, eventProperties, meta = {}) {
14372
- if (!mixpanelService.isReady()) {
14373
- throw new Error("Mixpanel is not initialized.");
14374
- }
14409
+ function trackEvent(eventName, eventProperties, meta = {}) {
14410
+ return tryTrackEvent(
14411
+ () => submitEvent(eventName, eventProperties, meta),
14412
+ eventName
14413
+ );
14414
+ }
14415
+ async function submitEvent(eventName, eventProperties, meta) {
14375
14416
  try {
14376
14417
  validatorService.parseEventProps(eventName, eventProperties);
14377
14418
  const distinctId = mixpanelService.getMixpanelDistinctId();
14378
14419
  const botDetectionEnabled = botDetectionRelayService.isEnabledFor(eventName);
14379
- let fingerprint;
14380
- let fingerprintRequestId;
14381
- if (botDetectionEnabled && fingerprintService.isFingerprintReady()) {
14382
- const identification = await fingerprintService.identifyVisitor(true);
14383
- fingerprint = identification.visitorId;
14384
- fingerprintRequestId = identification.requestId;
14420
+ let fingerprint = "";
14421
+ if (botDetectionEnabled) {
14422
+ try {
14423
+ fingerprint = fingerprintService.getCachedFingerprint() ?? "";
14424
+ } catch (error) {
14425
+ console.error("Error reading cached fingerprint:", error);
14426
+ }
14385
14427
  } else {
14386
14428
  const cacheOnly = !FP_TRIGGER_EVENTS.includes(eventName);
14387
- fingerprint = await fingerprintService.getFingerprint(cacheOnly);
14429
+ try {
14430
+ fingerprint = await fingerprintService.getFingerprint(cacheOnly);
14431
+ } catch {
14432
+ console.error("Tracking diagnostic", {
14433
+ eventName,
14434
+ stage: "fingerprint",
14435
+ reason: "failed"
14436
+ });
14437
+ }
14388
14438
  }
14389
14439
  const defaultCoordinates = {};
14390
14440
  const coordinates = geolocationService.getCoordinates();
@@ -14409,19 +14459,29 @@ async function trackEvent(eventName, eventProperties, meta = {}) {
14409
14459
  ...meta
14410
14460
  };
14411
14461
  mixpanelService.track(eventName, properties);
14412
- if (fingerprintRequestId) {
14413
- void botDetectionRelayService.relay({
14414
- requestId: fingerprintRequestId,
14415
- visitorId: fingerprint,
14416
- ...distinctId && { distinctId },
14417
- eventName
14418
- });
14462
+ if (botDetectionEnabled) {
14463
+ void identifyAndRelayFingerprint(eventName, distinctId);
14419
14464
  }
14420
14465
  } catch (error) {
14421
14466
  console.error(`Error tracking event '${eventName}':`, error);
14422
14467
  trackEventError(eventName, error, eventProperties);
14423
14468
  }
14424
14469
  }
14470
+ async function identifyAndRelayFingerprint(eventName, distinctId) {
14471
+ try {
14472
+ if (!fingerprintService.isFingerprintReady()) return;
14473
+ const identification = await fingerprintService.identifyVisitor(true);
14474
+ if (!identification.requestId) return;
14475
+ await botDetectionRelayService.relay({
14476
+ requestId: identification.requestId,
14477
+ visitorId: identification.visitorId,
14478
+ ...distinctId && { distinctId },
14479
+ eventName
14480
+ });
14481
+ } catch (error) {
14482
+ console.error(`Error running Bot Detection for '${eventName}':`, error);
14483
+ }
14484
+ }
14425
14485
  function extractTrackingParams() {
14426
14486
  try {
14427
14487
  const urlParams = new URLSearchParams(window.location.search);