onerway-analytics 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.
Files changed (48) hide show
  1. package/dist/bundle/index.js +1361 -0
  2. package/dist/bundle/index.js.map +7 -0
  3. package/dist/core/analytics.d.ts +45 -0
  4. package/dist/core/analytics.d.ts.map +1 -0
  5. package/dist/core/analytics.js +369 -0
  6. package/dist/core/analytics.test.d.ts +2 -0
  7. package/dist/core/analytics.test.d.ts.map +1 -0
  8. package/dist/core/analytics.test.js +169 -0
  9. package/dist/core/auto-track.d.ts +6 -0
  10. package/dist/core/auto-track.d.ts.map +1 -0
  11. package/dist/core/auto-track.js +126 -0
  12. package/dist/core/context-collector.d.ts +4 -0
  13. package/dist/core/context-collector.d.ts.map +1 -0
  14. package/dist/core/context-collector.js +42 -0
  15. package/dist/core/error-monitor.d.ts +5 -0
  16. package/dist/core/error-monitor.d.ts.map +1 -0
  17. package/dist/core/error-monitor.js +61 -0
  18. package/dist/core/ga-adapter.d.ts +12 -0
  19. package/dist/core/ga-adapter.d.ts.map +1 -0
  20. package/dist/core/ga-adapter.js +35 -0
  21. package/dist/core/ga-loader.d.ts +10 -0
  22. package/dist/core/ga-loader.d.ts.map +1 -0
  23. package/dist/core/ga-loader.js +30 -0
  24. package/dist/core/ga-manager.d.ts +24 -0
  25. package/dist/core/ga-manager.d.ts.map +1 -0
  26. package/dist/core/ga-manager.js +64 -0
  27. package/dist/core/performance-monitor.d.ts +5 -0
  28. package/dist/core/performance-monitor.d.ts.map +1 -0
  29. package/dist/core/performance-monitor.js +43 -0
  30. package/dist/core/sender.d.ts +10 -0
  31. package/dist/core/sender.d.ts.map +1 -0
  32. package/dist/core/sender.js +65 -0
  33. package/dist/core/session-manager.d.ts +13 -0
  34. package/dist/core/session-manager.d.ts.map +1 -0
  35. package/dist/core/session-manager.js +54 -0
  36. package/dist/core/storage.d.ts +9 -0
  37. package/dist/core/storage.d.ts.map +1 -0
  38. package/dist/core/storage.js +48 -0
  39. package/dist/index.d.ts +16 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +11 -0
  42. package/dist/types/index.d.ts +162 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types/index.js +28 -0
  45. package/dist/utils/index.d.ts +30 -0
  46. package/dist/utils/index.d.ts.map +1 -0
  47. package/dist/utils/index.js +216 -0
  48. package/package.json +47 -0
