@sensiblestats/widget-react 0.157.0 → 0.159.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.js CHANGED
@@ -1383,7 +1383,7 @@ function parseCustomTotal(text2, format2) {
1383
1383
  }
1384
1384
 
1385
1385
  // src/version.generated.ts
1386
- var WIDGET_REACT_VERSION = "0.157.0";
1386
+ var WIDGET_REACT_VERSION = "0.159.0";
1387
1387
 
1388
1388
  // src/config.ts
1389
1389
  var COLOR_KEYS = ["accent", "cta", "surface", "ink"];
@@ -1793,6 +1793,8 @@ function toClientOptions(cfg) {
1793
1793
  const opts = { operatorId: cfg.operatorId, publicKey: cfg.publicKey, client: `${cfg.transport?.client ?? "widget-react"}/${WIDGET_REACT_VERSION}` };
1794
1794
  if (cfg.baseUrl !== void 0) opts.baseUrl = cfg.baseUrl;
1795
1795
  if (cfg.transport?.fetch !== void 0) opts.fetch = cfg.transport.fetch;
1796
+ const timeZone = validZone(cfg.timeZone);
1797
+ if (timeZone !== void 0) opts.timeZone = timeZone;
1796
1798
  return opts;
1797
1799
  }
1798
1800
  function toUserContext(cfg) {
@@ -2801,15 +2803,29 @@ var STATES = /* @__PURE__ */ new WeakMap();
2801
2803
  function isValidId(value) {
2802
2804
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
2803
2805
  }
2806
+ var isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
2807
+ function betFieldsOf(bet) {
2808
+ const { stake, currency, betId, betType, totalOdds } = bet;
2809
+ return {
2810
+ ...isFiniteNumber(stake) ? { stake } : {},
2811
+ ...typeof currency === "string" ? { currency } : {},
2812
+ ...typeof betId === "string" ? { betId } : {},
2813
+ ...typeof betType === "string" ? { betType } : {},
2814
+ ...isFiniteNumber(totalOdds) ? { totalOdds } : {}
2815
+ };
2816
+ }
2804
2817
  function createWidgetHost() {
2805
- const state = { queue: [], sink: null, teaser: null };
2818
+ const state = { queue: [], sink: null, teaser: null, dropped: 0 };
2806
2819
  const dispatch = (command) => {
2807
2820
  if (state.sink) {
2808
2821
  state.sink(command);
2809
2822
  return;
2810
2823
  }
2811
2824
  state.queue.push(command);
2812
- if (state.queue.length > HOST_QUEUE_MAX) state.queue.shift();
2825
+ if (state.queue.length > HOST_QUEUE_MAX) {
2826
+ state.queue.shift();
2827
+ state.dropped++;
2828
+ }
2813
2829
  };
2814
2830
  const host = {
2815
2831
  // Every body sits in a try: a missing argument, a malformed one and a widget bug behind the
@@ -2838,7 +2854,7 @@ function createWidgetHost() {
2838
2854
  const ref = sport ? { matchOddsId: id, sport } : { matchOddsId: id };
2839
2855
  if (!legs.some((l) => sameLeg(l, ref))) legs.push(ref);
2840
2856
  }
2841
- if (legs.length > 0) dispatch({ kind: "betPlaced", legs });
2857
+ dispatch({ kind: "betPlaced", legs, legCount: raw.length, bet: betFieldsOf(bet) });
2842
2858
  } catch {
2843
2859
  }
2844
2860
  },
@@ -2891,11 +2907,19 @@ function attachTeaser(host, control) {
2891
2907
  if (state.teaser === control) state.teaser = null;
2892
2908
  };
2893
2909
  }
