@statsig/web-analytics 0.0.1-beta.41 → 1.0.0

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": "0.0.1-beta.41",
3
+ "version": "1.0.0",
4
4
  "dependencies": {
5
- "@statsig/client-core": "0.0.1-beta.41",
6
- "@statsig/js-client": "0.0.1-beta.41"
5
+ "@statsig/client-core": "1.0.0",
6
+ "@statsig/js-client": "1.0.0"
7
7
  },
8
8
  "jsdelivr": "./build/statsig-web-analytics.min.js",
9
9
  "type": "commonjs",
@@ -1,5 +1,5 @@
1
1
  import { StatsigClient } from '@statsig/js-client';
2
- export declare function runStatsigAutoCapture(client: StatsigClient): void;
2
+ export declare function runStatsigAutoCapture(client: StatsigClient): AutoCapture;
3
3
  export declare class AutoCapture {
4
4
  private _client;
5
5
  private _errorBoundary;
@@ -12,8 +12,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AutoCapture = exports.runStatsigAutoCapture = void 0;
13
13
  const client_core_1 = require("@statsig/client-core");
14
14
  const Utils_1 = require("./Utils");
15
+ const payloadUtils_1 = require("./payloadUtils");
15
16
  function runStatsigAutoCapture(client) {
16
- new AutoCapture(client);
17
+ return new AutoCapture(client);
17
18
  }
18
19
  exports.runStatsigAutoCapture = runStatsigAutoCapture;
19
20
  class AutoCapture {
@@ -102,13 +103,12 @@ class AutoCapture {
102
103
  }
103
104
  _logPageView() {
104
105
  setTimeout(() => {
105
- var _a, _b;
106
106
  const url = (0, Utils_1._getSafeUrl)();
107
- this._enqueueAutoCapture('page_view', (0, Utils_1._getSanitizedPageUrl)(), {
108
- title: (_a = (0, client_core_1._getDocumentSafe)()) === null || _a === void 0 ? void 0 : _a.title,
109
- queryParams: Object.fromEntries(url.searchParams),
110
- referrer: ((_b = (0, client_core_1._getDocumentSafe)()) === null || _b === void 0 ? void 0 : _b.referrer) || '',
111
- }, { flushImmediately: true, addNewSessionMetadata: true });
107
+ const payload = (0, payloadUtils_1._gatherPageViewPayload)(url);
108
+ this._enqueueAutoCapture('page_view', (0, Utils_1._getSanitizedPageUrl)(), payload, {
109
+ flushImmediately: true,
110
+ addNewSessionMetadata: true,
111
+ });
112
112
  }, 1);
113
113
  }
114
114
  _logPerformance() {
@@ -0,0 +1 @@
1
+ export declare function _gatherPageViewPayload(url: URL): Record<string, string | number>;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._gatherPageViewPayload = void 0;
4
+ const client_core_1 = require("@statsig/client-core");
5
+ function _gatherPageViewPayload(url) {
6
+ var _a, _b;
7
+ const safeDoc = (0, client_core_1._getDocumentSafe)();
8
+ const safeWnd = (0, client_core_1._getWindowSafe)();
9
+ if (!safeDoc || !safeWnd) {
10
+ return {};
11
+ }
12
+ const navigator = safeWnd === null || safeWnd === void 0 ? void 0 : safeWnd.navigator;
13
+ const referrer = (safeDoc === null || safeDoc === void 0 ? void 0 : safeDoc.referrer) || '';
14
+ let refUrl = new URL('empty:');
15
+ if (referrer) {
16
+ try {
17
+ refUrl = new URL(referrer || 'empty:');
18
+ }
19
+ catch (e) {
20
+ /* empty */
21
+ }
22
+ }
23
+ const searchInfo = getSearchInfo(refUrl);
24
+ const campaignParams = getCampaignParams(url);
25
+ const queryParams = {};
26
+ url.searchParams.forEach((v, k) => {
27
+ queryParams[k] = v;
28
+ });
29
+ return Object.assign(Object.assign(Object.assign(Object.assign({}, searchInfo), campaignParams), queryParams), { title: (safeDoc === null || safeDoc === void 0 ? void 0 : safeDoc.title) || '', locale: (navigator === null || navigator === void 0 ? void 0 : navigator.language) || 'unknown', referrer, screen_width: ((_a = safeWnd === null || safeWnd === void 0 ? void 0 : safeWnd.screen) === null || _a === void 0 ? void 0 : _a.width) || 'unknown', screen_height: ((_b = safeWnd === null || safeWnd === void 0 ? void 0 : safeWnd.screen) === null || _b === void 0 ? void 0 : _b.height) || 'unknown', viewport_width: (safeWnd === null || safeWnd === void 0 ? void 0 : safeWnd.innerWidth) || 'unknown', viewport_height: (safeWnd === null || safeWnd === void 0 ? void 0 : safeWnd.innerHeight) || 'unknown' });
30
+ }
31
+ exports._gatherPageViewPayload = _gatherPageViewPayload;
32
+ function getCampaignParams(url) {
33
+ const urlParams = url.searchParams;
34
+ const campaignParams = {};
35
+ const commonUtms = [
36
+ 'utm_source',
37
+ 'utm_medium',
38
+ 'utm_campaign',
39
+ 'utm_term',
40
+ 'utm_content',
41
+ 'gclid', // Google
42
+ 'gclsrc', // Google
43
+ 'dclid', // DoubleClick
44
+ 'fbclid', // Facebook
45
+ 'msclkid', // Bing
46
+ 'mc_eid', // Mailchimp
47
+ 'mc_cid', // Mailchimp
48
+ 'twclid', // Twitter
49
+ 'li_fat_id', // LinkedIn
50
+ 'igshid', // Instagram
51
+ 'utm_id', // Hubspot
52
+ 'ttc', // TikTok
53
+ 'ttclid', // TikTok
54
+ 'ttc_id', // TikTok
55
+ ];
56
+ commonUtms.forEach((p) => {
57
+ const val = urlParams.get(p);
58
+ if (val) {
59
+ campaignParams[p] = val;
60
+ }
61
+ });
62
+ return campaignParams;
63
+ }
64
+ function getSearchEngine(refUrl) {
65
+ const host = refUrl.hostname;
66
+ const engine = ['google', 'bing', 'yahoo', 'duckduckgo', 'baidu'].find((e) => host.includes(e + '.'));
67
+ return engine || '';
68
+ }
69
+ function getSearchInfo(refUrl) {
70
+ const searchEngine = getSearchEngine(refUrl);
71
+ const searchQuery = refUrl.searchParams.get(searchEngine === 'yahoo' ? 'p' : 'q') || '';
72
+ return { searchEngine, searchQuery };
73
+ }