@@ -0,0 +1,216 @@
1
+ export function generateId() {
2
+ return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
3
+ }
4
+ export function getTimestamp() {
5
+ return Date.now();
6
+ }
7
+ export function isObject(value) {
8
+ return Object.prototype.toString.call(value) === '[object Object]';
9
+ }
10
+ export function merge(target, source) {
11
+ const result = { ...target };
12
+ for (const key in source) {
13
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
14
+ result[key] = source[key];
15
+ }
16
+ }
17
+ return result;
18
+ }
19
+ export function generateUUID() {
20
+ const hex = '0123456789abcdef';
21
+ const template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
22
+ return template.replace(/[xy]/g, (c) => {
23
+ const r = (Math.random() * 16) | 0;
24
+ const v = c === 'x' ? r : (r & 0x3) | 0x8;
25
+ return hex[v];
26
+ });
27
+ }
28
+ export function generateShortId() {
29
+ return Math.random().toString(36).substring(2, 10);
30
+ }
31
+ export function detectBrowser() {
32
+ if (typeof navigator === 'undefined')
33
+ return { name: 'Unknown', version: '0' };
34
+ const ua = navigator.userAgent;
35
+ const edgeMatch = ua.match(/Edg\/(\d+)/);
36
+ if (edgeMatch)
37
+ return { name: 'Edge', version: edgeMatch[1] };
38
+ const chromeMatch = ua.match(/Chrome\/(\d+)/);
39
+ const safariMatch = ua.match(/Safari\/(\d+)/);
40
+ if (chromeMatch && !ua.includes('Edg') && !ua.includes('OPR')) {
41
+ return { name: 'Chrome', version: chromeMatch[1] };
42
+ }
43
+ const firefoxMatch = ua.match(/Firefox\/(\d+)/);
44
+ if (firefoxMatch)
45
+ return { name: 'Firefox', version: firefoxMatch[1] };
46
+ const opMatch = ua.match(/OPR\/(\d+)/);
47
+ if (opMatch)
48
+ return { name: 'Opera', version: opMatch[1] };
49
+ if (safariMatch && !ua.includes('Chrome')) {
50
+ const versionMatch = ua.match(/Version\/(\d+)/);
51
+ return { name: 'Safari', version: versionMatch ? versionMatch[1] : safariMatch[1] };
52
+ }
53
+ const ieMatch = ua.match(/(?:MSIE |Trident\/.*; rv:)(\d+)/);
54
+ if (ieMatch)
55
+ return { name: 'IE', version: ieMatch[1] };
56
+ return { name: 'Unknown', version: '0' };
57
+ }
58
+ export function detectOS() {
59
+ if (typeof navigator === 'undefined')
60
+ return { name: 'Unknown', version: '0' };
61
+ const ua = navigator.userAgent;
62
+ const platform = navigator.platform || '';
63
+ if (ua.includes('Windows NT 10'))
64
+ return { name: 'Windows', version: '10' };
65
+ if (ua.includes('Windows NT 6.3'))
66
+ return { name: 'Windows', version: '8.1' };
67
+ if (ua.includes('Windows NT 6.2'))
68
+ return { name: 'Windows', version: '8' };
69
+ if (ua.includes('Windows NT 6.1'))
70
+ return { name: 'Windows', version: '7' };
71
+ if (/Windows NT \d/.test(ua))
72
+ return { name: 'Windows', version: ua.match(/Windows NT (\d+\.?\d*)/)?.[1] || '0' };
73
+ const macMatch = ua.match(/Mac OS X (\d+[._]\d+)/);
74
+ if (macMatch)
75
+ return { name: 'macOS', version: macMatch[1].replace('_', '.') };
76
+ if (platform.startsWith('Mac'))
77
+ return { name: 'macOS', version: '0' };
78
+ if (/Android (\d+\.?\d*)/.test(ua))
79
+ return { name: 'Android', version: ua.match(/Android (\d+\.?\d*)/)?.[1] || '0' };
80
+ if (/iPhone|iPad|iPod/.test(ua)) {
81
+ const iosMatch = ua.match(/OS (\d+[._]\d+)/);
82
+ return { name: 'iOS', version: iosMatch ? iosMatch[1].replace('_', '.') : '0' };
83
+ }
84
+ if (/Linux/.test(ua) || platform.includes('Linux'))
85
+ return { name: 'Linux', version: '0' };
86
+ return { name: 'Unknown', version: '0' };
87
+ }
88
+ export function getDeviceType() {
89
+ if (typeof window === 'undefined' || typeof navigator === 'undefined')
90
+ return 'desktop';
91
+ const width = window.screen.width;
92
+ const hasTouch = navigator.maxTouchPoints > 0;
93
+ if (width < 768 && hasTouch)
94
+ return 'mobile';
95
+ if (width >= 768 && width <= 1024 && hasTouch)
96
+ return 'tablet';
97
+ return 'desktop';
98
+ }
99
+ export function getScreenSize() {
100
+ if (typeof window === 'undefined')
101
+ return { width: 0, height: 0 };
102
+ return {
103
+ width: window.screen.width,
104
+ height: window.screen.height,
105
+ };
106
+ }
107
+ export function getPageUrl() {
108
+ if (typeof window === 'undefined')
109
+ return '';
110
+ return window.location.href;
111
+ }
112
+ export function getReferrer() {
113
+ if (typeof document === 'undefined')
114
+ return '';
115
+ return document.referrer || '';
116
+ }
117
+ export function getHostname() {
118
+ if (typeof window === 'undefined')
119
+ return '';
120
+ return window.location.host.split(':')[0];
121
+ }
122
+ export function getTimezone() {
123
+ try {
124
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
125
+ }
126
+ catch {
127
+ return 'UTC';
128
+ }
129
+ }
130
+ export function getLanguage() {
131
+ if (typeof navigator === 'undefined')
132
+ return 'en-US';
133
+ return navigator.language || 'en-US';
134
+ }
135
+ export function getCountryFromLanguage(language) {
136
+ const parts = language.split('-');
137
+ if (parts.length > 1)
138
+ return parts[1].toUpperCase();
139
+ const langMap = {
140
+ zh: 'CN', en: 'US', ja: 'JP', ko: 'KR', fr: 'FR',
141
+ de: 'DE', es: 'ES', pt: 'BR', ru: 'RU', ar: 'SA',
142
+ };
143
+ return langMap[parts[0]] || parts[0].toUpperCase();
144
+ }
145
+ export function parseUTMParams() {
146
+ if (typeof window === 'undefined')
147
+ return {};
148
+ const params = {};
149
+ const searchParams = new URLSearchParams(window.location.search);
150
+ const utmKeys = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
151
+ for (const key of utmKeys) {
152
+ const value = searchParams.get(key);
153
+ if (value)
154
+ params[key] = value;
155
+ }
156
+ return params;
157
+ }
158
+ export function isBrowser() {
159
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
160
+ }
161
+ export function safeLocalStorageGet(key) {
162
+ try {
163
+ return localStorage.getItem(key);
164
+ }
165
+ catch {
166
+ return null;
167
+ }
168
+ }
169
+ export function safeLocalStorageSet(key, value) {
170
+ try {
171
+ localStorage.setItem(key, value);
172
+ return true;
173
+ }
174
+ catch {
175
+ return false;
176
+ }
177
+ }
178
+ export function safeLocalStorageRemove(key) {
179
+ try {
180
+ localStorage.removeItem(key);
181
+ }
182
+ catch {
183
+ // ignore
184
+ }
185
+ }
186
+ export function safeSessionStorageGet(key) {
187
+ try {
188
+ return sessionStorage.getItem(key);
189
+ }
190
+ catch {
191
+ return null;
192
+ }
193
+ }
194
+ export function safeSessionStorageSet(key, value) {
195
+ try {
196
+ sessionStorage.setItem(key, value);
197
+ return true;
198
+ }
199
+ catch {
200
+ return false;
201
+ }
202
+ }
203
+ export function safeSessionStorageRemove(key) {
204
+ try {
205
+ sessionStorage.removeItem(key);
206
+ }
207
+ catch {
208
+ // ignore
209
+ }
210
+ }
211
+ export function truncateText(text, maxLength) {
212
+ if (!text)
213
+ return '';
214
+ const trimmed = text.trim().replace(/\s+/g, ' ');
215
+ return trimmed.length > maxLength ? trimmed.substring(0, maxLength) + '...' : trimmed;
216
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "onerway-analytics",
3
+ "version": "1.0.0",
4
+ "description": "Onrway Analytics SDK",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/types/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/types/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/bundle/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "build:browser": "node build-browser.js",
22
+ "build:watch": "tsc --watch",
23
+ "lint": "eslint src --ext .ts",
24
+ "test": "jest",
25
+ "typecheck": "tsc --noEmit"
26
+ },
27
+ "keywords": [
28
+ "analytics",
29
+ "tracking",
30
+ "sdk"
31
+ ],
32
+ "license": "MIT",
33
+ "devDependencies": {
34
+ "@types/jest": "^29.5.0",
35
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
36
+ "@typescript-eslint/parser": "^6.0.0",
37
+ "esbuild": "^0.28.0",
38
+ "eslint": "^8.57.0",
39
+ "jest": "^29.7.0",
40
+ "jest-environment-jsdom": "^30.4.1",
41
+ "ts-jest": "^29.1.0",
42
+ "typescript": "^5.3.0"
43
+ },
44
+ "dependencies": {
45
+ "web-vitals": "^5.2.0"
46
+ }
47
+ }