@findlane/browser 0.25.0 → 0.26.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.d.ts CHANGED
@@ -153,6 +153,12 @@ export type SearchResponse<T> = {
153
153
  };
154
154
  /** With `subindexes`: each subindex's results by name, or its error (0.22.0). */
155
155
  subindexes?: Record<string, SearchResponse<Record<string, unknown>> | SubindexError>;
156
+ /** While the index runs an A/B test of its settings (0.26.0): the test and the shopper's group, which this
157
+ * client's events carry so the dashboard can compare the groups. Nothing to do with it. */
158
+ abTest?: {
159
+ id: string;
160
+ variant: "a" | "b";
161
+ };
156
162
  };
157
163
  /** One search of a multi-index search (multiSearch): its index's public ID, the client's own when left out. */
158
164
  export type MultiSearchQuery = SearchRequest & {
@@ -405,6 +411,7 @@ export declare class SearchBrowserClient {
405
411
  /** The shopper's recent searches, kept in this browser only. */
406
412
  readonly recentSearches: RecentSearches;
407
413
  private popular;
414
+ private test;
408
415
  constructor(options: BrowserClientOptions);
409
416
  /** The signed key searches send: the one given, or the function's, asked for again 30 seconds before it
410
417
  * expires, or at once when `fresh`. */
package/dist/index.js CHANGED
@@ -162,7 +162,34 @@ function deviceIds(publicId) {
162
162
  },
163
163
  };
164
164
  }
165
- function eventSender(url, fetcher, mode, ids) {
165
+ /** The shopper's A/B test label (`abt_…:a`), from the last search answer that had one, kept for the visit so events
166
+ * on later pages carry it too. */
167
+ function testLabel(publicId) {
168
+ const key = `findlane:abtest:${publicId}`;
169
+ let current = null;
170
+ try {
171
+ current = sessionStorage.getItem(key);
172
+ }
173
+ catch {
174
+ // No storage: kept in memory only.
175
+ }
176
+ return {
177
+ get: () => current,
178
+ set(test) {
179
+ const label = `${test.id}:${test.variant}`;
180
+ if (label === current)
181
+ return;
182
+ current = label;
183
+ try {
184
+ sessionStorage.setItem(key, label);
185
+ }
186
+ catch {
187
+ // No storage: kept in memory only.
188
+ }
189
+ },
190
+ };
191
+ }
192
+ function eventSender(url, fetcher, mode, ids, test) {
166
193
  let queue = [];
167
194
  let allowed = mode === "afterConsent" ? undefined : mode;
168
195
  let timer;
@@ -187,8 +214,10 @@ function eventSender(url, fetcher, mode, ids) {
187
214
  const flush = () => {
188
215
  clearTimeout(timer);
189
216
  timer = undefined;
190
- while (allowed && queue.length)
191
- send(JSON.stringify({ ...ids?.use(Date.now(), page), events: queue.splice(0, 100) }));
217
+ while (allowed && queue.length) {
218
+ const label = test?.get();
219
+ send(JSON.stringify({ ...ids?.use(Date.now(), page), ...(label ? { abTest: label } : {}), events: queue.splice(0, 100) }));
220
+ }
192
221
  };
193
222
  const add = (event) => {
194
223
  if (allowed === false)
@@ -255,6 +284,7 @@ export class SearchBrowserClient {
255
284
  /** The shopper's recent searches, kept in this browser only. */
256
285
  recentSearches;
257
286
  popular = new Map();
287
+ test;
258
288
  constructor(options) {
259
289
  if (!/^pub_[a-f0-9]{32}$/.test(options.publicId))
260
290
  throw new Error("A valid public index ID (pub_…) is required");
@@ -263,7 +293,8 @@ export class SearchBrowserClient {
263
293
  this.origin = new URL(this.url).origin;
264
294
  this.fetcher = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
265
295
  this.signer = options.signedKey;
266
- this.events = eventSender(options.eventsUrl ?? `${this.url}/events`, options.fetch, options.events ?? true, options.visitorIds ? deviceIds(options.publicId) : null);
296
+ this.test = testLabel(options.publicId);
297
+ this.events = eventSender(options.eventsUrl ?? `${this.url}/events`, options.fetch, options.events ?? true, options.visitorIds ? deviceIds(options.publicId) : null, this.test);
267
298
  this.recentSearches = new RecentSearches(options.publicId);
268
299
  }
269
300
  /** The signed key searches send: the one given, or the function's, asked for again 30 seconds before it
@@ -323,8 +354,11 @@ export class SearchBrowserClient {
323
354
  link.crossOrigin = "anonymous";
324
355
  document.head.append(link);
325
356
  }
326
- search(request, options = {}) {
327
- return this.post("search", request, options.signal);
357
+ async search(request, options = {}) {
358
+ const response = await this.post("search", request, options.signal);
359
+ if (response.abTest)
360
+ this.test.set(response.abTest);
361
+ return response;
328
362
  }
329
363
  /** Several indexes searched in one request, such as products and a blog (0.22.0): up to 10 searches, each
330
364
  * naming its index's public ID (this client's when left out), whose allowed sites must include the page's.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@findlane/browser",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "description": "Findlane browser SDK: fast, typo-tolerant public search from the browser with an index's public ID, plus React hooks and components.",
5
5
  "keywords": ["findlane", "search", "full-text search", "typo tolerance", "instant search", "ecommerce search", "browser", "react", "autocomplete"],
6
6
  "license": "MIT",