@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/README.md CHANGED
@@ -62,6 +62,7 @@ import { MicroSurvey } from "@microsurveysai/react-native";
62
62
  | `refreshOnForeground` | `true` | On app foreground, retry queued ingest and refresh a stale config. |
63
63
  | `configTTLms` | 12h | How long the cached config counts as fresh. |
64
64
  | `storage` | AsyncStorage | A synchronous store (e.g. MMKV) to persist into instead. |
65
+ | `appVersion` | — | Your app's version (e.g. `"3.2.0"`), sent with responses so results can be compared across releases. With Expo, pass `Application.nativeApplicationVersion` from `expo-application`. |
65
66
 
66
67
  `<MicroSurveysProvider>` also takes `autoStart` (default `true`) and `fontFamily` (a font your app
67
68
  registered, e.g. with `expo-font`; surveys use the system font otherwise).
package/dist/index.cjs CHANGED
@@ -117,16 +117,29 @@ function numOr(v, fallback) {
117
117
  function strOrUndef(v) {
118
118
  return typeof v === "string" ? v : void 0;
119
119
  }
120
+ var MAX_SCALE_POINTS = 101;
121
+ var MAX_SCALE_ABS = 1e3;
122
+ var MAX_STARS = 100;
123
+ var MAX_OPTIONS = 100;
124
+ function boundedScale(min, max) {
125
+ const lo = Math.min(Math.max(min, -MAX_SCALE_ABS), MAX_SCALE_ABS);
126
+ let hi = Math.min(Math.max(max, -MAX_SCALE_ABS), MAX_SCALE_ABS);
127
+ if (hi - lo > MAX_SCALE_POINTS - 1) hi = lo + MAX_SCALE_POINTS - 1;
128
+ return { min: lo, max: hi };
129
+ }
130
+ function boundedOptions(raw) {
131
+ return Array.isArray(raw) ? raw.slice(0, MAX_OPTIONS) : [];
132
+ }
120
133
  function normalizeConfig(type, raw) {
121
134
  switch (type) {
122
135
  case "NPS":
123
- return { kind: "scale", min: numOr(raw.min, 0), max: numOr(raw.max, 10), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
136
+ return { kind: "scale", ...boundedScale(numOr(raw.min, 0), numOr(raw.max, 10)), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
124
137
  case "CES":
125
- return { kind: "scale", min: numOr(raw.min, 1), max: numOr(raw.max, 7), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
138
+ return { kind: "scale", ...boundedScale(numOr(raw.min, 1), numOr(raw.max, 7)), minLabel: strOrUndef(raw.minLabel), maxLabel: strOrUndef(raw.maxLabel) };
126
139
  case "CSAT_STAR":
127
- return { kind: "stars", count: numOr(raw.count, 5) };
140
+ return { kind: "stars", count: Math.min(numOr(raw.count, 5), MAX_STARS) };
128
141
  case "CSAT_EMOJI": {
129
- const opts = Array.isArray(raw.options) ? raw.options : [];
142
+ const opts = boundedOptions(raw.options);
130
143
  const options = opts.map((o) => {
131
144
  const oo = asObject(o);
132
145
  return { value: String(oo.value ?? ""), emoji: typeof oo.emoji === "string" ? oo.emoji : "", label: strOrUndef(oo.label) };
@@ -136,7 +149,7 @@ function normalizeConfig(type, raw) {
136
149
  case "THUMBS":
137
150
  return { kind: "thumbs" };
138
151
  case "SINGLE_CHOICE": {
139
- const opts = Array.isArray(raw.options) ? raw.options : [];
152
+ const opts = boundedOptions(raw.options);
140
153
  const options = opts.map((o) => {
141
154
  const oo = asObject(o);
142
155
  const value = String(oo.value ?? "");
@@ -246,16 +259,21 @@ var ApiClient = class {
246
259
  return { kind: "config", rawText, config: parseConfig(rawText), etag: res.headers.get("ETag") };
247
260
  }
248
261
  // ── Impressions / responses ─────────────────────────────────────────────────
262
+ /** Queues the impression and returns its `clientId`, so the response from the
263
+ * same showing can point back to it. */
249
264
  recordImpression(input) {
265
+ const clientId = uuid();
250
266
  const payload = {
251
- clientId: uuid(),
267
+ clientId,
252
268
  surveyId: input.surveyId,
253
269
  triggerId: input.triggerId ?? void 0,
254
270
  endUserId: input.endUserId,
255
271
  shownAt: input.shownAt.toISOString(),
256
- dismissed: input.dismissed
272
+ dismissed: input.dismissed,
273
+ context: input.context
257
274
  };
258
275
  this.enqueue("/api/sdk/impressions", { impressions: [payload] });
276
+ return clientId;
259
277
  }
260
278
  recordResponse(input) {
261
279
  const payload = {
@@ -265,6 +283,9 @@ var ApiClient = class {
265
283
  completed: input.completed,
266
284
  submittedAt: input.submittedAt.toISOString(),
267
285
  userProps: input.userProps,
286
+ impressionId: input.impressionId,
287
+ triggerId: input.triggerId ?? void 0,
288
+ context: input.context,
268
289
  answers: input.answers
269
290
  };
270
291
  this.enqueue("/api/sdk/responses", { responses: [payload] });
@@ -699,6 +720,9 @@ var MicroSurveysClient = class {
699
720
  __publicField(this, "engine");
700
721
  __publicField(this, "configTTLms");
701
722
  __publicField(this, "refreshOnForeground");
723
+ __publicField(this, "platform");
724
+ __publicField(this, "osVersion");
725
+ __publicField(this, "appVersion");
702
726
  __publicField(this, "subscribeForeground");
703
727
  __publicField(this, "presentListeners", /* @__PURE__ */ new Set());
704
728
  __publicField(this, "configListeners", /* @__PURE__ */ new Set());
@@ -712,6 +736,9 @@ var MicroSurveysClient = class {
712
736
  this.api = new ApiClient(opts.apiKey, opts.apiBaseURL ?? DEFAULT_BASE_URL, kv);
713
737
  this.configTTLms = opts.configTTLms ?? DEFAULT_CONFIG_TTL_MS;
714
738
  this.refreshOnForeground = opts.refreshOnForeground ?? true;
739
+ this.platform = opts.platform;
740
+ this.osVersion = opts.osVersion;
741
+ this.appVersion = opts.appVersion;
715
742
  this.subscribeForeground = opts.subscribeForeground;
716
743
  this.engine = new TriggerEngine({
717
744
  store: this.triggerStateStore,
@@ -819,7 +846,15 @@ var MicroSurveysClient = class {
819
846
  }
820
847
  recordOutcome(surveyId, triggerId, identity, shownAt, result) {
821
848
  const endUserId = endUserIdOf(identity);
822
- this.api.recordImpression({ surveyId, triggerId, endUserId, shownAt, dismissed: result.dismissed });
849
+ const context = this.deviceContext();
850
+ const impressionId = this.api.recordImpression({
851
+ surveyId,
852
+ triggerId,
853
+ endUserId,
854
+ shownAt,
855
+ dismissed: result.dismissed,
856
+ context
857
+ });
823
858
  if (result.completed || result.answers.length > 0) {
824
859
  this.api.recordResponse({
825
860
  surveyId,
@@ -827,10 +862,23 @@ var MicroSurveysClient = class {
827
862
  completed: result.completed,
828
863
  submittedAt: /* @__PURE__ */ new Date(),
829
864
  userProps: identity.userProperties,
865
+ impressionId,
866
+ triggerId,
867
+ context,
830
868
  answers: result.answers
831
869
  });
832
870
  }
833
871
  }
872
+ /** Where this showing happened. The language is read at record time: the user
873
+ * can change it while the app runs. */
874
+ deviceContext() {
875
+ let locale;
876
+ try {
877
+ locale = Intl.DateTimeFormat().resolvedOptions().locale || void 0;
878
+ } catch {
879
+ }
880
+ return { platform: this.platform, appVersion: this.appVersion, osVersion: this.osVersion, locale };
881
+ }
834
882
  };
835
883
  function createClient(opts) {
836
884
  return new MicroSurveysClient(opts);
@@ -931,6 +979,8 @@ var MicroSurveys = class {
931
979
  refreshOnForeground: opts.refreshOnForeground ?? true,
932
980
  // Survey platform targeting: "ios" | "android".
933
981
  platform: import_react_native.Platform.OS,
982
+ osVersion: String(import_react_native.Platform.Version),
983
+ appVersion: opts.appVersion,
934
984
  storage: kv,
935
985
  subscribeForeground: appStateForeground
936
986
  });
@@ -1823,8 +1873,8 @@ var styles4 = import_react_native6.StyleSheet.create({
1823
1873
  // src/provider.tsx
1824
1874
  var import_jsx_runtime5 = require("react/jsx-runtime");
1825
1875
  var MicroSurveysContext = (0, import_react4.createContext)({ instance: null });
1826
- function MicroSurveysProvider({ apiKey, apiBaseURL, instance, autoStart = true, fontFamily, children }) {
1827
- const [owned] = (0, import_react4.useState)(() => !instance && apiKey ? new MicroSurveys({ apiKey, apiBaseURL }) : null);
1876
+ function MicroSurveysProvider({ apiKey, apiBaseURL, appVersion, instance, autoStart = true, fontFamily, children }) {
1877
+ const [owned] = (0, import_react4.useState)(() => !instance && apiKey ? new MicroSurveys({ apiKey, apiBaseURL, appVersion }) : null);
1828
1878
  const ms = instance ?? owned;
1829
1879
  (0, import_react4.useEffect)(() => {
1830
1880
  if (!ms || !autoStart) return;