@statsig/web-analytics 3.5.0 → 3.5.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.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@statsig/web-analytics",
3
- "version": "3.5.0",
3
+ "version": "3.5.1",
4
4
  "dependencies": {
5
- "@statsig/client-core": "3.5.0",
6
- "@statsig/js-client": "3.5.0"
5
+ "@statsig/client-core": "3.5.1",
6
+ "@statsig/js-client": "3.5.1"
7
7
  },
8
8
  "jsdelivr": "./build/statsig-web-analytics.min.js",
9
9
  "type": "commonjs",
@@ -1,9 +1,15 @@
1
1
  import { PrecomputedEvaluationsInterface, StatsigPlugin } from '@statsig/client-core';
2
+ import { AutoCaptureEvent } from './AutoCaptureEvent';
3
+ export type AutoCaptureOptions = {
4
+ eventFilterFunc?: (event: AutoCaptureEvent) => boolean;
5
+ };
2
6
  export declare class StatsigAutoCapturePlugin implements StatsigPlugin<PrecomputedEvaluationsInterface> {
7
+ private _options?;
3
8
  readonly __plugin = "auto-capture";
9
+ constructor(_options?: AutoCaptureOptions | undefined);
4
10
  bind(client: PrecomputedEvaluationsInterface): void;
5
11
  }
