@statsig/web-analytics 3.0.0 → 3.2.0-beta.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.0.0",
3
+ "version": "3.2.0-beta.1",
4
4
  "dependencies": {
5
- "@statsig/client-core": "3.0.0",
6
- "@statsig/js-client": "3.0.0"
5
+ "@statsig/client-core": "3.2.0-beta.1",
6
+ "@statsig/js-client": "3.2.0-beta.1"
7
7
  },
8
8
  "jsdelivr": "./build/statsig-web-analytics.min.js",
9
9
  "type": "commonjs",
@@ -1,4 +1,8 @@
1
- import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
1
+ import { PrecomputedEvaluationsInterface, StatsigPlugin } from '@statsig/client-core';
2
+ export declare class StatsigAutoCapturePlugin implements StatsigPlugin<PrecomputedEvaluationsInterface> {
3
+ readonly __plugin = "auto-capture";
4
+ bind(client: PrecomputedEvaluationsInterface): void;
5
+ }
2
6
  export declare function runStatsigAutoCapture(client: PrecomputedEvaluationsInterface): AutoCapture;
3
7
  export declare class AutoCapture {
4
8
  private _client;
@@ -6,13 +10,15 @@ export declare class AutoCapture {
6
10
  private _startTime;
7
11
  private _deepestScroll;
8
12
  private _disabledEvents;
13
+ private _previousLoggedPageViewUrl;
9
14
  constructor(_client: PrecomputedEvaluationsInterface);
10
15
  private _addEventHandlers;
16
+ private _addPageViewTracking;
11
17
  private _autoLogEvent;
12
18
  private _initialize;
13
19
  private _logError;
14
20
  private _logSessionStart;
15
- private _logPageView;
21
+ private _tryLogPageView;
16
22
  private _logPerformance;
17
23
  private _pageUnloadHandler;
18
24
  private _enqueueAutoCapture;
@@ -1,9 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AutoCapture = exports.runStatsigAutoCapture = void 0;
3
+ exports.AutoCapture = exports.runStatsigAutoCapture = exports.StatsigAutoCapturePlugin = void 0;
4
4
  const client_core_1 = require("@statsig/client-core");
5
5
  const Utils_1 = require("./Utils");
6
6
  const payloadUtils_1 = require("./payloadUtils");
7
+ class StatsigAutoCapturePlugin {
8
+ constructor() {
9
+ this.__plugin = 'auto-capture';
10
+ }
11
+ bind(client) {
12
+ runStatsigAutoCapture(client);
13
+ }
14
+ }
15
+ exports.StatsigAutoCapturePlugin = StatsigAutoCapturePlugin;
7
16
  function runStatsigAutoCapture(client) {
8
17
  return new AutoCapture(client);
9
18
  }
@@ -15,6 +24,7 @@ class AutoCapture {
15
24
  this._startTime = Date.now();
16
25
  this._deepestScroll = 0;
17
26
  this._disabledEvents = {};
27
+ this._previousLoggedPageViewUrl = null;
18
28
  const { sdkKey, errorBoundary, values } = _client.getContext();
19
29
  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 : {};
20
30
  this._errorBoundary = errorBoundary;
@@ -47,6 +57,21 @@ class AutoCapture {
47
57
  (0, Utils_1._registerEventHandler)(win, 'beforeunload', () => this._pageUnloadHandler());
48
58
  (0, Utils_1._registerEventHandler)(win, 'scroll', () => this._scrollEventHandler());
49
59
  }
60
+ _addPageViewTracking() {
61
+ const win = (0, client_core_1._getWindowSafe)();
62
+ const doc = (0, client_core_1._getDocumentSafe)();
63
+ if (!win || !doc) {
64
+ return;
65
+ }
66
+ (0, Utils_1._registerEventHandler)(win, 'popstate', () => this._tryLogPageView());
67
+ window.history.pushState = new Proxy(window.history.pushState, {
68
+ apply: (target, thisArg, [state, unused, url]) => {
69
+ target.apply(thisArg, [state, unused, url]);
70
+ this._tryLogPageView();
71
+ },
72
+ });
73
+ this._tryLogPageView();
74
+ }
50
75
  _autoLogEvent(event) {
51
76
  var _a;
52
77
  let eventType = (_a = event.type) === null || _a === void 0 ? void 0 : _a.toLowerCase();
@@ -69,8 +94,8 @@ class AutoCapture {
69
94
  }
70
95
  _initialize() {
71
96
  this._addEventHandlers();
97
+ this._addPageViewTracking();
72
98
  this._logSessionStart();
73
- this._logPageView();
74
99
  this._logPerformance();
75
100
  }
76
101
  _logError(event) {
@@ -107,15 +132,18 @@ class AutoCapture {
107
132
  this._errorBoundary.logError('AC::logSession', err);
108
133
  }
109
134
  }
110
- _logPageView() {
111
- setTimeout(() => {
112
- const url = (0, Utils_1._getSafeUrl)();
113
- const payload = (0, payloadUtils_1._gatherPageViewPayload)(url);
114
- this._enqueueAutoCapture('page_view', (0, Utils_1._getSanitizedPageUrl)(), payload, {
115
- flushImmediately: true,
116
- addNewSessionMetadata: true,
117
- });
118
- }, 1);
135
+ _tryLogPageView() {
136
+ const url = (0, Utils_1._getSafeUrl)();
137
+ const last = this._previousLoggedPageViewUrl;
138
+ if (last && url.href === last.href) {
139
+ return;
140
+ }
141
+ this._previousLoggedPageViewUrl = url;
142
+ const payload = (0, payloadUtils_1._gatherPageViewPayload)(url);
143
+ this._enqueueAutoCapture('page_view', (0, Utils_1._getSanitizedPageUrl)(), payload, {
144
+ flushImmediately: true,
145
+ addNewSessionMetadata: true,
146
+ });
119
147
  }
120
148
  _logPerformance() {
121
149
  const win = (0, client_core_1._getWindowSafe)();
package/src/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { AutoCapture, runStatsigAutoCapture } from './AutoCapture';
2
- export { AutoCapture, runStatsigAutoCapture };
1
+ import { AutoCapture, StatsigAutoCapturePlugin, runStatsigAutoCapture } from './AutoCapture';
2
+ export { AutoCapture, runStatsigAutoCapture, StatsigAutoCapturePlugin };
3
3
  export default __STATSIG__;
package/src/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runStatsigAutoCapture = exports.AutoCapture = void 0;
3
+ exports.StatsigAutoCapturePlugin = exports.runStatsigAutoCapture = exports.AutoCapture = void 0;
4
4
  const AutoCapture_1 = require("./AutoCapture");
5
5
  Object.defineProperty(exports, "AutoCapture", { enumerable: true, get: function () { return AutoCapture_1.AutoCapture; } });
6
+ Object.defineProperty(exports, "StatsigAutoCapturePlugin", { enumerable: true, get: function () { return AutoCapture_1.StatsigAutoCapturePlugin; } });
6
7
  Object.defineProperty(exports, "runStatsigAutoCapture", { enumerable: true, get: function () { return AutoCapture_1.runStatsigAutoCapture; } });
7
8
  __STATSIG__ = Object.assign(Object.assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { AutoCapture: AutoCapture_1.AutoCapture,
8
- runStatsigAutoCapture: AutoCapture_1.runStatsigAutoCapture });
9
+ runStatsigAutoCapture: AutoCapture_1.runStatsigAutoCapture,
10
+ StatsigAutoCapturePlugin: AutoCapture_1.StatsigAutoCapturePlugin });
9
11
  exports.default = __STATSIG__;