@brainfish-ai/web-tracker 0.0.20 → 0.0.21-rc.1

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
@@ -3566,14 +3615,25 @@ class TrackerSdk {
3566
3615
  });
3567
3616
  }
3568
3617
  }
3569
- async record(events, callback) {
3570
- return this.send({
3618
+ async record(events) {
3619
+ if (this.options.disabled) {
3620
+ return Promise.resolve(null);
3621
+ }
3622
+ const payload = {
3571
3623
  type: "event.record",
3572
3624
  payload: {
3573
3625
  events,
3574
3626
  userId: this.userId
3575
3627
  }
3576
- }, callback);
3628
+ };
3629
+ if (this.options.filter && !this.options.filter(payload)) {
3630
+ return Promise.resolve(null);
3631
+ }
3632
+ const response = await this.httpClient.send("/s", {
3633
+ events,
3634
+ userId: this.userId
3635
+ });
3636
+ return response?.sessionId || null;
3577
3637
  }
3578
3638
  async alias(payload) {
3579
3639
  return this.send({
@@ -38367,7 +38427,7 @@ function toCamelCase(str) {
38367
38427
  ($1) => $1.toUpperCase().replace("-", "").replace("_", "")
38368
38428
  );
38369
38429
  }
38370
- const VERSION = "0.0.20";
38430
+ const VERSION = "0.0.21-rc.1";
38371
38431
  class Tracker extends TrackerSdk {
38372
38432
  // 750KB
38373
38433
  constructor(options) {
@@ -38539,14 +38599,12 @@ class Tracker extends TrackerSdk {
38539
38599
  * @param events Array of events to be recorded
38540
38600
  * @returns Promise that resolves when events are recorded and session is managed
38541
38601
  */
38542
- // NB: this responds to the websocket request, payload is the response
38543
38602
  async recordEventsToServer(events) {
38544
- this.record(events, (payload) => {
38545
- this.sessionManager.updateSession(payload.sessionId, {
38546
- takeFullSnapshot: () => {
38547
- this.agent.takeFullSnapshot();
38548
- }
38549
- });
38603
+ const sessionId = await this.record(events);
38604
+ this.sessionManager.updateSession(sessionId, {
38605
+ takeFullSnapshot: () => {
38606
+ this.agent.takeFullSnapshot();
38607
+ }
38550
38608
  });
38551
38609
  }
38552
38610
  sendScreenViewEvent(props) {
Binary file