@brainfish-ai/web-tracker 0.0.20 → 0.0.21

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.
@@ -3357,6 +3357,44 @@ class Client {
3357
3357
  this.socket.on(eventName, (data) => handler(this.decode(data)));
3358
3358
  }
3359
3359
  }
3360
+ class HttpClient {
3361
+ constructor(config) {
3362
+ __publicField(this, "baseUrl");
3363
+ __publicField(this, "authToken");
3364
+ __publicField(this, "sdkMetadata");
3365
+ this.baseUrl = config.baseUrl;
3366
+ this.authToken = config.authToken;
3367
+ this.sdkMetadata = config.sdkMetadata;
3368
+ }
3369
+ async send(endpoint, data) {
3370
+ const url2 = new URL(endpoint, this.baseUrl);
3371
+ const requestBody = {
3372
+ ...data,
3373
+ ...this.sdkMetadata && { sdk: this.sdkMetadata }
3374
+ };
3375
+ try {
3376
+ const response = await fetch(url2.toString(), {
3377
+ method: "POST",
3378
+ headers: {
3379
+ "Content-Type": "application/json",
3380
+ ...this.authToken && { Authorization: `Bearer ${this.authToken}` }
3381
+ },
3382
+ body: JSON.stringify(requestBody)
3383
+ });
3384
+ if (!response.ok) {
3385
+ return null;
3386
+ }
3387
+ let responseData;
3388
+ try {
3389
+ responseData = await response.json();
3390
+ } catch {
3391
+ responseData = {};
3392
+ }
3393
+ return responseData;
3394
+ } catch (error) {
3395
+ }
3396
+ }
3397
+ }
3360
3398
  class TTLSet {
3361
3399
  constructor(defaultTTL) {
3362
3400
  __publicField(this, "defaultTTL");
@@ -3434,6 +3472,7 @@ class TrackerSdk {
3434
3472
  constructor(options) {
3435
3473
  __publicField(this, "options");
3436
3474
  __publicField(this, "client");
3475
+ __publicField(this, "httpClient");
3437
3476
  __publicField(this, "userId");
3438
3477
  __publicField(this, "global");
3439
3478
  __publicField(this, "queue", []);
@@ -3446,11 +3485,21 @@ class TrackerSdk {
3446
3485
  sdk_name: options.sdk || "node",
3447
3486
  sdk_version: options.sdkVersion || ""
3448
3487
  };
3488
+ const baseUrl = options.apiUrl || "https://analytic.brainfi.sh";
3489
+ const authToken = options.accessKey;
3449
3490
  this.client = new Client({
3450
- baseUrl: options.apiUrl || "https://analytic.brainfi.sh",
3451
- authToken: options.accessKey,
3491
+ baseUrl,
3492
+ authToken,
3452
3493
  query
3453
3494
  });
3495
+ this.httpClient = new HttpClient({
3496
+ baseUrl,
3497
+ authToken,
3498
+ sdkMetadata: {
3499
+ name: options.sdk || "node",
3500
+ version: options.sdkVersion || ""
3501
+ }
3502
+ });
3454
3503
  this.setupIncomingPayloadHandler();
3455
3504
  }
3456
3505
  // placeholder for future use
@@ -3537,6 +3586,8 @@ class TrackerSdk {
3537
3586
  if (payload.userId) {
3538
3587
  this.userId = payload.userId;
3539
3588
  this.flush();
3589
+ } else {
3590
+ console.warn("BrainfishWidgetWarn: `.identify` called without a userId. User identification requires a valid userId to be provided.");
3540
3591
  }
3541
3592
  const iframe = document.querySelector("#bf-iframe-container .trigger-iframe");
3542
3593
  if (iframe) {
@@ -3566,14 +3617,25 @@ class TrackerSdk {
3566
3617
  });
3567
3618
  }
3568
3619
  }
3569
- async record(events, callback) {
3570
- return this.send({
3620
+ async record(events) {
3621
+ if (this.options.disabled) {
3622
+ return Promise.resolve(null);
3623
+ }
3624
+ const payload = {
3571
3625
  type: "event.record",
3572
3626
  payload: {
3573
3627
  events,
3574
3628
  userId: this.userId
3575
3629
  }
3576
- }, callback);
3630
+ };
3631
+ if (this.options.filter && !this.options.filter(payload)) {
3632
+ return Promise.resolve(null);
3633
+ }
3634
+ const response = await this.httpClient.send("/s", {
3635
+ events,
3636
+ userId: this.userId
3637
+ });
3638
+ return response?.sessionId || null;
3577
3639
  }
3578
3640
  async alias(payload) {
3579
3641
  return this.send({
@@ -38367,7 +38429,7 @@ function toCamelCase(str) {
38367
38429
  ($1) => $1.toUpperCase().replace("-", "").replace("_", "")
38368
38430
  );
38369
38431
  }
38370
- const VERSION = "0.0.20";
38432
+ const VERSION = "0.0.21";
38371
38433
  class Tracker extends TrackerSdk {
38372
38434
  // 750KB
38373
38435
  constructor(options) {
@@ -38539,14 +38601,12 @@ class Tracker extends TrackerSdk {
38539
38601
  * @param events Array of events to be recorded
38540
38602
  * @returns Promise that resolves when events are recorded and session is managed
38541
38603
  */
38542
- // NB: this responds to the websocket request, payload is the response
38543
38604
  async recordEventsToServer(events) {
38544
- this.record(events, (payload) => {
38545
- this.sessionManager.updateSession(payload.sessionId, {
38546
- takeFullSnapshot: () => {
38547
- this.agent.takeFullSnapshot();
38548
- }
38549
- });
38605
+ const sessionId = await this.record(events);
38606
+ this.sessionManager.updateSession(sessionId, {
38607
+ takeFullSnapshot: () => {
38608
+ this.agent.takeFullSnapshot();
38609
+ }
38550
38610
  });
38551
38611
  }
38552
38612
  sendScreenViewEvent(props) {
Binary file