@wireai/activation 0.7.0 → 0.8.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.
@@ -212,6 +212,47 @@ var detectAppVersion = (requireModule = runtimeRequire) => {
212
212
  return void 0;
213
213
  };
214
214
 
215
+ // src/device/deviceModel.ts
216
+ var coerceModel = (value) => {
217
+ if (typeof value !== "string") return void 0;
218
+ const trimmed = value.trim();
219
+ return trimmed.length > 0 ? trimmed : void 0;
220
+ };
221
+ var runtimeRequire2 = (moduleName) => {
222
+ try {
223
+ if (typeof __require !== "function") return void 0;
224
+ return __require(moduleName);
225
+ } catch {
226
+ return void 0;
227
+ }
228
+ };
229
+ var interop2 = (mod) => {
230
+ if (!mod || typeof mod !== "object") return void 0;
231
+ const def = mod.default;
232
+ if (def && typeof def === "object") return def;
233
+ return mod;
234
+ };
235
+ var safeInterop2 = (requireModule, moduleName) => {
236
+ try {
237
+ return interop2(requireModule(moduleName));
238
+ } catch {
239
+ return void 0;
240
+ }
241
+ };
242
+ var detectNativeModel = (requireModule = runtimeRequire2) => {
243
+ try {
244
+ const device = safeInterop2(requireModule, "expo-device");
245
+ if (device) {
246
+ const modelName = coerceModel(device.modelName);
247
+ if (modelName) return modelName;
248
+ const modelId = coerceModel(device.modelId);
249
+ if (modelId) return modelId;
250
+ }
251
+ } catch {
252
+ }
253
+ return void 0;
254
+ };
255
+
215
256
  // src/device/deviceContext.ts
216
257
  var deriveFormFactor = (iosIdiom, width, height) => {
217
258
  if (iosIdiom === "pad") return "tablet";
@@ -258,6 +299,8 @@ var collectDeviceContext = () => {
258
299
  ctx.interfaceIdiom = idiom;
259
300
  iosIdiom = idiom;
260
301
  }
302
+ const iosModel = detectNativeModel();
303
+ if (iosModel) ctx.model = iosModel;
261
304
  }
262
305
  } catch {
263
306
  }
@@ -591,20 +634,46 @@ var resolveUserContext = (ctx = {}, opts = {}) => {
591
634
  return result;
592
635
  };
593
636
 
637
+ // src/context/deviceId.ts
638
+ var AUTO_DEVICE_ID_PREFIX = "wdev_";
639
+ var deviceIdStorageKey = (appId) => `wireai:analytics:deviceKey:${appId != null ? appId : "default"}`;
640
+ var randomChunk = () => Math.floor(Math.random() * 4294967296).toString(36).padStart(6, "0");
641
+ var mintDeviceId = () => {
642
+ const time = Date.now().toString(36);
643
+ return `${AUTO_DEVICE_ID_PREFIX}${time}_${randomChunk()}${randomChunk()}`;
644
+ };
645
+
594
646
  // src/analytics/analyticsFacade.ts
595
647
  var createAnalytics = (config, options = {}) => {
596
- var _a, _b, _c, _d;
648
+ var _a, _b, _c, _d, _e;
597
649
  const instanceSessionId = (_a = config.sessionId) != null ? _a : makeSessionId();
598
650
  const resolveSessionId = () => {
599
651
  var _a2, _b2;
600
652
  return (_b2 = (_a2 = config.sessionId) != null ? _a2 : getCurrentSessionId()) != null ? _b2 : instanceSessionId;
601
653
  };
602
- const envelope = () => buildContextEnvelope({
603
- sessionId: resolveSessionId(),
604
- appVersion: config.appVersion,
605
- appBuild: config.appBuild,
606
- networkType: config.networkType
607
- });
654
+ let userContext = { ...(_b = config.userContext) != null ? _b : {} };
655
+ const hostDeviceKeyAtInit = typeof ((_c = config.userContext) == null ? void 0 : _c.deviceKey) === "string" && config.userContext.deviceKey.trim() ? config.userContext.deviceKey.trim() : void 0;
656
+ let autoDeviceKey = mintDeviceId();
657
+ if (config.storage && !hostDeviceKeyAtInit) {
658
+ const storage = config.storage;
659
+ const deviceKey = deviceIdStorageKey(config.appId);
660
+ void storage.getItem(deviceKey).then((saved) => {
661
+ const persisted = typeof saved === "string" && saved.trim() ? saved.trim() : void 0;
662
+ if (persisted) autoDeviceKey = persisted;
663
+ else void storage.setItem(deviceKey, autoDeviceKey).catch(() => {
664
+ });
665
+ }).catch(() => {
666
+ });
667
+ }
668
+ const envelope = () => {
669
+ var _a2;
670
+ return buildContextEnvelope({
671
+ sessionId: resolveSessionId(),
672
+ appVersion: (_a2 = userContext.appVersion) != null ? _a2 : config.appVersion,
673
+ appBuild: config.appBuild,
674
+ networkType: config.networkType
675
+ });
676
+ };
608
677
  const queue = createEventQueue({
609
678
  target: { serverUrl: config.serverUrl, apiKey: config.apiKey },
610
679
  storage: config.storage,
@@ -612,9 +681,8 @@ var createAnalytics = (config, options = {}) => {
612
681
  envelope,
613
682
  ...options
614
683
  });
615
- let userContext = { ...(_b = config.userContext) != null ? _b : {} };
616
- let boundUserId = sanitizeUserId((_c = config.userContext) == null ? void 0 : _c.userId);
617
- const storageKey = `wireai:analytics:userId:${(_d = config.appId) != null ? _d : "default"}`;
684
+ let boundUserId = sanitizeUserId((_d = config.userContext) == null ? void 0 : _d.userId);
685
+ const storageKey = `wireai:analytics:userId:${(_e = config.appId) != null ? _e : "default"}`;
618
686
  if (config.storage) {
619
687
  void config.storage.getItem(storageKey).then((saved) => {
620
688
  if (saved && !boundUserId) boundUserId = saved;
@@ -623,7 +691,11 @@ var createAnalytics = (config, options = {}) => {
623
691
  }
624
692
  const applyContext = (event) => {
625
693
  var _a2;
626
- const resolved = resolveUserContext(userContext, { autoAppVersion: config.appVersion });
694
+ const hostDeviceKey = typeof userContext.deviceKey === "string" && userContext.deviceKey.trim() ? userContext.deviceKey : void 0;
695
+ const resolved = resolveUserContext(
696
+ { ...userContext, deviceKey: hostDeviceKey != null ? hostDeviceKey : autoDeviceKey },
697
+ { autoAppVersion: config.appVersion }
698
+ );
627
699
  if (resolved.userContext) {
628
700
  event.user_context = { ...resolved.userContext, ...(_a2 = event.user_context) != null ? _a2 : {} };
629
701
  }