@sensiblestats/widget-react 0.158.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.158.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"];
@@ -2803,15 +2803,29 @@ var STATES = /* @__PURE__ */ new WeakMap();
2803
2803
  function isValidId(value) {
2804
2804
  return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
2805
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
+ }
2806
2817
  function createWidgetHost() {
2807
- const state = { queue: [], sink: null, teaser: null };
2818
+ const state = { queue: [], sink: null, teaser: null, dropped: 0 };
2808
2819
  const dispatch = (command) => {
2809
2820
  if (state.sink) {
2810
2821
  state.sink(command);
2811
2822
  return;
2812
2823
  }
2813
2824
  state.queue.push(command);
2814
- 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
+ }
2815
2829
  };
2816
2830
  const host = {
2817
2831
  // Every body sits in a try: a missing argument, a malformed one and a widget bug behind the
@@ -2840,7 +2854,7 @@ function createWidgetHost() {
2840
2854
  const ref = sport ? { matchOddsId: id, sport } : { matchOddsId: id };
2841
2855
  if (!legs.some((l) => sameLeg(l, ref))) legs.push(ref);
2842
2856
  }
2843
- if (legs.length > 0) dispatch({ kind: "betPlaced", legs });
2857
+ dispatch({ kind: "betPlaced", legs, legCount: raw.length, bet: betFieldsOf(bet) });
2844
2858
  } catch {
2845
2859
  }
2846
2860
  },
@@ -2893,11 +2907,19 @@ function attachTeaser(host, control) {
2893
2907
  if (state.teaser === control) state.teaser = null;
2894
2908
  };
2895
2909
  }