6
- export declare function runStatsigAutoCapture(client: PrecomputedEvaluationsInterface): AutoCapture;
12
+ export declare function runStatsigAutoCapture(client: PrecomputedEvaluationsInterface, options?: AutoCaptureOptions): AutoCapture;
7
13
  export declare class AutoCapture {
8
14
  private _client;
9
15
  private _errorBoundary;
@@ -11,7 +17,8 @@ export declare class AutoCapture {
11
17
  private _deepestScroll;
12
18
  private _disabledEvents;
13
19
  private _previousLoggedPageViewUrl;
14
- constructor(_client: PrecomputedEvaluationsInterface);
20
+ private _eventFilterFunc?;
21
+ constructor(_client: PrecomputedEvaluationsInterface, options?: AutoCaptureOptions);
15
22
  private _addEventHandlers;
16
23
  private _addPageViewTracking;
17
24
  private _autoLogEvent;
@@ -2,23 +2,29 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AutoCapture = exports.runStatsigAutoCapture = exports.StatsigAutoCapturePlugin = void 0;
4
4
  const client_core_1 = require("@statsig/client-core");
5
+ const AutoCaptureEvent_1 = require("./AutoCaptureEvent");
5
6
  const Utils_1 = require("./Utils");
6
7
  const payloadUtils_1 = require("./payloadUtils");
8
+ const AUTO_EVENT_MAPPING = {
9
+ submit: AutoCaptureEvent_1.AutoCaptureEventName.FORM_SUBMIT,
10
+ click: AutoCaptureEvent_1.AutoCaptureEventName.CLICK,
11
+ };
7
12
  class StatsigAutoCapturePlugin {
8
- constructor() {
13
+ constructor(_options) {
14
+ this._options = _options;
9
15
  this.__plugin = 'auto-capture';
10
16
  }
11
17
  bind(client) {
12
- runStatsigAutoCapture(client);
18
+ runStatsigAutoCapture(client, this._options);
13
19
  }
14
20
  }
15
21
  exports.StatsigAutoCapturePlugin = StatsigAutoCapturePlugin;
16
- function runStatsigAutoCapture(client) {
17
- return new AutoCapture(client);
22
+ function runStatsigAutoCapture(client, options) {
23
+ return new AutoCapture(client, options);
18
24
  }
19
25
  exports.runStatsigAutoCapture = runStatsigAutoCapture;
20
26
  class AutoCapture {
21
- constructor(_client) {
27
+ constructor(_client, options) {
22
28
  var _a, _b, _c;
23
29
  this._client = _client;
24
30
  this._startTime = Date.now();
@@ -29,6 +35,7 @@ class AutoCapture {
29
35
  this._disabledEvents = (_b = (_a = values === null || values === void 0 ? void 0 : values.auto_capture_settings) === null || _a === void 0 ? void 0 : _a.disabled_events) !== null && _b !== void 0 ? _b : {};
30
36
  this._errorBoundary = errorBoundary;
31
37
  this._errorBoundary.wrap(this);
38
+ this._eventFilterFunc = options === null || options === void 0 ? void 0 : options.eventFilterFunc;
32
39
  const doc = (0, client_core_1._getDocumentSafe)();
33
40
  if (!(0, client_core_1._isServerEnv)()) {
34
41
  __STATSIG__ = (0, client_core_1._getStatsigGlobal)();
@@ -74,7 +81,7 @@ class AutoCapture {
74
81
  }
75
82
  _autoLogEvent(event) {
76
83
  var _a;
77
- let eventType = (_a = event.type) === null || _a === void 0 ? void 0 : _a.toLowerCase();
84
+ const eventType = (_a = event.type) === null || _a === void 0 ? void 0 : _a.toLowerCase();
78
85
  if (eventType === 'error' && event instanceof ErrorEvent) {
79
86
  this._logError(event);
80
87
  return;
@@ -86,11 +93,12 @@ class AutoCapture {
86
93
  if (!(0, Utils_1._shouldLogEvent)(event, target)) {
87
94
  return;
88
95
  }
89
- if (eventType === 'submit') {
90
- eventType = 'form_submit';
96
+ const eventName = AUTO_EVENT_MAPPING[eventType];
97
+ if (!eventName) {
98
+ return;
91
99
  }
92
100
  const { value, metadata } = (0, Utils_1._gatherEventData)(target);
93
- this._enqueueAutoCapture(eventType, value, metadata);
101
+ this._enqueueAutoCapture(eventName, value, metadata);
94
102
  }
95
103
  _initialize() {
96
104
  this._addEventHandlers();
@@ -112,7 +120,7 @@ class AutoCapture {
112
120
  : 'Unknown Error';
113
121
  }
114
122
  }
115
- this._enqueueAutoCapture('error', event.message, {
123
+ this._enqueueAutoCapture(AutoCaptureEvent_1.AutoCaptureEventName.ERROR, event.message, {
116
124
  message: event.message,
117
125
  filename: event.filename,
118
126
  lineno: event.lineno,
@@ -126,7 +134,7 @@ class AutoCapture {
126
134
  if (!this._isNewSession(session)) {
127
135
  return;
128
136
  }
129
- this._enqueueAutoCapture('session_start', (0, Utils_1._getSanitizedPageUrl)(), { sessionID: session.data.sessionID }, { flushImmediately: true });
137
+ this._enqueueAutoCapture(AutoCaptureEvent_1.AutoCaptureEventName.SESSION_START, (0, Utils_1._getSanitizedPageUrl)(), { sessionID: session.data.sessionID }, { flushImmediately: true });
130
138
  }
131
139
  catch (err) {
132
140
  this._errorBoundary.logError('AC::logSession', err);
@@ -140,7 +148,7 @@ class AutoCapture {
140
148
  }
141
149
  this._previousLoggedPageViewUrl = url;
142
150
  const payload = (0, payloadUtils_1._gatherPageViewPayload)(url);
143
- this._enqueueAutoCapture('page_view', (0, Utils_1._getSanitizedPageUrl)(), payload, {
151
+ this._enqueueAutoCapture(AutoCaptureEvent_1.AutoCaptureEventName.PAGE_VIEW, (0, Utils_1._getSanitizedPageUrl)(), payload, {
144
152
  flushImmediately: true,
145
153
  addNewSessionMetadata: true,
146
154
  });
@@ -178,18 +186,19 @@ class AutoCapture {
178
186
  metadata['downlink_kbps'] = networkInfo.downlink;
179
187
  metadata['save_data'] = networkInfo.saveData;
180
188
  }
181
- this._enqueueAutoCapture('performance', (0, Utils_1._getSanitizedPageUrl)(), metadata);
189
+ this._enqueueAutoCapture(AutoCaptureEvent_1.AutoCaptureEventName.PERFORMANCE, (0, Utils_1._getSanitizedPageUrl)(), metadata);
182
190
  }, 1);
183
191
  }
184
192
  _pageUnloadHandler() {
185
- this._enqueueAutoCapture('page_view_end', (0, Utils_1._getSanitizedPageUrl)(), {
193
+ this._enqueueAutoCapture(AutoCaptureEvent_1.AutoCaptureEventName.PAGE_VIEW_END, (0, Utils_1._getSanitizedPageUrl)(), {
186
194
  scrollDepth: this._deepestScroll,
187
195
  pageViewLength: Date.now() - this._startTime,
188
196
  }, { flushImmediately: true });
189
197
  }
190
- _enqueueAutoCapture(name, value, metadata, options) {
198
+ _enqueueAutoCapture(eventName, value, metadata, options) {
191
199
  var _a, _b, _c;
192
- if (this._disabledEvents[name]) {
200
+ const subname = eventName.slice('auto_capture::'.length);
201
+ if (this._disabledEvents[eventName] || this._disabledEvents[subname]) {
193
202
  return;
194
203
  }
195
204
  const session = this._getSessionFromClient();
@@ -199,10 +208,13 @@ class AutoCapture {
199
208
  logMetadata['isNewSession'] = String(this._isNewSession(session));
200
209
  }
201
210
  const event = {
202
- eventName: `auto_capture::${name}`,
211
+ eventName,
203
212
  value,
204
213
  metadata: logMetadata,
205
214
  };
215
+ if (this._eventFilterFunc && !this._eventFilterFunc(event)) {
216
+ return;
217
+ }
206
218
  this._client.logEvent(event);
207
219
  if (options === null || options === void 0 ? void 0 : options.flushImmediately) {
208
220
  this._client.flush().catch((e) => {
@@ -0,0 +1,14 @@
1
+ import { StatsigEvent } from '@statsig/client-core';
2
+ export declare const AutoCaptureEventName: {
3
+ readonly PAGE_VIEW: "auto_capture::page_view";
4
+ readonly PAGE_VIEW_END: "auto_capture::page_view_end";
5
+ readonly ERROR: "auto_capture::error";
6
+ readonly SESSION_START: "auto_capture::session_start";
7
+ readonly PERFORMANCE: "auto_capture::performance";
8
+ readonly FORM_SUBMIT: "auto_capture::form_submit";
9
+ readonly CLICK: "auto_capture::click";
10
+ };
11
+ export type AutoCaptureEventName = (typeof AutoCaptureEventName)[keyof typeof AutoCaptureEventName] & string;
12
+ export type AutoCaptureEvent = StatsigEvent & {
13
+ eventName: AutoCaptureEventName;
14
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AutoCaptureEventName = void 0;
4
+ exports.AutoCaptureEventName = {
5
+ PAGE_VIEW: 'auto_capture::page_view',
6
+ PAGE_VIEW_END: 'auto_capture::page_view_end',
7
+ ERROR: 'auto_capture::error',
8
+ SESSION_START: 'auto_capture::session_start',
9
+ PERFORMANCE: 'auto_capture::performance',
10
+ FORM_SUBMIT: 'auto_capture::form_submit',
11
+ CLICK: 'auto_capture::click',
12
+ };