@brainfish-ai/web-tracker 0.0.22 → 0.0.24

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/tracker.d.ts CHANGED
@@ -2,7 +2,7 @@ declare global {
2
2
  interface Window {
3
3
  BrainfishAnalytics: {
4
4
  q?: [string, ...any[]];
5
- (method: string, ...args: any[]): void;
5
+ (method: string, ...args: any[]): any;
6
6
  };
7
7
  }
8
8
  }
@@ -3468,7 +3468,7 @@ var NudgeCTABlockType;
3468
3468
  NudgeCTABlockType2["Button"] = "button";
3469
3469
  NudgeCTABlockType2["Link"] = "link";
3470
3470
  })(NudgeCTABlockType || (NudgeCTABlockType = {}));
3471
- class TrackerSdk {
3471
+ const _TrackerSdk = class _TrackerSdk {
3472
3472
  constructor(options) {
3473
3473
  __publicField(this, "options");
3474
3474
  __publicField(this, "client");
@@ -3480,6 +3480,7 @@ class TrackerSdk {
3480
3480
  __publicField(this, "eventListeners", {});
3481
3481
  // this set is used to prevent showing the same nudge multiple times in a short period
3482
3482
  __publicField(this, "recentNudgeIds", new TTLSet(1e3));
3483
+ __publicField(this, "trackingDisabled", false);
3483
3484
  this.options = options;
3484
3485
  const query = {
3485
3486
  sdk_name: options.sdk || "node",
@@ -3500,6 +3501,9 @@ class TrackerSdk {
3500
3501
  version: options.sdkVersion || ""
3501
3502
  }
3502
3503
  });
3504
+ if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
3505
+ this.trackingDisabled = localStorage.getItem(_TrackerSdk.TRACKING_DISABLED_KEY) === "true";
3506
+ }
3503
3507
  this.setupIncomingPayloadHandler();
3504
3508
  }
3505
3509
  // placeholder for future use
@@ -3552,6 +3556,9 @@ class TrackerSdk {
3552
3556
  if (this.options.disabled) {
3553
3557
  return Promise.resolve();
3554
3558
  }
3559
+ if (this.trackingDisabled) {
3560
+ return Promise.resolve();
3561
+ }
3555
3562
  if (this.options.filter && !this.options.filter(payload)) {
3556
3563
  return Promise.resolve();
3557
3564
  }
@@ -3589,6 +3596,22 @@ class TrackerSdk {
3589
3596
  } else {
3590
3597
  console.warn("BrainfishWidgetWarn: `.identify` called without a userId. User identification requires a valid userId to be provided.");
3591
3598
  }
3599
+ const allowedFields = /* @__PURE__ */ new Set([
3600
+ "userId",
3601
+ "firstName",
3602
+ "lastName",
3603
+ "email",
3604
+ "phone",
3605
+ "avatar",
3606
+ "properties"
3607
+ ]);
3608
+ const extraFields = Object.keys(payload).filter((key) => !allowedFields.has(key));
3609
+ if (extraFields.length > 0) {
3610
+ console.warn(`BrainfishWidgetWarn: The following fields are not recognized as main identifier fields: ${extraFields.join(", ")}. Please place custom fields under the 'properties' object instead. Note: Using fields incorrectly may cause user identification to not work as expected.`);
3611
+ }
3612
+ if (payload.phone && !/^\+[1-9]\d{1,14}$/.test(payload.phone)) {
3613
+ console.warn("BrainfishWidgetWarn: phone should be in E.164 format (e.g., +14155552671). Providing the correct format ensures accurate user identification.");
3614
+ }
3592
3615
  const iframe = document.querySelector("#bf-iframe-container .trigger-iframe");
3593
3616
  if (iframe) {
3594
3617
  const sendMessage = () => {
@@ -3621,6 +3644,9 @@ class TrackerSdk {
3621
3644
  if (this.options.disabled) {
3622
3645
  return Promise.resolve(null);
3623
3646
  }
3647
+ if (this.trackingDisabled) {
3648
+ return Promise.resolve(null);
3649
+ }
3624
3650
  const payload = {
3625
3651
  type: "event.record",
3626
3652
  payload: {
@@ -3662,8 +3688,6 @@ class TrackerSdk {
3662
3688
  this.queue.forEach((item) => {
3663
3689
  this.send({
3664
3690
  ...item,
3665
- // Not sure why ts-expect-error is needed here
3666
- // @ts-expect-error
3667
3691
  payload: {
3668
3692
  ...item.payload,
3669
3693
  userId: item.payload.userId ?? this.userId
@@ -3672,7 +3696,37 @@ class TrackerSdk {
3672
3696
  });
3673
3697
  this.queue = [];
3674
3698
  }
3675
- }
3699
+ /**
3700
+ * Disables privacy-sensitive tracking features (recording, screenshots).
3701
+ * This setting persists across page reloads via localStorage.
3702
+ */
3703
+ disableTracking() {
3704
+ this.trackingDisabled = true;
3705
+ if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
3706
+ localStorage.setItem(_TrackerSdk.TRACKING_DISABLED_KEY, "true");
3707
+ }
3708
+ }
3709
+ /**
3710
+ * Re-enables privacy-sensitive tracking features (recording, screenshots).
3711
+ * Note: Full effect requires a page reload to restart recording.
3712
+ */
3713
+ enableTracking() {
3714
+ this.trackingDisabled = false;
3715
+ if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
3716
+ localStorage.removeItem(_TrackerSdk.TRACKING_DISABLED_KEY);
3717
+ }
3718
+ console.log("Brainfish tracking re-enabled. Refresh page for full effect.");
3719
+ }
3720
+ /**
3721
+ * Returns whether privacy-sensitive tracking is currently disabled.
3722
+ * @returns true if tracking is disabled, false otherwise
3723
+ */
3724
+ isTrackingDisabled() {
3725
+ return this.trackingDisabled;
3726
+ }
3727
+ };
3728
+ __publicField(_TrackerSdk, "TRACKING_DISABLED_KEY", "bf_tracking_disabled");
3729
+ let TrackerSdk = _TrackerSdk;
3676
3730
  var cache = {
3677
3731
  image: /* @__PURE__ */ new Map(),
3678
3732
  background: /* @__PURE__ */ new Map(),
@@ -38426,9 +38480,8 @@ function toCamelCase(str) {
38426
38480
  ($1) => $1.toUpperCase().replace("-", "").replace("_", "")
38427
38481
  );
38428
38482
  }
38429
- const VERSION = "0.0.22";
38483
+ const VERSION = "0.0.24";
38430
38484
  class Tracker extends TrackerSdk {
38431
- // 750KB
38432
38485
  constructor(options) {
38433
38486
  super({
38434
38487
  sdk: "web",
@@ -38441,6 +38494,8 @@ class Tracker extends TrackerSdk {
38441
38494
  __publicField(this, "sessionManager", SessionManager.getInstance());
38442
38495
  __publicField(this, "recordingBlocklist", []);
38443
38496
  __publicField(this, "MAX_SCREENSHOT_SIZE", 750 * 1024);
38497
+ // 750KB
38498
+ __publicField(this, "isRecordingActive", false);
38444
38499
  this.options = options;
38445
38500
  this.agent = new Agent();
38446
38501
  this.recordingBlocklist = options.recordingBlocklist || [];
@@ -38448,17 +38503,19 @@ class Tracker extends TrackerSdk {
38448
38503
  this.setGlobalProperties({
38449
38504
  __referrer: document.referrer
38450
38505
  });
38451
- if (this.options.trackOutgoingLinks) {
38506
+ const trackingDisabled = this.isTrackingDisabled();
38507
+ if (this.options.trackOutgoingLinks && !trackingDisabled) {
38452
38508
  this.trackOutgoingLinks();
38453
38509
  }
38454
38510
  if (this.options.trackScreenViews) {
38455
38511
  this.trackScreenViews();
38456
38512
  }
38457
- if (this.options.trackAttributes) {
38513
+ if (this.options.trackAttributes && !trackingDisabled) {
38458
38514
  this.trackAttributes();
38459
38515
  }
38460
- if (this.options.enableRecording && this.options._allowScreenRecording) {
38516
+ if (this.options.enableRecording && this.options._allowScreenRecording && !trackingDisabled) {
38461
38517
  this.startRecording();
38518
+ this.isRecordingActive = true;
38462
38519
  console.log("Brainfish has started ambient learning");
38463
38520
  }
38464
38521
  }
@@ -38573,7 +38630,7 @@ class Tracker extends TrackerSdk {
38573
38630
  __path: path,
38574
38631
  __title: document.title
38575
38632
  };
38576
- if (this.options.enableRecording && canDiscover(
38633
+ if (this.options.enableRecording && !this.isTrackingDisabled() && canDiscover(
38577
38634
  this.recordingBlocklist,
38578
38635
  this.options._allowLocalhostRecording
38579
38636
  )) {
@@ -38611,6 +38668,26 @@ class Tracker extends TrackerSdk {
38611
38668
  super.track("screen_view", props);
38612
38669
  this.sessionManager.updateLastScreenViewEventTime();
38613
38670
  }
38671
+ /**
38672
+ * Disables privacy-sensitive tracking features and stops active recording.
38673
+ */
38674
+ disableTracking() {
38675
+ super.disableTracking();
38676
+ if (this.isRecordingActive) {
38677
+ if (this.agent.recordingManager.recorder.stop) {
38678
+ this.agent.recordingManager.recorder.stop();
38679
+ }
38680
+ this.isRecordingActive = false;
38681
+ console.log("Brainfish has stopped ambient learning");
38682
+ }
38683
+ }
38684
+ /**
38685
+ * Re-enables privacy-sensitive tracking features.
38686
+ * Note: Full effect requires a page reload to restart recording and event listeners.
38687
+ */
38688
+ enableTracking() {
38689
+ super.enableTracking();
38690
+ }
38614
38691
  }
38615
38692
  ((window2) => {
38616
38693
  if (window2.BrainfishAnalytics && "q" in window2.BrainfishAnalytics) {
@@ -38630,7 +38707,7 @@ class Tracker extends TrackerSdk {
38630
38707
  window2.BrainfishAnalytics = (t, ...args) => {
38631
38708
  const fn = tracker[t] ? tracker[t].bind(tracker) : void 0;
38632
38709
  if (typeof fn === "function") {
38633
- fn(...args);
38710
+ return fn(...args);
38634
38711
  } else {
38635
38712
  console.warn(`Method ${t} does not exist on BrainfishAnalytics`);
38636
38713
  }
Binary file