@microsurveysai/react-native 0.1.0 → 0.1.2

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.d.cts CHANGED
@@ -176,6 +176,11 @@ interface CreateClientOptions {
176
176
  refreshOnForeground?: boolean;
177
177
  /** This device's platform for survey targeting ("ios" | "android"). */
178
178
  platform: string;
179
+ /** The OS version, sent with responses. */
180
+ osVersion?: string;
181
+ /** The host app's version, sent with responses so results can be compared
182
+ * across releases. */
183
+ appVersion?: string;
179
184
  /** Where config, identity, trigger state and the outbox persist. Must already
180
185
  * hold its data when the client is created. */
181
186
  storage: KeyValueStorage;
@@ -199,6 +204,9 @@ declare class MicroSurveysClient {
199
204
  private readonly engine;
200
205
  private readonly configTTLms;
201
206
  private readonly refreshOnForeground;
207
+ private readonly platform;
208
+ private readonly osVersion;
209
+ private readonly appVersion;
202
210
  private readonly subscribeForeground;
203
211
  private readonly presentListeners;
204
212
  private readonly configListeners;
@@ -233,6 +241,9 @@ declare class MicroSurveysClient {
233
241
  /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
234
242
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
235
243
  private recordOutcome;
244
+ /** Where this showing happened. The language is read at record time: the user
245
+ * can change it while the app runs. */
246
+ private deviceContext;
236
247
  }
237
248
 
238
249
  /** The fields of an Amplitude event this plugin reads. */
@@ -284,6 +295,9 @@ interface MicroSurveysOptions {
284
295
  storage?: KeyValueStorage;
285
296
  /** An async store to persist into. Defaults to `@react-native-async-storage/async-storage`. */
286
297
  asyncStorage?: AsyncKeyValueStorage;
298
+ /** Your app's version (e.g. "3.2.0"), sent with responses so results can be
299
+ * compared across releases. With Expo: `Application.nativeApplicationVersion`. */
300
+ appVersion?: string;
287
301
  }
288
302
  declare class MicroSurveys {
289
303
  readonly apiKey: string;
@@ -330,6 +344,8 @@ interface MicroSurveysProviderProps {
330
344
  apiKey?: string;
331
345
  /** Override the API base for self-hosted deployments. Ignored when `instance` is passed. */
332
346
  apiBaseURL?: string;
347
+ /** Your app's version (e.g. "3.2.0"), sent with responses. Ignored when `instance` is passed. */
348
+ appVersion?: string;
333
349
  /** A `MicroSurveys` you created yourself, e.g. to register its Amplitude plugin. */
334
350
  instance?: MicroSurveys;
335
351
  /** Start automatically on mount (load config, flush queued ingest). Default true. */
@@ -339,7 +355,7 @@ interface MicroSurveysProviderProps {
339
355
  fontFamily?: string;
340
356
  children?: ReactNode;
341
357
  }
342
- declare function MicroSurveysProvider({ apiKey, apiBaseURL, instance, autoStart, fontFamily, children }: MicroSurveysProviderProps): react.JSX.Element;
358
+ declare function MicroSurveysProvider({ apiKey, apiBaseURL, appVersion, instance, autoStart, fontFamily, children }: MicroSurveysProviderProps): react.JSX.Element;
343
359
  /** The `MicroSurveys` instance from the nearest provider (or null outside one). */
344
360
  declare function useMicroSurveysInstance(): MicroSurveys | null;
345
361
  /** Event + identity helpers bound to the provider's instance. Safe no-ops
package/dist/index.d.ts CHANGED
@@ -176,6 +176,11 @@ interface CreateClientOptions {
176
176
  refreshOnForeground?: boolean;
177
177
  /** This device's platform for survey targeting ("ios" | "android"). */
178
178
  platform: string;
179
+ /** The OS version, sent with responses. */
180
+ osVersion?: string;
181
+ /** The host app's version, sent with responses so results can be compared
182
+ * across releases. */
183
+ appVersion?: string;
179
184
  /** Where config, identity, trigger state and the outbox persist. Must already
180
185
  * hold its data when the client is created. */
181
186
  storage: KeyValueStorage;
@@ -199,6 +204,9 @@ declare class MicroSurveysClient {
199
204
  private readonly engine;
200
205
  private readonly configTTLms;
201
206
  private readonly refreshOnForeground;
207
+ private readonly platform;
208
+ private readonly osVersion;
209
+ private readonly appVersion;
202
210
  private readonly subscribeForeground;
203
211
  private readonly presentListeners;
204
212
  private readonly configListeners;
@@ -233,6 +241,9 @@ declare class MicroSurveysClient {
233
241
  /** Records the outcome of an **inline** `<MicroSurvey>` (no trigger/gate). */
234
242
  recordInline(survey: Survey, result: SurveyResult, shownAt: Date): void;
235
243
  private recordOutcome;
244
+ /** Where this showing happened. The language is read at record time: the user
245
+ * can change it while the app runs. */
246
+ private deviceContext;
236
247
  }
237
248
 
238
249
  /** The fields of an Amplitude event this plugin reads. */
@@ -284,6 +295,9 @@ interface MicroSurveysOptions {
284
295
  storage?: KeyValueStorage;
285
296
  /** An async store to persist into. Defaults to `@react-native-async-storage/async-storage`. */
286
297
  asyncStorage?: AsyncKeyValueStorage;
298
+ /** Your app's version (e.g. "3.2.0"), sent with responses so results can be
299
+ * compared across releases. With Expo: `Application.nativeApplicationVersion`. */
300
+ appVersion?: string;
287
301
  }
288
302
  declare class MicroSurveys {
289
303
  readonly apiKey: string;
@@ -330,6 +344,8 @@ interface MicroSurveysProviderProps {
330
344
  apiKey?: string;
331
345
  /** Override the API base for self-hosted deployments. Ignored when `instance` is passed. */
332
346
  apiBaseURL?: string;
347
+ /** Your app's version (e.g. "3.2.0"), sent with responses. Ignored when `instance` is passed. */
348
+ appVersion?: string;
333
349
  /** A `MicroSurveys` you created yourself, e.g. to register its Amplitude plugin. */
334
350
  instance?: MicroSurveys;
335
351
  /** Start automatically on mount (load config, flush queued ingest). Default true. */
@@ -339,7 +355,7 @@ interface MicroSurveysProviderProps {
339
355
  fontFamily?: string;
340
356
  children?: ReactNode;
341
357
  }
342
- declare function MicroSurveysProvider({ apiKey, apiBaseURL, instance, autoStart, fontFamily, children }: MicroSurveysProviderProps): react.JSX.Element;
358
+ declare function MicroSurveysProvider({ apiKey, apiBaseURL, appVersion, instance, autoStart, fontFamily, children }: MicroSurveysProviderProps): react.JSX.Element;
343
359
  /** The `MicroSurveys` instance from the nearest provider (or null outside one). */
344
360
  declare function useMicroSurveysInstance(): MicroSurveys | null;
345
361
  /** Event + identity helpers bound to the provider's instance. Safe no-ops
package/dist/index.js CHANGED
@@ -76,16 +76,29 @@ function numOr(v, fallback) {
76
76
  function strOrUndef(v) {
77
77
  return typeof v === "string" ? v : void 0;
78
78
  }
79
+ var MAX_SCALE_POINTS = 101;
80
+ var MAX_SCALE_ABS = 1e3;
81
+ var MAX_STARS = 100;
82
+ var MAX_OPTIONS = 100;
83
+ function boundedScale(min, max) {
84
+ const lo = Math.min(Math.max(min, -MAX_SCALE_ABS), MAX_SCALE_ABS);
85
+ let hi = Math.min(Math.max(max, -MAX_SCALE_ABS), MAX_SCALE_ABS);
86
+ if (hi - lo > MAX_SCALE_POINTS - 1) hi = lo + MAX_SCALE_POINTS - 1;
87
+ return { min: lo, max: hi };
88
+ }
89
+ function boundedOptions(raw) {
90
+ return Array.isArray(raw) ? raw.slice(0, MAX_OPTIONS) : [];
91
+ }
79
92
  function normalizeConfig(type, raw) {
80
93
  switch (type) {
81
94
  case "NPS":
82
- return { kind: "scale", min: numOr(raw.min, 0), max: numOr(raw.max, 10), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
95
+ return { kind: "scale", ...boundedScale(numOr(raw.min, 0), numOr(raw.max, 10)), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
83
96
  case "CES":
84
- return { kind: "scale", min: numOr(raw.min, 1), max: numOr(raw.max, 7), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
97
+ return { kind: "scale", ...boundedScale(numOr(raw.min, 1), numOr(raw.max, 7)), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
85
98
  case "CSAT_STAR":
86
- return { kind: "stars", count: numOr(raw.count, 5) };
99
+ return { kind: "stars", count: Math.min(numOr(raw.count, 5), MAX_STARS) };
87
100
  case "CSAT_EMOJI": {
88
- const opts = Array.isArray(raw.options) ? raw.options : [];
101
+ const opts = boundedOptions(raw.options);
89
102
  const options = opts.map((o) => {
90
103
  const oo = asObject(o);
91
104
  return { value: String(oo.value ?? ""), emoji: typeof oo.emoji === "string" ? oo.emoji : "", label: strOrUndef(oo.label) };
@@ -95,7 +108,7 @@ function normalizeConfig(type, raw) {
95
108
  case "THUMBS":
96
109
  return { kind: "thumbs" };
97
110
  case "SINGLE_CHOICE": {
98
- const opts = Array.isArray(raw.options) ? raw.options : [];
111
+ const opts = boundedOptions(raw.options);
99
112
  const options = opts.map((o) => {
100
113
  const oo = asObject(o);
101
114
  const value = String(oo.value ?? "");
@@ -205,16 +218,21 @@ var ApiClient = class {
205
218
  return { kind: "config", rawText, config: parseConfig(rawText), etag: res.headers.get("ETag") };
206
219
  }
207
220
  // ── Impressions / responses ─────────────────────────────────────────────────
221
+ /** Queues the impression and returns its `clientId`, so the response from the
222
+ * same showing can point back to it. */
208
223
  recordImpression(input) {
224
+ const clientId = uuid();
209
225
  const payload = {
210
- clientId: uuid(),
226
+ clientId,
211
227
  surveyId: input.surveyId,
212
228
  triggerId: input.triggerId ?? void 0,
213
229
  endUserId: input.endUserId,
214
230
  shownAt: input.shownAt.toISOString(),
215
- dismissed: input.dismissed
231
+ dismissed: input.dismissed,
232
+ context: input.context
216
233
  };
217
234
  this.enqueue("/api/sdk/impressions", { impressions: [payload] });
235
+ return clientId;
218
236
  }
219
237
  recordResponse(input) {
220
238
  const payload = {
@@ -224,6 +242,9 @@ var ApiClient = class {
224
242
  completed: input.completed,
225
243
  submittedAt: input.submittedAt.toISOString(),
226
244
  userProps: input.userProps,
245
+ impressionId: input.impressionId,
246
+ triggerId: input.triggerId ?? void 0,
247
+ context: input.context,
227
248
  answers: input.answers
228
249
  };
229
250
  this.enqueue("/api/sdk/responses", { responses: [payload] });
@@ -658,6 +679,9 @@ var MicroSurveysClient = class {
658
679
  __publicField(this, "engine");
659
680
  __publicField(this, "configTTLms");
660
681
  __publicField(this, "refreshOnForeground");
682
+ __publicField(this, "platform");
683
+ __publicField(this, "osVersion");
684
+ __publicField(this, "appVersion");
661
685
  __publicField(this, "subscribeForeground");
662
686
  __publicField(this, "presentListeners", /* @__PURE__ */ new Set());
663
687
  __publicField(this, "configListeners", /* @__PURE__ */ new Set());
@@ -671,6 +695,9 @@ var MicroSurveysClient = class {
671
695
  this.api = new ApiClient(opts.apiKey, opts.apiBaseURL ?? DEFAULT_BASE_URL, kv);
672
696
  this.configTTLms = opts.configTTLms ?? DEFAULT_CONFIG_TTL_MS;
673
697
  this.refreshOnForeground = opts.refreshOnForeground ?? true;
698
+ this.platform = opts.platform;
699
+ this.osVersion = opts.osVersion;
700
+ this.appVersion = opts.appVersion;
674
701
  this.subscribeForeground = opts.subscribeForeground;
675
702
  this.engine = new TriggerEngine({
676
703
  store: this.triggerStateStore,
@@ -778,7 +805,15 @@ var MicroSurveysClient = class {
778
805
  }
779
806
  recordOutcome(surveyId, triggerId, identity, shownAt, result) {
780
807
  const endUserId = endUserIdOf(identity);
781
- this.api.recordImpression({ surveyId, triggerId, endUserId, shownAt, dismissed: result.dismissed });
808
+ const context = this.deviceContext();
809
+ const impressionId = this.api.recordImpression({
810
+ surveyId,
811
+ triggerId,
812
+ endUserId,
813
+ shownAt,
814
+ dismissed: result.dismissed,
815
+ context
816
+ });
782
817
  if (result.completed || result.answers.length > 0) {
783
818
  this.api.recordResponse({
784
819
  surveyId,
@@ -786,10 +821,23 @@ var MicroSurveysClient = class {
786
821
  completed: result.completed,
787
822
  submittedAt: /* @__PURE__ */ new Date(),
788
823
  userProps: identity.userProperties,
824
+ impressionId,
825
+ triggerId,
826
+ context,
789
827
  answers: result.answers
790
828
  });
791
829
  }
792
830
  }
831
+ /** Where this showing happened. The language is read at record time: the user
832
+ * can change it while the app runs. */
833
+ deviceContext() {
834
+ let locale;
835
+ try {
836
+ locale = Intl.DateTimeFormat().resolvedOptions().locale || void 0;
837
+ } catch {
838
+ }
839
+ return { platform: this.platform, appVersion: this.appVersion, osVersion: this.osVersion, locale };
840
+ }
793
841
  };
794
842
  function createClient(opts) {
795
843
  return new MicroSurveysClient(opts);
@@ -890,6 +938,8 @@ var MicroSurveys = class {
890
938
  refreshOnForeground: opts.refreshOnForeground ?? true,
891
939
  // Survey platform targeting: "ios" | "android".
892
940
  platform: Platform.OS,
941
+ osVersion: String(Platform.Version),
942
+ appVersion: opts.appVersion,
893
943
  storage: kv,
894
944
  subscribeForeground: appStateForeground
895
945
  });
@@ -1795,8 +1845,8 @@ var styles4 = StyleSheet4.create({
1795
1845
  // src/provider.tsx
1796
1846
  import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
1797
1847
  var MicroSurveysContext = createContext({ instance: null });
1798
- function MicroSurveysProvider({ apiKey, apiBaseURL, instance, autoStart = true, fontFamily, children }) {
1799
- const [owned] = useState4(() => !instance && apiKey ? new MicroSurveys({ apiKey, apiBaseURL }) : null);
1848
+ function MicroSurveysProvider({ apiKey, apiBaseURL, appVersion, instance, autoStart = true, fontFamily, children }) {
1849
+ const [owned] = useState4(() => !instance && apiKey ? new MicroSurveys({ apiKey, apiBaseURL, appVersion }) : null);
1800
1850
  const ms = instance ?? owned;
1801
1851
  useEffect3(() => {
1802
1852
  if (!ms || !autoStart) return;