@statsig/web-analytics 0.0.1-beta.30 → 0.0.1-beta.32

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/README.md CHANGED
@@ -1,11 +1,7 @@
1
- # web-analytics
1
+ # Statsig Web Analytics
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ Web analytics lets you track and watch key measures for your website easily. It is different from product analytics because it's simpler and more direct, making it great for marketers, web site maintainers, or anyone familiar with tools like Google Analytics.
4
4
 
5
- ## Building
5
+ ## Getting Started
6
6
 
7
- Run `nx build web-analytics` to build the library.
8
-
9
- ## Running unit tests
10
-
11
- Run `nx test web-analytics` to execute the unit tests via [Jest](https://jestjs.io).
7
+ Read through the [Documentation](https://docs.statsig.com/webanalytics/overview) or check out the [Samples](https://github.com/statsig-io/js-client-monorepo/tree/main/samples).
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@statsig/web-analytics",
3
- "version": "0.0.1-beta.30",
3
+ "version": "0.0.1-beta.32",
4
4
  "dependencies": {
5
- "@statsig/client-core": "0.0.1-beta.30",
6
- "@statsig/js-client": "0.0.1-beta.30"
5
+ "@statsig/client-core": "0.0.1-beta.32",
6
+ "@statsig/js-client": "0.0.1-beta.32"
7
7
  },
8
8
  "jsdelivr": "./build/statsig-web-analytics.min.js",
9
9
  "type": "commonjs",
@@ -16,4 +16,5 @@ export declare class AutoCapture {
16
16
  private _enqueueAutoCapture;
17
17
  private _logAutoCaptureImmediately;
18
18
  private _scrollEventHandler;
19
+ private _getSessionIdFromClient;
19
20
  }
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.AutoCapture = exports.runStatsigAutoCapture = void 0;
4
13
  const client_core_1 = require("@statsig/client-core");
@@ -16,11 +25,13 @@ class AutoCapture {
16
25
  const { sdkKey } = _client.getContext();
17
26
  this._errorBoundary = new client_core_1.ErrorBoundary(sdkKey);
18
27
  (0, client_core_1.monitorClass)(this._errorBoundary, this);
19
- __STATSIG__ = __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {};
20
- const instances = (_a = __STATSIG__.acInstances) !== null && _a !== void 0 ? _a : {};
21
- instances[sdkKey] = this;
22
- __STATSIG__.acInstances = instances;
23
28
  const doc = (0, client_core_1._getDocumentSafe)();
29
+ if ((0, client_core_1._isBrowserEnv)()) {
30
+ __STATSIG__ = (0, client_core_1._getStatsigGlobal)();
31
+ const instances = (_a = __STATSIG__.acInstances) !== null && _a !== void 0 ? _a : {};
32
+ instances[sdkKey] = this;
33
+ __STATSIG__.acInstances = instances;
34
+ }
24
35
  if ((doc === null || doc === void 0 ? void 0 : doc.readyState) === 'loading') {
25
36
  doc.addEventListener('DOMContentLoaded', () => this._initialize());
26
37
  return;
@@ -135,14 +146,19 @@ class AutoCapture {
135
146
  });
136
147
  }
137
148
  _enqueueAutoCapture(name, value, metadata) {
138
- var _a, _b, _c;
139
- const event = {
140
- eventName: `auto_capture::${name}`,
141
- value,
142
- metadata: Object.assign({ sessionId: (0, Utils_1._getWebSessionId)(this._client.getContext().sdkKey), page_url: (_c = (_b = (_a = (0, client_core_1._getWindowSafe)()) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.href) !== null && _c !== void 0 ? _c : '' }, metadata),
143
- };
144
- this._client.logEvent(event);
145
- client_core_1.Log.debug('Enqueued Event', event);
149
+ this._getSessionIdFromClient()
150
+ .then((sessionID) => {
151
+ var _a, _b, _c;
152
+ const event = {
153
+ eventName: `auto_capture::${name}`,
154
+ value,
155
+ metadata: Object.assign({ sessionID, page_url: (_c = (_b = (_a = (0, client_core_1._getWindowSafe)()) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.href) !== null && _c !== void 0 ? _c : '' }, metadata),
156
+ };
157
+ this._client.logEvent(event);
158
+ })
159
+ .catch((err) => {
160
+ this._errorBoundary.logError('AC::enqueue', err);
161
+ });
146
162
  }
147
163
  _logAutoCaptureImmediately(name, value, metadata) {
148
164
  this._enqueueAutoCapture(name, value, metadata);
@@ -158,5 +174,11 @@ class AutoCapture {
158
174
  const innerHeight = (_d = win === null || win === void 0 ? void 0 : win.innerHeight) !== null && _d !== void 0 ? _d : 1;
159
175
  this._deepestScroll = Math.max(this._deepestScroll, Math.min(100, Math.round(((scrollY + innerHeight) / scrollHeight) * 100)));
160
176
  }
177
+ _getSessionIdFromClient() {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ const x = yield this._client.getAsyncContext();
180
+ return x.sessionID;
181
+ });
182
+ }
161
183
  }
162
184
  exports.AutoCapture = AutoCapture;
package/src/Utils.d.ts CHANGED
@@ -5,6 +5,5 @@ export declare function _gatherEventData(target: Element): {
5
5
  export declare function _getTargetNode(e: Event): Element | null;
6
6
  export declare function _shouldLogEvent(e: Event, el: Element): boolean;
7
7
  export declare function _getSafeUrl(): URL;
8
- export declare function _getWebSessionId(sdkKey: string): string;
9
8
  export declare function _getSanitizedPageUrl(): string;
10
9
  export declare function _registerEventHandler(element: Document | Window, eventType: string, handler: (event: Event) => void): void;
package/src/Utils.js CHANGED
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._registerEventHandler = exports._getSanitizedPageUrl = exports._getWebSessionId = exports._getSafeUrl = exports._shouldLogEvent = exports._getTargetNode = exports._gatherEventData = void 0;
3
+ exports._registerEventHandler = exports._getSanitizedPageUrl = exports._getSafeUrl = exports._shouldLogEvent = exports._getTargetNode = exports._gatherEventData = void 0;
4
4
  const client_core_1 = require("@statsig/client-core");
5
- const MAX_SESSION_IDLE_TIME = 10 * 60 * 1000; // 10 minutes
6
- const MAX_SESSION_AGE = 4 * 60 * 60 * 1000; // 4 hours
7
- const globals = {};
8
5
  function _gatherEventData(target) {
9
6
  var _a;
10
7
  const tagName = target.tagName.toLowerCase();
@@ -87,27 +84,8 @@ function _getSafeUrl() {
87
84
  return url;
88
85
  }
89
86
  exports._getSafeUrl = _getSafeUrl;
90
- function _getWebSessionId(sdkKey) {
91
- const key = `statsig.web_analytics.session.${(0, client_core_1.DJB2)(sdkKey)}`;
92
- const json = _getLocalValue(key);
93
- const now = Date.now();
94
- let session = json
95
- ? (0, client_core_1.typedJsonParse)(json, 'lastAccessedTime', 'Failed to parse WebSession')
96
- : null;
97
- if (!session ||
98
- now - session.startTime > MAX_SESSION_AGE ||
99
- now - session.lastAccessedTime > MAX_SESSION_IDLE_TIME) {
100
- session = _createNewSession();
101
- }
102
- session.lastAccessedTime = now;
103
- _setLocalValue(key, JSON.stringify(session));
104
- return session.id;
105
- }
106
- exports._getWebSessionId = _getWebSessionId;
107
87
  function _getSanitizedPageUrl() {
108
- var _a, _b, _c;
109
- const url = (_c = (_b = (_a = (0, client_core_1._getWindowSafe)()) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.href) === null || _c === void 0 ? void 0 : _c.split(/[?#]/)[0];
110
- return url || '';
88
+ return (0, client_core_1._getCurrentPageUrlSafe)() || '';
111
89
  }
112
90
  exports._getSanitizedPageUrl = _getSanitizedPageUrl;
113
91
  function _registerEventHandler(element, eventType, handler) {
@@ -134,28 +112,3 @@ function _getAnchorNodeInHierarchy(node) {
134
112
  }
135
113
  return null;
136
114
  }
137
- function _setLocalValue(key, value) {
138
- const win = (0, client_core_1._getWindowSafe)();
139
- if (win && win.localStorage) {
140
- win.localStorage.setItem(key, value);
141
- }
142
- else {
143
- globals[key] = value;
144
- client_core_1.Log.error('AutoCapture: No window.localStorage');
145
- }
146
- }
147
- function _getLocalValue(key) {
148
- const win = (0, client_core_1._getWindowSafe)();
149
- if (win && win.localStorage) {
150
- return win.localStorage.getItem(key);
151
- }
152
- return globals[key];
153
- }
154
- function _createNewSession() {
155
- const now = Date.now();
156
- return {
157
- id: (0, client_core_1.getUUID)(),
158
- startTime: now,
159
- lastAccessedTime: now,
160
- };
161
- }