2896
- function attachHost(host, sink) {
2910
+ function attachHost(host, sink, onDropped) {
2897
2911
  const state = STATES.get(host);
2898
2912
  if (!state) return () => {
2899
2913
  };
2900
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
+ }
2901
2923
  const queued = state.queue;
2902
2924
  state.queue = [];
2903
2925
  for (const command of queued) {
@@ -3124,6 +3146,22 @@ function toSlipEvent(name, entry) {
3124
3146
  }
3125
3147
 
3126
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
+ }
3127
3165
  function useHostEvents(opts) {
3128
3166
  const { host, operatorId, currentUser, logger, isOpen, closePanel } = opts;
3129
3167
  const [added, setAdded] = useState2({});
@@ -3176,9 +3214,12 @@ function useHostEvents(opts) {
3176
3214
  () => tracksViews ? createMatchViews(`ss-w-views:${operatorId}`, { user: currentUser }) : null,
3177
3215
  [tracksViews, operatorId, currentUser]
3178
3216
  );
3217
+ const loggerRef = useRef2(logger);
3218
+ loggerRef.current = logger;
3179
3219
  const sinkRef = useRef2(() => {
3180
3220
  });
3181
3221
  sinkRef.current = (command) => {
3222
+ recordRaw(logger, command);
3182
3223
  if (command.kind === "matchOpened") {
3183
3224
  if (views?.view(command.matchId) === "log") {
3184
3225
  logger?.log({ name: "match_view", entityType: "match", entityId: String(command.matchId) });
@@ -3199,7 +3240,7 @@ function useHostEvents(opts) {
3199
3240
  };
3200
3241
  useEffect2(() => {
3201
3242
  if (!host) return;
3202
- return attachHost(host, (command) => sinkRef.current(command));
3243
+ return attachHost(host, (command) => sinkRef.current(command), (count2) => loggerRef.current?.addHostDropped?.(count2));
3203
3244
  }, [host]);
3204
3245
  useEffect2(() => {
3205
3246
  if (!isOpen || !views) return;
@@ -8255,6 +8296,7 @@ function useMatchesDayPick(today) {
8255
8296
  }
8256
8297
 
8257
8298
  // src/tracking.ts
8299
+ import { isWidgetEventName } from "@sensiblestats/widget-sdk";
8258
8300
  function createTracker(read2) {
8259
8301
  return (body) => {
8260
8302
  const { handler, envelope } = read2();
@@ -8269,6 +8311,45 @@ function createTracker(read2) {
8269
8311
  });
8270
8312
  };
8271
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
+ }
8272
8353
  function withTracking(logger, track) {
8273
8354
  return {
8274
8355
  log(input) {
@@ -8280,7 +8361,10 @@ function withTracking(logger, track) {
8280
8361
  flushNow: () => logger.flushNow(),
8281
8362
  flushOnHide: () => logger.flushOnHide(),
8282
8363
  dispose: () => logger.dispose(),
8283
- resume: () => logger.resume()
8364
+ resume: () => logger.resume(),
8365
+ markOpened: () => logger.markOpened?.(),
8366
+ markVisible: () => logger.markVisible?.(),
8367
+ addHostDropped: (count2) => logger.addHostDropped?.(count2)
8284
8368
  };
8285
8369
  }
8286
8370
  var entity = (input) => input;
@@ -8343,7 +8427,27 @@ var WIRE_TO_PUBLIC = {
8343
8427
  greeting_shown: null,
8344
8428
  greeting_cta_click: null,
8345
8429
  greeting_card_tap: null,
8346
- 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
8347
8451
  };
8348
8452
 
8349
8453
  // src/popupTelemetry.ts
@@ -8428,6 +8532,12 @@ function useMobileBinding(args) {
8428
8532
  useEffect30(() => {
8429
8533
  if (theme) binding?.onTheme?.(theme);
8430
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]);
8431
8541
  const onCloseRequested = useCallback26(() => bindingRef.current?.onCloseRequested(), []);
8432
8542
  const onRegisterBack = useCallback26((fn) => {
8433
8543
  backRef.current = fn;
@@ -22441,14 +22551,29 @@ function StatsWidget(props) {
22441
22551
  const track = useMemo17(() => createTracker(() => trackingRef.current), []);
22442
22552
  const eventTextRef = useRef50(false);
22443
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;
22444
22559
  const logger = useMemo17(() => {
22445
22560
  if (!built) return null;
22561
+ const key = tenantKey;
22562
+ const current = () => clientRef.current?.key === key ? clientRef.current.client : built.client;
22446
22563
  const raw = built.client.createEventLogger({
22447
22564
  operatorUserId: cfg?.userContext?.operatorUserId,
22448
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
+ }
22449
22573
  });
22450
22574
  return withTracking(raw, track);
22451
- }, [built?.client, track]);
22575
+ }, [built !== null, injectedClient, cfg?.operatorId, cfg?.publicKey, cfg?.baseUrl, track]);
22576
+ const record2 = useMemo17(() => createRecorder(logger, track), [logger, track]);
22452
22577
  const widgetStrings = useWidgetStrings(cfg, built?.conversation ?? null);
22453
22578
  const stringsPending = widgetStrings.strings === void 0;
22454
22579
  const ui = useMemo17(
@@ -22468,7 +22593,7 @@ function StatsWidget(props) {
22468
22593
  () => ({ operatorId: cfg?.operatorId ?? "", operatorUserId: cfg?.userContext?.operatorUserId, anonymousUserId: cfg?.userContext?.anonymousUserId }),
22469
22594
  [cfg?.operatorId, cfg?.userContext?.operatorUserId, cfg?.userContext?.anonymousUserId]
22470
22595
  );
22471
- const chatTracking = useMemo17(() => ({ track, eventText: () => eventTextRef.current }), [track]);
22596
+ const chatTracking = useMemo17(() => ({ track: record2, eventText: () => eventTextRef.current }), [record2]);
22472
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);
22473
22598
  const panelOpen = controller.state.isOpen && !stringsPending;
22474
22599
  const liveCardWatches = useMemo17(() => {
@@ -22634,7 +22759,7 @@ function StatsWidget(props) {
22634
22759
  operatorUserId,
22635
22760
  anonymousUserId,
22636
22761
  enabled: hasFavouritesIdentity && panelEverOpened,
22637
- track,
22762
+ track: record2,
22638
22763
  onChanged: discovery.refreshSchedule
22639
22764
  });
22640
22765
  const favouritesEnabled = hasFavouritesIdentity && favourites.status !== "refused";
@@ -22729,7 +22854,8 @@ function StatsWidget(props) {
22729
22854
  close: controller.close,
22730
22855
  dismissPopup: () => dismissPopupRef.current(),
22731
22856
  labels: stringsPending ? null : { launcher: ui.launcherLabel, dismiss: ui.dismissGreeting },
22732
- theme: resolvedTheme
22857
+ theme: resolvedTheme,
22858
+ record: record2
22733
22859
  });
22734
22860
  const dismissPopup = usePopupTelemetry(logger, panelOpen, controller.state.popupText, controller.dismissPopup, hostDrawsLauncher ? mobile.confirmedShown : void 0);
22735
22861
  dismissPopupRef.current = dismissPopup;
@@ -22756,12 +22882,15 @@ function StatsWidget(props) {
22756
22882
  };
22757
22883
  onTeaserTapRef.current = () => {
22758
22884
  if (!controller.state.popupText) return;
22759
- track({ name: "bubble_click" });
22885
+ record2({ name: "bubble_click" });
22760
22886
  if (!bubbleCta) logCtaClick();
22761
22887
  openFromCta();
22762
22888
  };
22763
22889
  const onCta = useCallback31(() => onCtaRef.current(), []);
22764
22890
  useOpenTelemetry(logger, panelOpen);
22891
+ useEffect59(() => {
22892
+ if (panelOpen) logger?.markOpened?.();
22893
+ }, [panelOpen, logger]);
22765
22894
  const onControlRef = useRef50(props.onControl);
22766
22895
  onControlRef.current = props.onControl;
22767
22896
  const { open: openPanel, close: closePanel } = controller;
@@ -22782,6 +22911,34 @@ function StatsWidget(props) {
22782
22911
  page: cfg?.page
22783
22912
  }
22784
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 } : {} });
22785
22942
  useEffect59(() => {
22786
22943
  if (!logger) return;
22787
22944
  logger.setIdentity({ operatorUserId, anonymousUserId, conversationId });
@@ -22794,6 +22951,7 @@ function StatsWidget(props) {
22794
22951
  };
22795
22952
  const onVisibilityHide = () => {
22796
22953
  if (document.visibilityState === "hidden") logger.flushOnHide();
22954
+ else logger.markVisible?.();
22797
22955
  };
22798
22956
  window.addEventListener("pagehide", onHide);
22799
22957
  document.addEventListener("visibilitychange", onVisibilityHide);
@@ -22827,7 +22985,7 @@ function StatsWidget(props) {
22827
22985
  }), [monitor, favouritesEnabled, monitor.enabled, monitor.setActiveTab, clubFixturesResult.settled, clubFixturesResult.tabReady, clubFixturesResult.enabled, favourites, built, holds, builderMemory]);
22828
22986
  if (!built || widgetStrings.state === "unavailable") return null;
22829
22987
  const attrs = themeAttrs(ui, oddsSettings.userTheme);
22830
- 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(
22831
22989
  ChatPanel,
22832
22990
  {
22833
22991
  controller,