@tolinku/react-native-sdk 0.4.0 → 0.5.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.
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import { useState, useRef, useMemo, useEffect, useCallback } from 'react';
4
4
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
5
5
 
6
6
  // src/types.ts
7
- var SDK_VERSION = "0.4.0";
7
+ var SDK_VERSION = "0.5.0";
8
8
 
9
9
  // src/debug.ts
10
10
  var _debugEnabled = false;
@@ -247,12 +247,33 @@ function validateEventType(eventType) {
247
247
  var BATCH_SIZE = 10;
248
248
  var FLUSH_INTERVAL_MS = 5e3;
249
249
  var MAX_QUEUE_SIZE = 1e3;
250
- var Analytics = class {
250
+ var _Analytics = class _Analytics {
251
251
  constructor(client) {
252
252
  this.queue = [];
253
253
  this.flushTimer = null;
254
254
  this.appStateSubscription = null;
255
255
  this.isFlushing = false;
256
+ /**
257
+ * Track a custom event. The event is added to the internal queue
258
+ * and will be flushed automatically or when flush() is called.
259
+ *
260
+ * Event type must match /^custom\.[a-z0-9_]+$/.
261
+ * Auto-prefixes with "custom." if missing.
262
+ */
263
+ /**
264
+ * Set once the server says this Appspace does not attribute app opens, so the
265
+ * setting costs one request a launch rather than one per link.
266
+ */
267
+ this.appOpensDisabled = false;
268
+ /**
269
+ * The last link reported, and when. Cold start and the link stream can both
270
+ * deliver the same tap, depending on the plugin, so an app instrumenting both
271
+ * paths would otherwise report it twice and be billed twice. A genuine second
272
+ * tap of the same link inside this window is implausible; a duplicate delivery
273
+ * of one tap is not.
274
+ */
275
+ this.lastOpenUrl = null;
276
+ this.lastOpenAt = 0;
256
277
  this.handleAppStateChange = (state) => {
257
278
  if (state === "background" || state === "inactive") {
258
279
  this.flush().catch(() => {
@@ -266,12 +287,51 @@ var Analytics = class {
266
287
  );
267
288
  }
268
289
  /**
269
- * Track a custom event. The event is added to the internal queue
270
- * and will be flushed automatically or when flush() is called.
290
+ * Report that a link opened the app without the browser being involved.
271
291
  *
272
- * Event type must match /^custom\.[a-z0-9_]+$/.
273
- * Auto-prefixes with "custom." if missing.
292
+ * A Universal Link or App Link hands the app the URL directly, so Tolinku is
293
+ * never contacted and the tap goes unrecorded. Those taps come from people who
294
+ * already have the app, so leaving them out makes a re-engagement campaign
295
+ * look like a failure exactly when it worked.
296
+ *
297
+ * Only http and https links are reported. A custom scheme means Tolinku's own
298
+ * hand-off page opened the app, and that tap was counted when the page was
299
+ * served, so passing one does nothing rather than counting it twice.
300
+ *
301
+ * Never throws. This runs on the path that routes the user somewhere, and a
302
+ * tap that goes unrecorded is not worth interrupting that.
274
303
  */
304
+ async trackLinkOpen(url, userId) {
305
+ if (this.appOpensDisabled) return;
306
+ const trimmed = typeof url === "string" ? url.trim() : "";
307
+ if (!trimmed) return;
308
+ let parsed;
309
+ try {
310
+ parsed = new URL(trimmed);
311
+ } catch {
312
+ return;
313
+ }
314
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return;
315
+ const now = Date.now();
316
+ if (this.lastOpenUrl === trimmed && now - this.lastOpenAt < _Analytics.OPEN_DEDUPE_MS) return;
317
+ this.lastOpenUrl = trimmed;
318
+ this.lastOpenAt = now;
319
+ try {
320
+ const result = await this.client.postPublic(
321
+ "/v1/api/opens",
322
+ {
323
+ url: trimmed,
324
+ // The User-Agent cannot say which platform this is: this SDK
325
+ // identifies itself the same way on both. Without it an app open has
326
+ // no platform and lands in a blank bucket on every breakdown.
327
+ platform: Platform.OS === "ios" || Platform.OS === "android" ? Platform.OS : void 0,
328
+ ...userId ? { user_id: userId } : {}
329
+ }
330
+ );
331
+ if (result?.attribute === false) this.appOpensDisabled = true;
332
+ } catch {
333
+ }
334
+ }
275
335
  async track(eventType, properties) {
276
336
  const normalizedType = validateEventType(eventType);
277
337
  const event = {
@@ -347,6 +407,8 @@ var Analytics = class {
347
407
  }
348
408
  }
349
409
  };
410
+ _Analytics.OPEN_DEDUPE_MS = 5e3;
411
+ var Analytics = _Analytics;
350
412
  var BATCH_SIZE2 = 10;
351
413
  var FLUSH_INTERVAL_MS2 = 5e3;
352
414
  var MAX_QUEUE_SIZE2 = 500;
@@ -741,17 +803,17 @@ var Deferred = class {
741
803
  async attemptSignals(options) {
742
804
  try {
743
805
  const { width, height } = Dimensions.get("screen");
744
- const resolvedTimezone = options.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone;
745
- const resolvedLanguage = options.language || (typeof Intl !== "undefined" && typeof Intl.DateTimeFormat === "function" ? Intl.DateTimeFormat().resolvedOptions().locale : void 0) || "en";
806
+ const resolvedTimezone = override(options.timezone) || Intl.DateTimeFormat().resolvedOptions().timeZone;
807
+ const resolvedLanguage = override(options.language) || (typeof Intl !== "undefined" && typeof Intl.DateTimeFormat === "function" ? Intl.DateTimeFormat().resolvedOptions().locale : void 0) || "en";
746
808
  const link = await this.client.postPublic("/v1/api/deferred/claim-by-signals", {
747
809
  appspace_id: options.appspaceId,
748
810
  timezone: resolvedTimezone,
749
811
  language: resolvedLanguage,
750
- screen_width: options.screenWidth || width,
751
- screen_height: options.screenHeight || height,
812
+ screen_width: override(options.screenWidth) || width,
813
+ screen_height: override(options.screenHeight) || height,
752
814
  // Separates devices reporting identical dp dimensions.
753
- device_pixel_ratio: options.devicePixelRatio || PixelRatio.get(),
754
- os_version: options.osVersion || String(Platform.Version)
815
+ device_pixel_ratio: override(options.devicePixelRatio) || PixelRatio.get(),
816
+ os_version: override(options.osVersion) || String(Platform.Version)
755
817
  });
756
818
  return { link, settled: true };
757
819
  } catch (err) {
@@ -771,6 +833,16 @@ var Deferred = class {
771
833
  }
772
834
  }
773
835
  };
836
+ function override(value) {
837
+ if (typeof value === "string") {
838
+ const trimmed = value.trim();
839
+ return trimmed === "" ? void 0 : trimmed;
840
+ }
841
+ if (typeof value === "number") {
842
+ return Number.isFinite(value) && value > 0 ? value : void 0;
843
+ }
844
+ return void 0;
845
+ }
774
846
  var BASE_KEY = "tolinku_message_dismissed";
775
847
  var IMPRESSIONS_BASE_KEY = "tolinku_message_impressions";
776
848
  var LAST_SHOWN_BASE_KEY = "tolinku_message_last_shown";
@@ -931,6 +1003,41 @@ var _Tolinku = class _Tolinku {
931
1003
  const mergedProps = _Tolinku._userId ? { user_id: _Tolinku._userId, ...properties } : properties;
932
1004
  return _Tolinku.analyticsInstance.track(eventType, mergedProps);
933
1005
  }
1006
+ /**
1007
+ * Report that a link opened the app, when it opened without the browser.
1008
+ *
1009
+ * A Universal Link or App Link hands the app the URL directly, so Tolinku is
1010
+ * never contacted and the tap goes unrecorded. Those taps come from people who
1011
+ * already have your app, so leaving them out makes a re-engagement campaign
1012
+ * look like a failure exactly when it worked.
1013
+ *
1014
+ * Both ways a link arrives need it. The listener fires only while the app is
1015
+ * already running, so a link that launched the app cold arrives separately:
1016
+ *
1017
+ * ```ts
1018
+ * // Launched by a link, app was not running.
1019
+ * const initial = await Linking.getInitialURL();
1020
+ * if (initial) Tolinku.trackLinkOpen(initial);
1021
+ *
1022
+ * // Tapped while the app was already open.
1023
+ * Linking.addEventListener('url', ({ url }) => {
1024
+ * Tolinku.trackLinkOpen(url);
1025
+ * // your own routing
1026
+ * });
1027
+ * ```
1028
+ *
1029
+ * Wiring both is safe: the same link arriving twice in quick succession is
1030
+ * reported once.
1031
+ *
1032
+ * Only http and https links are reported. A custom scheme means Tolinku's own
1033
+ * hand-off page opened the app, and that tap is already counted.
1034
+ *
1035
+ * Never throws.
1036
+ */
1037
+ static async trackLinkOpen(url) {
1038
+ if (!_Tolinku.analyticsInstance) return;
1039
+ return _Tolinku.analyticsInstance.trackLinkOpen(url, _Tolinku._userId ?? void 0);
1040
+ }
934
1041
  /**
935
1042
  * Immediately flush all queued analytics and ecommerce events to the server.
936
1043
  */
@@ -1065,13 +1172,14 @@ function PuckComponentRenderer({ component, messageId, options }) {
1065
1172
  case "Button": {
1066
1173
  const handlePress = () => {
1067
1174
  const action = props.action || "";
1175
+ if (!action) return;
1176
+ if (!isSafeUrl(action)) {
1177
+ debugWarn(`Button action URL blocked (unsafe protocol): ${action}`);
1178
+ return;
1179
+ }
1068
1180
  if (options.onButtonPress) {
1069
1181
  options.onButtonPress(action, messageId);
1070
- } else if (action) {
1071
- if (!isSafeUrl(action)) {
1072
- debugWarn(`Button action URL blocked (unsafe protocol): ${action}`);
1073
- return;
1074
- }
1182
+ } else {
1075
1183
  Linking.openURL(action).catch(() => {
1076
1184
  });
1077
1185
  }