@zaplier/sdk 1.2.7 → 1.3.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.cjs CHANGED
@@ -7285,37 +7285,28 @@ class SessionReplayEngine {
7285
7285
  this.sendToBackend(payload);
7286
7286
  }
7287
7287
  /**
7288
- * Set anti-adblock manager for WebRTC transport
7288
+ * Set SDK instance for transport
7289
7289
  */
7290
- setAntiAdblockManager(manager) {
7291
- this.antiAdblockManager = manager;
7290
+ setSDKInstance(sdk) {
7291
+ this.sdkInstance = sdk;
7292
7292
  }
7293
7293
  /**
7294
- * Send data via WebRTC (anti-adblock) or fallback to standard fetch
7294
+ * Send replay batch via SDK's transport system
7295
7295
  */
7296
7296
  async sendToBackend(payload) {
7297
7297
  try {
7298
- // Try WebRTC first if anti-adblock manager is available
7299
- if (this.antiAdblockManager) {
7300
- console.log("[Zaplier] Trying WebRTC transport...");
7301
- const result = await this.antiAdblockManager.send(payload, "/replays/record");
7302
- if (result.success) {
7303
- console.log("[Zaplier] Session replay sent via WebRTC");
7304
- return; // Success via WebRTC
7305
- }
7306
- console.log("[Zaplier] WebRTC failed, falling back to fetch");
7298
+ if (!this.sdkInstance) {
7299
+ console.error("[Zaplier] No SDK instance available for replay transport");
7300
+ return;
7307
7301
  }
7308
- // Get API endpoint from global config
7309
- const apiEndpoint = window.zaplier_config?.apiEndpoint || "http://localhost:3001";
7310
- const token = window.zaplier_config?.token || "ws_demo_token";
7311
- const url = `${apiEndpoint}/replays/record?token=${token}`;
7312
- console.log(`[Zaplier] Sending replay batch to ${url}`);
7302
+ console.log(`[Zaplier] Sending replay batch via SDK transport`);
7313
7303
  console.log(`[Zaplier] Payload:`, {
7314
7304
  sessionId: payload.sessionId,
7315
7305
  eventCount: payload.events.length,
7316
7306
  hasMetadata: !!payload.metadata,
7317
7307
  });
7318
- const requestBody = {
7308
+ // Use SDK's sendReplayBatch method
7309
+ const result = await this.sdkInstance.sendReplayBatch({
7319
7310
  sessionId: payload.sessionId,
7320
7311
  events: payload.events,
7321
7312
  metadata: {
@@ -7325,23 +7316,12 @@ class SessionReplayEngine {
7325
7316
  hasConversion: false,
7326
7317
  ...payload.metadata,
7327
7318
  },
7328
- };
7329
- // Fallback to standard fetch
7330
- const response = await fetch(url, {
7331
- method: "POST",
7332
- headers: { "Content-Type": "application/json" },
7333
- body: JSON.stringify(requestBody),
7334
7319
  });
7335
- const responseText = await response.text();
7336
- if (!response.ok) {
7337
- console.error("[Zaplier] Session replay batch failed:", {
7338
- status: response.status,
7339
- statusText: response.statusText,
7340
- response: responseText,
7341
- });
7320
+ if (result && result.success) {
7321
+ console.log("[Zaplier] Session replay batch sent successfully");
7342
7322
  }
7343
7323
  else {
7344
- console.log("[Zaplier] Session replay batch sent successfully:", responseText);
7324
+ console.error("[Zaplier] Session replay batch failed:", result);
7345
7325
  }
7346
7326
  }
7347
7327
  catch (error) {
@@ -7393,7 +7373,7 @@ const DEFAULT_CONFIG = {
7393
7373
  */
7394
7374
  class ZaplierSDK {
7395
7375
  constructor(userConfig) {
7396
- this.version = "3.0.0";
7376
+ this.version = "1.3.0";
7397
7377
  this.isInitialized = false;
7398
7378
  this.eventQueue = [];
7399
7379
  /**
@@ -7471,7 +7451,7 @@ class ZaplierSDK {
7471
7451
  });
7472
7452
  // Connect to anti-adblock manager
7473
7453
  if (this.antiAdblockManager) {
7474
- this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7454
+ this.replayEngine.setSDKInstance(this);
7475
7455
  }
7476
7456
  const started = this.replayEngine.start();
7477
7457
  if (this.config.debug) {
@@ -7482,10 +7462,35 @@ class ZaplierSDK {
7482
7462
  }
7483
7463
  }
7484
7464
  else if (this.replayEngine) {
7485
- // If engine already exists, try to start it (might have failed due to sampling before)
7486
- const started = this.replayEngine.start();
7487
- if (this.config.debug) {
7488
- console.log("[Zaplier] Replay engine already exists, attempting start", { started });
7465
+ // If engine already exists but isn't recording, recreate it with 100% sample rate
7466
+ if (!this.replayEngine.isRecording()) {
7467
+ // Stop and recreate with 100% sample rate
7468
+ this.replayEngine.stop();
7469
+ this.replayEngine = undefined;
7470
+ // Recreate with 100% sample rate
7471
+ if (!this.sessionId) {
7472
+ this.sessionId = this.generateSessionId();
7473
+ }
7474
+ const sessionId = this.sessionId;
7475
+ this.replayEngine = new SessionReplayEngine(sessionId, {
7476
+ sampleRate: 1.0, // Force 100% when explicitly enabled
7477
+ maskSensitiveFields: this.config.replayMaskInputs,
7478
+ compressionEnabled: true,
7479
+ });
7480
+ // Connect to anti-adblock manager
7481
+ if (this.antiAdblockManager) {
7482
+ this.replayEngine.setSDKInstance(this);
7483
+ }
7484
+ const started = this.replayEngine.start();
7485
+ if (this.config.debug) {
7486
+ console.log("[Zaplier] Replay engine recreated with 100% sample rate", { started, isRecording: this.replayEngine.isRecording() });
7487
+ }
7488
+ }
7489
+ else {
7490
+ // Already recording, just log
7491
+ if (this.config.debug) {
7492
+ console.log("[Zaplier] Replay already recording");
7493
+ }
7489
7494
  }
7490
7495
  }
7491
7496
  if (this.config.debug && !this.replayEngine) {
@@ -7516,7 +7521,7 @@ class ZaplierSDK {
7516
7521
  });
7517
7522
  // Connect to anti-adblock manager
7518
7523
  if (this.antiAdblockManager) {
7519
- this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7524
+ this.replayEngine.setSDKInstance(this);
7520
7525
  }
7521
7526
  return this.replayEngine.start();
7522
7527
  }
@@ -7620,10 +7625,8 @@ class ZaplierSDK {
7620
7625
  maskSensitiveFields: this.config.replayMaskInputs,
7621
7626
  compressionEnabled: true,
7622
7627
  });
7623
- // Connect to anti-adblock manager for WebRTC transport
7624
- if (this.antiAdblockManager) {
7625
- this.replayEngine.setAntiAdblockManager(this.antiAdblockManager);
7626
- }
7628
+ // Connect SDK instance for transport
7629
+ this.replayEngine.setSDKInstance(this);
7627
7630
  const started = this.replayEngine.start();
7628
7631
  if (this.config.debug) {
7629
7632
  console.log("[Zaplier] Session Replay started", {
@@ -7909,6 +7912,46 @@ class ZaplierSDK {
7909
7912
  }
7910
7913
  }
7911
7914
  }
7915
+ /**
7916
+ * Send session replay batch to backend
7917
+ */
7918
+ async sendReplayBatch(replayData) {
7919
+ if (!this.isInitialized) {
7920
+ console.error("[Zaplier] SDK not initialized for replay batch sending");
7921
+ return { success: false, error: "SDK not initialized" };
7922
+ }
7923
+ try {
7924
+ const payload = {
7925
+ sessionId: replayData.sessionId,
7926
+ userId: this.backendVisitorId,
7927
+ events: replayData.events,
7928
+ metadata: {
7929
+ ...replayData.metadata,
7930
+ visitorId: this.backendVisitorId,
7931
+ fingerprintHash: this.fingerprint?.hash,
7932
+ },
7933
+ };
7934
+ if (this.config.debug) {
7935
+ console.log("[Zaplier] Sending replay batch:", {
7936
+ sessionId: payload.sessionId,
7937
+ eventCount: payload.events.length,
7938
+ hasMetadata: !!payload.metadata,
7939
+ });
7940
+ }
7941
+ // Use anti-adblock transport or standard HTTP
7942
+ const result = await this.makeRequest(`/replays/record?token=${encodeURIComponent(this.config.token)}`, payload, "POST");
7943
+ if (this.config.debug) {
7944
+ console.log("[Zaplier] Replay batch result:", result);
7945
+ }
7946
+ return result;
7947
+ }
7948
+ catch (error) {
7949
+ if (this.config.debug) {
7950
+ console.error("[Zaplier] Replay batch sending failed:", error);
7951
+ }
7952
+ return { success: false, error: error instanceof Error ? error.message : "Unknown error" };
7953
+ }
7954
+ }
7912
7955
  /**
7913
7956
  * Make HTTP request to backend using anti-adblock system
7914
7957
  */