2894
- function attachHost(host, sink) {
2910
+ function attachHost(host, sink, onDropped) {
2895
2911
  const state = STATES.get(host);
2896
2912
  if (!state) return () => {
2897
2913
  };
2898
2914
  state.sink = sink;
2915
+ if (state.dropped > 0) {
2916
+ const count2 = state.dropped;
2917
+ state.dropped = 0;
2918
+ try {
2919
+ onDropped?.(count2);
2920
+ } catch {
2921
+ }
2922
+ }
2899
2923
  const queued = state.queue;
2900
2924
  state.queue = [];
2901
2925
  for (const command of queued) {
@@ -3122,6 +3146,22 @@ function toSlipEvent(name, entry) {
3122
3146
  }
3123
3147
 
3124
3148
  // src/useHostEvents.ts
3149
+ function recordRaw(logger, command) {
3150
+ if (!logger) return;
3151
+ try {
3152
+ if (command.kind === "matchOpened") {
3153
+ logger.log({ name: "host_match_opened", props: { matchId: command.matchId } });
3154
+ } else if (command.kind === "slipRemoved") {
3155
+ logger.log({ name: "host_slip_removed", props: { matchOddsId: command.matchOddsId, ...command.sport ? { sport: command.sport } : {} } });
3156
+ } else {
3157
+ logger.log({
3158
+ name: "host_bet_placed",
3159
+ props: { legs: command.legs.map((leg) => leg.sport ? { matchOddsId: leg.matchOddsId, sport: leg.sport } : { matchOddsId: leg.matchOddsId }), legCount: command.legCount, ...command.bet }
3160
+ });
3161
+ }
3162
+ } catch {
3163
+ }
3164
+ }
3125
3165
  function useHostEvents(opts) {
3126
3166
  const { host, operatorId, currentUser, logger, isOpen, closePanel } = opts;
3127
3167
  const [added, setAdded] = useState2({});
@@ -3174,9 +3214,12 @@ function useHostEvents(opts) {
3174
3214
  () => tracksViews ? createMatchViews(`ss-w-views:${operatorId}`, { user: currentUser }) : null,
3175
3215
  [tracksViews, operatorId, currentUser]
3176
3216
  );
3217
+ const loggerRef = useRef2(logger);
3218
+ loggerRef.current = logger;
3177
3219
  const sinkRef = useRef2(() => {
3178
3220
  });
3179
3221
  sinkRef.current = (command) => {
3222
+ recordRaw(logger, command);
3180
3223
  if (command.kind === "matchOpened") {
3181
3224
  if (views?.view(command.matchId) === "log") {
3182
3225
  logger?.log({ name: "match_view", entityType: "match", entityId: String(command.matchId) });
@@ -3197,7 +3240,7 @@ function useHostEvents(opts) {
3197
3240
  };
3198
3241
  useEffect2(() => {
3199
3242
  if (!host) return;
3200
- return attachHost(host, (command) => sinkRef.current(command));
3243
+ return attachHost(host, (command) => sinkRef.current(command), (count2) => loggerRef.current?.addHostDropped?.(count2));
3201
3244
  }, [host]);
3202
3245
  useEffect2(() => {
3203
3246
  if (!isOpen || !views) return;
@@ -8253,6 +8296,7 @@ function useMatchesDayPick(today) {
8253
8296
  }
8254
8297
 
8255
8298
  // src/tracking.ts
8299
+ import { isWidgetEventName } from "@sensiblestats/widget-sdk";
8256
8300
  function createTracker(read2) {
8257
8301
  return (body) => {
8258
8302
  const { handler, envelope } = read2();
@@ -8267,6 +8311,45 @@ function createTracker(read2) {
8267
8311
  });
8268
8312
  };
8269
8313
  }
8314
+ var PUBLIC_ONLY_NAMES = [
8315
+ "launcher_click",
8316
+ "bubble_click",
8317
+ "widget_close",
8318
+ "tab_change",
8319
+ "question_asked",
8320
+ "answer_received",
8321
+ "answer_failed",
8322
+ "answer_stopped",
8323
+ "new_conversation",
8324
+ "combo_variant_click",
8325
+ "combo_swap_click",
8326
+ "combo_add_click",
8327
+ "favourite_change"
8328
+ ];
8329
+ var PUBLIC_ONLY = new Set(PUBLIC_ONLY_NAMES);
8330
+ function isPublicOnly(name) {
8331
+ return PUBLIC_ONLY.has(name);
8332
+ }
8333
+ function createRecorder(logger, track) {
8334
+ return (input) => {
8335
+ try {
8336
+ if (isPublicOnly(input.name)) {
8337
+ track(input);
8338
+ if (logger) {
8339
+ const { name, ...props } = input;
8340
+ logger.log({ name, props });
8341
+ }
8342
+ return;
8343
+ }
8344
+ if (logger && isWidgetEventName(input.name)) {
8345
+ logger.log(input);
8346
+ return;
8347
+ }
8348
+ track(input);
8349
+ } catch {
8350
+ }
8351
+ };
8352
+ }
8270
8353
  function withTracking(logger, track) {
8271
8354
  return {
8272
8355
  log(input) {
@@ -8278,7 +8361,10 @@ function withTracking(logger, track) {
8278
8361
  flushNow: () => logger.flushNow(),
8279
8362
  flushOnHide: () => logger.flushOnHide(),
8280
8363
  dispose: () => logger.dispose(),
8281
- resume: () => logger.resume()
8364
+ resume: () => logger.resume(),
8365
+ markOpened: () => logger.markOpened?.(),
8366
+ markVisible: () => logger.markVisible?.(),
8367
+ addHostDropped: (count2) => logger.addHostDropped?.(count2)
8282
8368
  };
8283
8369
  }
8284
8370
  var entity = (input) => input;
@@ -8341,7 +8427,27 @@ var WIRE_TO_PUBLIC = {
8341
8427
  greeting_shown: null,
8342
8428
  greeting_cta_click: null,
8343
8429
  greeting_card_tap: null,
8344
- greeting_ask_tap: null
8430
+ greeting_ask_tap: null,
8431
+ answer_failed: null,
8432
+ answer_received: null,
8433
+ answer_stopped: null,
8434
+ bubble_click: null,
8435
+ combo_add_click: null,
8436
+ combo_swap_click: null,
8437
+ combo_variant_click: null,
8438
+ favourite_change: null,
8439
+ host_bet_placed: null,
8440
+ host_match_opened: null,
8441
+ host_page_change: null,
8442
+ host_slip_removed: null,
8443
+ launcher_click: null,
8444
+ native_command: null,
8445
+ new_conversation: null,
8446
+ props_too_large: null,
8447
+ question_asked: null,
8448
+ tab_change: null,
8449
+ widget_close: null,
8450
+ widget_unavailable: null
8345
8451
  };
8346
8452
 
8347
8453
  // src/popupTelemetry.ts
@@ -8426,6 +8532,12 @@ function useMobileBinding(args) {
8426
8532
  useEffect30(() => {
8427
8533
  if (theme) binding?.onTheme?.(theme);
8428
8534
  }, [binding, theme]);
8535
+ const record2 = args.record;
8536
+ useEffect30(() => {
8537
+ if (!binding?.onRecorder) return;
8538
+ binding.onRecorder(record2);
8539
+ return () => binding.onRecorder?.(null);
8540
+ }, [binding, record2]);
8429
8541
  const onCloseRequested = useCallback26(() => bindingRef.current?.onCloseRequested(), []);
8430
8542
  const onRegisterBack = useCallback26((fn) => {
8431
8543
  backRef.current = fn;
@@ -22434,19 +22546,34 @@ function StatsWidget(props) {
22434
22546
  onUnavailableRef.current?.(reason);
22435
22547
  } catch {
22436
22548
  }
22437
- }), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]);
22549
+ }), [injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId, cfg?.timeZone]);
22438
22550
  const trackingRef = useRef50({ envelope: { operatorId: "", widgetVersion: WIDGET_REACT_VERSION } });
22439
22551
  const track = useMemo17(() => createTracker(() => trackingRef.current), []);
22440
22552
  const eventTextRef = useRef50(false);
22441
22553
  eventTextRef.current = cfg?.eventText === true;
22554
+ const wireContextRef = useRef50(() => ({ widgetVersion: WIDGET_REACT_VERSION, surface: "react" }));
22555
+ const wireScopeRef = useRef50(() => ({}));
22556
+ const tenantKey = injectedClient ?? `${cfg?.operatorId}|${cfg?.publicKey}|${cfg?.baseUrl}`;
22557
+ const clientRef = useRef50(null);
22558
+ clientRef.current = built ? { client: built.client, key: tenantKey } : null;
22442
22559
  const logger = useMemo17(() => {
22443
22560
  if (!built) return null;
22561
+ const key = tenantKey;
22562
+ const current = () => clientRef.current?.key === key ? clientRef.current.client : built.client;
22444
22563
  const raw = built.client.createEventLogger({
22445
22564
  operatorUserId: cfg?.userContext?.operatorUserId,
22446
22565
  anonymousUserId: cfg?.userContext?.anonymousUserId
22566
+ }, {
22567
+ context: () => wireContextRef.current(),
22568
+ scope: () => wireScopeRef.current(),
22569
+ transport: {
22570
+ flush: (batch) => current().sendEvents(batch),
22571
+ flushKeepalive: (batch) => current().sendEventsKeepalive(batch)
22572
+ }
22447
22573
  });
22448
22574
  return withTracking(raw, track);
22449
- }, [built?.client, track]);
22575
+ }, [built !== null, injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, track]);
22576
+ const record2 = useMemo17(() => createRecorder(logger, track), [logger, track]);
22450
22577
  const widgetStrings = useWidgetStrings(cfg, built?.conversation ?? null);
22451
22578
  const stringsPending = widgetStrings.strings === void 0;
22452
22579
  const ui = useMemo17(
@@ -22466,7 +22593,7 @@ function StatsWidget(props) {
22466
22593
  () => ({ operatorId: cfg?.operatorId ?? "", operatorUserId: cfg?.userContext?.operatorUserId, anonymousUserId: cfg?.userContext?.anonymousUserId }),
22467
22594
  [cfg?.operatorId, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]
22468
22595
  );
22469
- const chatTracking = useMemo17(() => ({ track, eventText: () => eventTextRef.current }), [track]);
22596
+ const chatTracking = useMemo17(() => ({ track: record2, eventText: () => eventTextRef.current }), [record2]);
22470
22597
  const controller = useChatStream(built?.conversation ?? emptyConversation(), ui.starters, built ? ui.greetingMessage : void 0, convStorageKey, cfg?.page, sportScope, language, chatTracking, injectedClient ? oddsSettings.userOddsFormat : oddsSettings.oddsFormat);
22471
22598
  const panelOpen = controller.state.isOpen && !stringsPending;
22472
22599
  const liveCardWatches = useMemo17(() => {
@@ -22632,7 +22759,7 @@ function StatsWidget(props) {
22632
22759
  operatorUserId,
22633
22760
  anonymousUserId,
22634
22761
  enabled: hasFavouritesIdentity && panelEverOpened,
22635
- track,
22762
+ track: record2,
22636
22763
  onChanged: discovery.refreshSchedule
22637
22764
  });
22638
22765
  const favouritesEnabled = hasFavouritesIdentity && favourites.status !== "refused";
@@ -22727,7 +22854,8 @@ function StatsWidget(props) {
22727
22854
  close: controller.close,
22728
22855
  dismissPopup: () => dismissPopupRef.current(),
22729
22856
  labels: stringsPending ? null : { launcher: ui.launcherLabel, dismiss: ui.dismissGreeting },
22730
- theme: resolvedTheme
22857
+ theme: resolvedTheme,
22858
+ record: record2
22731
22859
  });
22732
22860
  const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22733
22861
  dismissPopupRef.current = dismissPopup;
@@ -22754,12 +22882,15 @@ function StatsWidget(props) {
22754
22882
  };
22755
22883
  onTeaserTapRef.current = () => {
22756
22884
  if (!controller.state.popupText) return;
22757
- track({ name: "bubble_click" });
22885
+ record2({ name: "bubble_click" });
22758
22886
  if (!bubbleCta) logCtaClick();
22759
22887
  openFromCta();
22760
22888
  };
22761
22889
  const onCta = useCallback31(() => onCtaRef.current(), []);
22762
22890
  useOpenTelemetry(logger, panelOpen);
22891
+ useEffect59(() => {
22892
+ if (panelOpen) logger?.markOpened?.();
22893
+ }, [panelOpen, logger]);
22763
22894
  const onControlRef = useRef50(props.onControl);
22764
22895
  onControlRef.current = props.onControl;
22765
22896
  const { open: openPanel, close: closePanel } = controller;
@@ -22780,6 +22911,34 @@ function StatsWidget(props) {
22780
22911
  page: cfg?.page
22781
22912
  }
22782
22913
  };
22914
+ const themeAttr = themeAttrs(ui, oddsSettings.userTheme)["data-ss-w-theme"];
22915
+ const surface = props.mobile ? "mobile" : props.surface ?? "react";
22916
+ const resolvedLanguage = widgetStrings.strings?.tag;
22917
+ const tenant = clubFixturesResult.enabled ? "club" : "sportsbook";
22918
+ const oddsFormat = oddsSettings.oddsFormat;
22919
+ wireContextRef.current = () => ({
22920
+ widgetVersion: WIDGET_REACT_VERSION,
22921
+ surface,
22922
+ ...props.mobile?.client ? { client: props.mobile.client } : {},
22923
+ language,
22924
+ resolvedLanguage,
22925
+ tenant,
22926
+ oddsFormat,
22927
+ theme: themeAttr,
22928
+ layout: typeof window.matchMedia === "function" && window.matchMedia(FULLSCREEN_QUERY).matches ? "fullscreen" : "modal",
22929
+ viewport: { w: Math.round(window.innerWidth / 10) * 10, h: Math.round(window.innerHeight / 10) * 10 }
22930
+ });
22931
+ const pageKey2 = JSON.stringify(cfg?.page ?? null);
22932
+ const pageSeen = useRef50(null);
22933
+ useEffect59(() => {
22934
+ const prev = pageSeen.current;
22935
+ pageSeen.current = pageKey2;
22936
+ if (prev === null || prev === pageKey2) return;
22937
+ record2({ name: "host_page_change", props: cfg?.page ? { page: { ...cfg.page } } : {} });
22938
+ }, [pageKey2]);
22939
+ const activeTab = monitor.activeTab;
22940
+ const scopePage = cfg?.page;
22941
+ wireScopeRef.current = () => ({ ...panelOpen ? { tab: activeTab } : {}, ...scopePage ? { page: scopePage } : {} });
22783
22942
  useEffect59(() => {
22784
22943
  if (!logger) return;
22785
22944
  logger.setIdentity({ operatorUserId, anonymousUserId, conversationId });
@@ -22792,6 +22951,7 @@ function StatsWidget(props) {
22792
22951
  };
22793
22952
  const onVisibilityHide = () => {
22794
22953
  if (document.visibilityState === "hidden") logger.flushOnHide();
22954
+ else logger.markVisible?.();
22795
22955
  };
22796
22956
  window.addEventListener("pagehide", onHide);
22797
22957
  document.addEventListener("visibilitychange", onVisibilityHide);
@@ -22825,7 +22985,7 @@ function StatsWidget(props) {
22825
22985
  }), [monitor, favouritesEnabled, monitor.enabled, monitor.setActiveTab, clubFixturesResult.settled, clubFixturesResult.tabReady, clubFixturesResult.enabled, favourites, built, holds, builderMemory]);
22826
22986
  if (!built || widgetStrings.state === "unavailable") return null;
22827
22987
  const attrs = themeAttrs(ui, oddsSettings.userTheme);
22828
- return /* @__PURE__ */ jsx164(EventLogContext.Provider, { value: logger, children: /* @__PURE__ */ jsx164(TrackingContext.Provider, { value: track, children: /* @__PURE__ */ jsx164(SportScopeContext.Provider, { value: sportScope, children: /* @__PURE__ */ jsx164(OddsSettingsContext.Provider, { value: oddsSettings, children: /* @__PURE__ */ jsx164(HostEventsProvider, { value: hostEvents, children: /* @__PURE__ */ jsx164("div", { ref: rootRef, className: "ss-w-root", "data-ss-w-theme": attrs["data-ss-w-theme"], "data-ss-w-pos": attrs["data-ss-w-pos"], "data-ss-w-mobile": props.mobile ? "" : void 0, style: attrs.style, children: panelOpen ? /* @__PURE__ */ jsx164(PersonalGreetingContext.Provider, { value: personalGreeting, children: /* @__PURE__ */ jsx164(GreetingHighlightContext.Provider, { value: highlightRequest, children: /* @__PURE__ */ jsx164(
22988
+ return /* @__PURE__ */ jsx164(EventLogContext.Provider, { value: logger, children: /* @__PURE__ */ jsx164(TrackingContext.Provider, { value: record2, children: /* @__PURE__ */ jsx164(SportScopeContext.Provider, { value: sportScope, children: /* @__PURE__ */ jsx164(OddsSettingsContext.Provider, { value: oddsSettings, children: /* @__PURE__ */ jsx164(HostEventsProvider, { value: hostEvents, children: /* @__PURE__ */ jsx164("div", { ref: rootRef, className: "ss-w-root", "data-ss-w-theme": attrs["data-ss-w-theme"], "data-ss-w-pos": attrs["data-ss-w-pos"], "data-ss-w-mobile": props.mobile ? "" : void 0, style: attrs.style, children: panelOpen ? /* @__PURE__ */ jsx164(PersonalGreetingContext.Provider, { value: personalGreeting, children: /* @__PURE__ */ jsx164(GreetingHighlightContext.Provider, { value: highlightRequest, children: /* @__PURE__ */ jsx164(
22829
22989
  ChatPanel,
22830
22990
  {
22831
22991
  controller,