userlens-analytics-sdk 0.1.9 → 0.1.10

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.
@@ -21312,11 +21312,11 @@ class SessionRecorder {
21312
21312
  constructor({
21313
21313
  WRITE_CODE,
21314
21314
  userId,
21315
- TIMEOUT = 10 * 60 * 1000,
21315
+ TIMEOUT = 30 * 60 * 1000,
21316
21316
  BUFFER_SIZE = 50,
21317
21317
  maskingOptions = ["passwords"], // "passwords", "all"
21318
21318
  }) {
21319
- if (window === undefined) {
21319
+ if (typeof window === "undefined") {
21320
21320
  console.error(
21321
21321
  "Userlens EventCollector error: unavailable outside of browser environment."
21322
21322
  );
@@ -21375,7 +21375,11 @@ class SessionRecorder {
21375
21375
 
21376
21376
  // init rrweb recorder
21377
21377
  #initRecorder() {
21378
- record({
21378
+ if (this.stopRecording) {
21379
+ this.stopRecording();
21380
+ }
21381
+
21382
+ this.stopRecording = record({
21379
21383
  emit: (event) => {
21380
21384
  this.#handleEvent(event);
21381
21385
  },
@@ -21386,8 +21390,19 @@ class SessionRecorder {
21386
21390
  }
21387
21391
 
21388
21392
  #handleEvent(event) {
21393
+ const lastActive = Number(
21394
+ window.localStorage.getItem("userlensSessionLastActive")
21395
+ );
21396
+
21397
+ if (lastActive) {
21398
+ const now = Date.now();
21399
+
21400
+ if (now - lastActive > this.TIMEOUT) {
21401
+ this.#handleInactivity();
21402
+ }
21403
+ }
21404
+
21389
21405
  this.sessionEvents.push(event);
21390
- // update last active in storage
21391
21406
  window.localStorage.setItem("userlensSessionLastActive", event.timestamp);
21392
21407
 
21393
21408
  if (this.sessionEvents.length >= this.BUFFER_SIZE) {
@@ -21395,6 +21410,22 @@ class SessionRecorder {
21395
21410
  }
21396
21411
  }
21397
21412
 
21413
+ #handleInactivity() {
21414
+ if (this.sessionEvents.length > 0) {
21415
+ this.#trackEvents();
21416
+ }
21417
+
21418
+ if (this.stopRecording) {
21419
+ this.stopRecording();
21420
+ }
21421
+
21422
+ localStorage.removeItem("userlensSessionUuid");
21423
+ localStorage.removeItem("userlensSessionLastActive");
21424
+
21425
+ this.#createSession();
21426
+ this.#initRecorder();
21427
+ }
21428
+
21398
21429
  #initUnloadListener() {
21399
21430
  window.addEventListener("beforeunload", () => {
21400
21431
  // save events on session.userlens.io service
@@ -21410,7 +21441,6 @@ class SessionRecorder {
21410
21441
 
21411
21442
  this.#clearEvents();
21412
21443
 
21413
- // add try/retry ?
21414
21444
  await fetch(`https://sessions.userlens.io/session/${this.sessionUuid}`, {
21415
21445
  method: "POST",
21416
21446
  headers: {
@@ -21308,11 +21308,11 @@ class SessionRecorder {
21308
21308
  constructor({
21309
21309
  WRITE_CODE,
21310
21310
  userId,
21311
- TIMEOUT = 10 * 60 * 1000,
21311
+ TIMEOUT = 30 * 60 * 1000,
21312
21312
  BUFFER_SIZE = 50,
21313
21313
  maskingOptions = ["passwords"], // "passwords", "all"
21314
21314
  }) {
21315
- if (window === undefined) {
21315
+ if (typeof window === "undefined") {
21316
21316
  console.error(
21317
21317
  "Userlens EventCollector error: unavailable outside of browser environment."
21318
21318
  );
@@ -21371,7 +21371,11 @@ class SessionRecorder {
21371
21371
 
21372
21372
  // init rrweb recorder
21373
21373
  #initRecorder() {
21374
- record({
21374
+ if (this.stopRecording) {
21375
+ this.stopRecording();
21376
+ }
21377
+
21378
+ this.stopRecording = record({
21375
21379
  emit: (event) => {
21376
21380
  this.#handleEvent(event);
21377
21381
  },
@@ -21382,8 +21386,19 @@ class SessionRecorder {
21382
21386
  }
21383
21387
 
21384
21388
  #handleEvent(event) {
21389
+ const lastActive = Number(
21390
+ window.localStorage.getItem("userlensSessionLastActive")
21391
+ );
21392
+
21393
+ if (lastActive) {
21394
+ const now = Date.now();
21395
+
21396
+ if (now - lastActive > this.TIMEOUT) {
21397
+ this.#handleInactivity();
21398
+ }
21399
+ }
21400
+
21385
21401
  this.sessionEvents.push(event);
21386
- // update last active in storage
21387
21402
  window.localStorage.setItem("userlensSessionLastActive", event.timestamp);
21388
21403
 
21389
21404
  if (this.sessionEvents.length >= this.BUFFER_SIZE) {
@@ -21391,6 +21406,22 @@ class SessionRecorder {
21391
21406
  }
21392
21407
  }
21393
21408
 
21409
+ #handleInactivity() {
21410
+ if (this.sessionEvents.length > 0) {
21411
+ this.#trackEvents();
21412
+ }
21413
+
21414
+ if (this.stopRecording) {
21415
+ this.stopRecording();
21416
+ }
21417
+
21418
+ localStorage.removeItem("userlensSessionUuid");
21419
+ localStorage.removeItem("userlensSessionLastActive");
21420
+
21421
+ this.#createSession();
21422
+ this.#initRecorder();
21423
+ }
21424
+
21394
21425
  #initUnloadListener() {
21395
21426
  window.addEventListener("beforeunload", () => {
21396
21427
  // save events on session.userlens.io service
@@ -21406,7 +21437,6 @@ class SessionRecorder {
21406
21437
 
21407
21438
  this.#clearEvents();
21408
21439
 
21409
- // add try/retry ?
21410
21440
  await fetch(`https://sessions.userlens.io/session/${this.sessionUuid}`, {
21411
21441
  method: "POST",
21412
21442
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "userlens-analytics-sdk",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "main": "dist/userlens.cjs.js",
5
5
  "module": "dist/userlens.esm.js",
6
6
  "scripts": {
@@ -5,11 +5,11 @@ export default class SessionRecorder {
5
5
  constructor({
6
6
  WRITE_CODE,
7
7
  userId,
8
- TIMEOUT = 10 * 60 * 1000,
8
+ TIMEOUT = 30 * 60 * 1000,
9
9
  BUFFER_SIZE = 50,
10
10
  maskingOptions = ["passwords"], // "passwords", "all"
11
11
  }) {
12
- if (window === undefined) {
12
+ if (typeof window === "undefined") {
13
13
  console.error(
14
14
  "Userlens EventCollector error: unavailable outside of browser environment."
15
15
  );
@@ -68,7 +68,11 @@ export default class SessionRecorder {
68
68
 
69
69
  // init rrweb recorder
70
70
  #initRecorder() {
71
- rrwebRecord({
71
+ if (this.stopRecording) {
72
+ this.stopRecording();
73
+ }
74
+
75
+ this.stopRecording = rrwebRecord({
72
76
  emit: (event) => {
73
77
  this.#handleEvent(event);
74
78
  },
@@ -79,8 +83,19 @@ export default class SessionRecorder {
79
83
  }
80
84
 
81
85
  #handleEvent(event) {
86
+ const lastActive = Number(
87
+ window.localStorage.getItem("userlensSessionLastActive")
88
+ );
89
+
90
+ if (lastActive) {
91
+ const now = Date.now();
92
+
93
+ if (now - lastActive > this.TIMEOUT) {
94
+ this.#handleInactivity();
95
+ }
96
+ }
97
+
82
98
  this.sessionEvents.push(event);
83
- // update last active in storage
84
99
  window.localStorage.setItem("userlensSessionLastActive", event.timestamp);
85
100
 
86
101
  if (this.sessionEvents.length >= this.BUFFER_SIZE) {
@@ -88,6 +103,22 @@ export default class SessionRecorder {
88
103
  }
89
104
  }
90
105
 
106
+ #handleInactivity() {
107
+ if (this.sessionEvents.length > 0) {
108
+ this.#trackEvents();
109
+ }
110
+
111
+ if (this.stopRecording) {
112
+ this.stopRecording();
113
+ }
114
+
115
+ localStorage.removeItem("userlensSessionUuid");
116
+ localStorage.removeItem("userlensSessionLastActive");
117
+
118
+ this.#createSession();
119
+ this.#initRecorder();
120
+ }
121
+
91
122
  #initUnloadListener() {
92
123
  window.addEventListener("beforeunload", () => {
93
124
  // save events on session.userlens.io service
@@ -103,7 +134,6 @@ export default class SessionRecorder {
103
134
 
104
135
  this.#clearEvents();
105
136
 
106
- // add try/retry ?
107
137
  await fetch(`https://sessions.userlens.io/session/${this.sessionUuid}`, {
108
138
  method: "POST",
109
139
  headers: {