@devskin/browser-sdk 1.0.40 → 1.0.41

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.
@@ -13878,6 +13878,7 @@ class DevSkinSDK {
13878
13878
  this.anonymousId = null;
13879
13879
  this.sessionStartTime = 0;
13880
13880
  this.initialized = false;
13881
+ this.initializing = false;
13881
13882
  this.heartbeatInterval = null;
13882
13883
  // Collectors
13883
13884
  this.deviceCollector = null;
@@ -13893,12 +13894,15 @@ class DevSkinSDK {
13893
13894
  }
13894
13895
  /**
13895
13896
  * Initialize the DevSkin SDK
13897
+ * Uses requestIdleCallback to defer heavy initialization without blocking the page
13896
13898
  */
13897
13899
  init(config) {
13898
- if (this.initialized) {
13899
- console.warn('[DevSkin] SDK already initialized');
13900
+ if (this.initialized || this.initializing) {
13901
+ console.warn('[DevSkin] SDK already initialized or initializing');
13900
13902
  return;
13901
13903
  }
13904
+ // Mark as initializing to prevent duplicate init() calls
13905
+ this.initializing = true;
13902
13906
  this.config = Object.assign({ debug: false, captureWebVitals: true, captureNetworkRequests: true, captureErrors: true, captureUserAgent: true, captureLocation: true, captureDevice: true, heatmapOptions: {
13903
13907
  enabled: true,
13904
13908
  trackClicks: true,
@@ -13908,99 +13912,118 @@ class DevSkinSDK {
13908
13912
  if (this.config.debug) {
13909
13913
  console.log('[DevSkin] Initializing SDK with config:', this.config);
13910
13914
  }
13911
- // Initialize transport
13915
+ // Initialize lightweight components immediately (needed for session context)
13912
13916
  this.transport = new Transport(this.config);
13913
- // Generate anonymous ID if not exists
13914
13917
  this.anonymousId = this.getOrCreateAnonymousId();
13915
- // Initialize collectors BEFORE starting session (so getContextData works)
13916
13918
  this.deviceCollector = new DeviceCollector(this.config);
13917
13919
  this.locationCollector = new LocationCollector(this.config);
13918
13920
  this.browserCollector = new BrowserCollector(this.config);
13919
- // Start session (will now include device/browser/location data)
13920
- // Wait for session creation to complete before starting collectors
13921
- this.startSession().then(() => {
13922
- // Session created, now safe to start collectors that send data
13923
- var _a, _b;
13924
- if (this.config.captureWebVitals) {
13925
- this.performanceCollector = new PerformanceCollector(this.config, this.transport);
13926
- this.performanceCollector.start();
13927
- }
13928
- if (this.config.captureErrors) {
13929
- this.errorCollector = new ErrorCollector(this.config, this.transport);
13930
- this.errorCollector.start();
13931
- }
13932
- if (this.config.captureNetworkRequests) {
13933
- this.networkCollector = new NetworkCollector(this.config, this.transport);
13934
- this.networkCollector.start();
13935
- }
13936
- // Initialize heatmap collector - SEMPRE habilitado
13937
- // Merge default heatmap config with user config
13938
- const heatmapConfig = Object.assign({ enabled: true, trackClicks: true, trackScroll: true, trackMouseMovement: true, mouseMoveSampling: 0.1 }, this.config.heatmapOptions);
13939
- this.config.heatmapOptions = heatmapConfig;
13940
- this.heatmapCollector = new HeatmapCollector(this.config, this.transport, this.anonymousId, this.sessionId);
13941
- this.heatmapCollector.start();
13942
- // Initialize screenshot collector and capture page
13943
- this.screenshotCollector = new ScreenshotCollector(this.config, this.transport);
13944
- this.screenshotCollector.captureAndSend(this.sessionId, window.location.href);
13945
- if (this.config.debug) {
13946
- console.log('[DevSkin] Heatmap collection enabled (always on)');
13947
- }
13948
- // Initialize session recording with rrweb
13949
- if ((_a = this.config.sessionRecording) === null || _a === void 0 ? void 0 : _a.enabled) {
13950
- // Use RRWebRecorder for complete DOM recording
13951
- // Pass sessionStartTime to ensure timestamp continuity across page navigations
13952
- this.rrwebRecorder = new RRWebRecorder(this.sessionId, {
13953
- enabled: true,
13954
- sampleRate: this.config.sessionRecording.sampling || 0.5,
13955
- blockClass: 'rr-block',
13956
- ignoreClass: this.config.sessionRecording.ignoreClass || 'rr-ignore',
13957
- maskAllInputs: this.config.sessionRecording.maskAllInputs !== undefined
13958
- ? this.config.sessionRecording.maskAllInputs
13959
- : true,
13960
- maskInputOptions: {
13961
- password: true,
13962
- email: true,
13963
- tel: true,
13964
- },
13965
- recordCanvas: this.config.sessionRecording.recordCanvas || false,
13966
- collectFonts: true,
13967
- inlineStylesheet: true,
13968
- checkoutEveryNms: 5 * 60 * 1000, // Every 5 minutes
13969
- checkoutEveryNth: 200, // Every 200 events
13970
- }, (events) => {
13971
- var _a;
13972
- // Send rrweb events to backend
13973
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.sendRecordingEvents(this.sessionId, events);
13974
- }, this.sessionStartTime // Pass session start time for timestamp continuity
13975
- );
13976
- // Start recording immediately (session already created)
13977
- this.rrwebRecorder.start();
13978
- if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.debug) {
13979
- console.log('[DevSkin] RRWeb recording started for session:', this.sessionId);
13921
+ // Defer heavy initialization to avoid blocking page load/rendering
13922
+ const initHeavyCollectors = () => {
13923
+ // Start session (will now include device/browser/location data)
13924
+ // Wait for session creation to complete before starting collectors
13925
+ this.startSession().then(() => {
13926
+ // Session created, now safe to start collectors that send data
13927
+ var _a, _b;
13928
+ if (this.config.captureWebVitals) {
13929
+ this.performanceCollector = new PerformanceCollector(this.config, this.transport);
13930
+ this.performanceCollector.start();
13931
+ }
13932
+ if (this.config.captureErrors) {
13933
+ this.errorCollector = new ErrorCollector(this.config, this.transport);
13934
+ this.errorCollector.start();
13935
+ }
13936
+ if (this.config.captureNetworkRequests) {
13937
+ this.networkCollector = new NetworkCollector(this.config, this.transport);
13938
+ this.networkCollector.start();
13939
+ }
13940
+ // Initialize heatmap collector - SEMPRE habilitado
13941
+ // Merge default heatmap config with user config
13942
+ const heatmapConfig = Object.assign({ enabled: true, trackClicks: true, trackScroll: true, trackMouseMovement: true, mouseMoveSampling: 0.1 }, this.config.heatmapOptions);
13943
+ this.config.heatmapOptions = heatmapConfig;
13944
+ this.heatmapCollector = new HeatmapCollector(this.config, this.transport, this.anonymousId, this.sessionId);
13945
+ this.heatmapCollector.start();
13946
+ // Initialize screenshot collector and capture page
13947
+ this.screenshotCollector = new ScreenshotCollector(this.config, this.transport);
13948
+ this.screenshotCollector.captureAndSend(this.sessionId, window.location.href);
13949
+ if (this.config.debug) {
13950
+ console.log('[DevSkin] Heatmap collection enabled (always on)');
13951
+ }
13952
+ // Initialize session recording with rrweb
13953
+ if ((_a = this.config.sessionRecording) === null || _a === void 0 ? void 0 : _a.enabled) {
13954
+ // Use RRWebRecorder for complete DOM recording
13955
+ // Pass sessionStartTime to ensure timestamp continuity across page navigations
13956
+ this.rrwebRecorder = new RRWebRecorder(this.sessionId, {
13957
+ enabled: true,
13958
+ sampleRate: this.config.sessionRecording.sampling || 0.5,
13959
+ blockClass: 'rr-block',
13960
+ ignoreClass: this.config.sessionRecording.ignoreClass || 'rr-ignore',
13961
+ maskAllInputs: this.config.sessionRecording.maskAllInputs !== undefined
13962
+ ? this.config.sessionRecording.maskAllInputs
13963
+ : true,
13964
+ maskInputOptions: {
13965
+ password: true,
13966
+ email: true,
13967
+ tel: true,
13968
+ },
13969
+ recordCanvas: this.config.sessionRecording.recordCanvas || false,
13970
+ collectFonts: true,
13971
+ inlineStylesheet: true,
13972
+ checkoutEveryNms: 5 * 60 * 1000, // Every 5 minutes
13973
+ checkoutEveryNth: 200, // Every 200 events
13974
+ }, (events) => {
13975
+ var _a;
13976
+ // Send rrweb events to backend
13977
+ (_a = this.transport) === null || _a === void 0 ? void 0 : _a.sendRecordingEvents(this.sessionId, events);
13978
+ }, this.sessionStartTime // Pass session start time for timestamp continuity
13979
+ );
13980
+ // Start recording immediately (session already created)
13981
+ this.rrwebRecorder.start();
13982
+ if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.debug) {
13983
+ console.log('[DevSkin] RRWeb recording started for session:', this.sessionId);
13984
+ }
13980
13985
  }
13986
+ // Track initial page view
13987
+ this.trackPageView();
13988
+ // Start heartbeat to update session duration every 30 seconds
13989
+ this.startHeartbeat();
13990
+ }).catch((err) => {
13991
+ console.error('[DevSkin] Failed to create session:', err);
13992
+ });
13993
+ // Track page visibility changes
13994
+ this.setupVisibilityTracking();
13995
+ // Track page unload
13996
+ this.setupUnloadTracking();
13997
+ // Mark as fully initialized only after everything is loaded
13998
+ this.initialized = true;
13999
+ this.initializing = false;
14000
+ };
14001
+ // Use requestIdleCallback to defer heavy initialization (non-blocking)
14002
+ // Falls back to setTimeout for browsers that don't support it
14003
+ if (typeof window !== 'undefined') {
14004
+ if ('requestIdleCallback' in window) {
14005
+ window.requestIdleCallback(initHeavyCollectors, { timeout: 2000 });
13981
14006
  }
13982
- // Track initial page view
13983
- this.trackPageView();
13984
- // Start heartbeat to update session duration every 30 seconds
13985
- this.startHeartbeat();
13986
- }).catch((err) => {
13987
- console.error('[DevSkin] Failed to create session:', err);
13988
- });
13989
- // Track page visibility changes
13990
- this.setupVisibilityTracking();
13991
- // Track page unload
13992
- this.setupUnloadTracking();
13993
- this.initialized = true;
14007
+ else {
14008
+ // Fallback for older browsers
14009
+ setTimeout(initHeavyCollectors, 1);
14010
+ }
14011
+ }
14012
+ else {
14013
+ // Node.js environment (SSR)
14014
+ initHeavyCollectors();
14015
+ }
13994
14016
  if (this.config.debug) {
13995
- console.log('[DevSkin] SDK initialized successfully');
14017
+ console.log('[DevSkin] SDK initialization started (heavy collectors loading in background)');
13996
14018
  }
13997
14019
  }
13998
14020
  /**
13999
14021
  * Track a custom event
14022
+ * Works immediately after init() even if heavy collectors are still loading
14000
14023
  */
14001
14024
  track(eventName, properties) {
14002
- var _a, _b;
14003
- if (!this.initialized) {
14025
+ var _a, _b, _c, _d;
14026
+ if (!this.transport) {
14004
14027
  console.warn('[DevSkin] SDK not initialized. Call init() first.');
14005
14028
  return;
14006
14029
  }
@@ -14008,15 +14031,15 @@ class DevSkinSDK {
14008
14031
  eventName: eventName,
14009
14032
  eventType: 'track',
14010
14033
  timestamp: new Date().toISOString(),
14011
- sessionId: this.sessionId,
14012
- userId: this.userId || undefined,
14013
- anonymousId: this.anonymousId || undefined,
14034
+ sessionId: (_a = this.sessionId) !== null && _a !== void 0 ? _a : undefined,
14035
+ userId: (_b = this.userId) !== null && _b !== void 0 ? _b : undefined,
14036
+ anonymousId: (_c = this.anonymousId) !== null && _c !== void 0 ? _c : undefined,
14014
14037
  properties: Object.assign(Object.assign({}, properties), this.getContextData()),
14015
14038
  pageUrl: window.location.href,
14016
14039
  pageTitle: document.title,
14017
14040
  };
14018
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.sendEvent(eventData);
14019
- if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.debug) {
14041
+ this.transport.sendEvent(eventData);
14042
+ if ((_d = this.config) === null || _d === void 0 ? void 0 : _d.debug) {
14020
14043
  console.log('[DevSkin] Event tracked:', eventData);
14021
14044
  }
14022
14045
  }
@@ -14045,23 +14068,24 @@ class DevSkinSDK {
14045
14068
  }
14046
14069
  /**
14047
14070
  * Identify a user
14071
+ * Works immediately after init() even if heavy collectors are still loading
14048
14072
  */
14049
14073
  identify(userId, traits) {
14050
- var _a, _b;
14051
- if (!this.initialized) {
14074
+ var _a, _b, _c;
14075
+ if (!this.transport) {
14052
14076
  console.warn('[DevSkin] SDK not initialized. Call init() first.');
14053
14077
  return;
14054
14078
  }
14055
14079
  this.userId = userId;
14056
14080
  const userData = {
14057
14081
  userId: userId,
14058
- anonymousId: this.anonymousId || undefined,
14082
+ anonymousId: (_a = this.anonymousId) !== null && _a !== void 0 ? _a : undefined,
14059
14083
  traits: Object.assign(Object.assign({}, traits), this.getContextData()),
14060
- sessionId: this.sessionId,
14084
+ sessionId: (_b = this.sessionId) !== null && _b !== void 0 ? _b : undefined,
14061
14085
  timestamp: new Date().toISOString(),
14062
14086
  };
14063
- (_a = this.transport) === null || _a === void 0 ? void 0 : _a.identifyUser(userData);
14064
- if ((_b = this.config) === null || _b === void 0 ? void 0 : _b.debug) {
14087
+ this.transport.identifyUser(userData);
14088
+ if ((_c = this.config) === null || _c === void 0 ? void 0 : _c.debug) {
14065
14089
  console.log('[DevSkin] User identified:', userData);
14066
14090
  }
14067
14091
  }