@shware/analytics 0.0.8 → 0.1.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/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { T as TrackTags, a as
|
|
1
|
+
import { T as TrackTags, a as ThirdPartyTracker, b as ThirdPartyUserSetter, E as EventName, c as TrackProperties, d as TrackEventResponse, V as Visitor, e as VisitorProperties } from './shared/analytics.BsiFCGfy.mjs';
|
|
2
|
+
import { z } from 'zod';
|
|
2
3
|
|
|
3
4
|
interface Storage {
|
|
4
5
|
getItem: (key: string) => (string | null) | Promise<string | null>;
|
|
@@ -10,12 +11,13 @@ interface Options {
|
|
|
10
11
|
endpoint: string;
|
|
11
12
|
getTags: () => TrackTags | Promise<TrackTags>;
|
|
12
13
|
getDeviceId: () => string | Promise<string>;
|
|
13
|
-
|
|
14
|
+
thirdPartyTrackers?: ThirdPartyTracker[];
|
|
15
|
+
thirdPartyUserSetters?: ThirdPartyUserSetter[];
|
|
14
16
|
}
|
|
15
17
|
declare function setupAnalytics(init: Options): void;
|
|
16
18
|
|
|
17
19
|
interface TrackOptions {
|
|
18
|
-
|
|
20
|
+
enableThirdPartyTracking?: boolean;
|
|
19
21
|
onSucceed?: (response?: TrackEventResponse) => void;
|
|
20
22
|
onError?: (error: unknown) => void;
|
|
21
23
|
}
|
|
@@ -24,4 +26,168 @@ declare function track<T extends EventName = EventName>(name: T, properties?: Tr
|
|
|
24
26
|
declare function getVisitor(): Promise<Visitor>;
|
|
25
27
|
declare function setVisitor(properties: VisitorProperties): Promise<Visitor>;
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
declare const createTrackEventSchema: z.ZodObject<{
|
|
30
|
+
name: z.ZodString;
|
|
31
|
+
tags: z.ZodObject<{
|
|
32
|
+
os: z.ZodOptional<z.ZodString>;
|
|
33
|
+
os_name: z.ZodOptional<z.ZodString>;
|
|
34
|
+
os_version: z.ZodOptional<z.ZodString>;
|
|
35
|
+
browser: z.ZodOptional<z.ZodString>;
|
|
36
|
+
browser_name: z.ZodOptional<z.ZodString>;
|
|
37
|
+
browser_version: z.ZodOptional<z.ZodString>;
|
|
38
|
+
platform: z.ZodOptional<z.ZodEnum<["ios", "android", "web", "macos", "windows", "linux", "unknown"]>>;
|
|
39
|
+
device: z.ZodOptional<z.ZodString>;
|
|
40
|
+
device_id: z.ZodOptional<z.ZodString>;
|
|
41
|
+
device_type: z.ZodOptional<z.ZodString>;
|
|
42
|
+
device_vendor: z.ZodOptional<z.ZodString>;
|
|
43
|
+
device_pixel_ratio: z.ZodOptional<z.ZodString>;
|
|
44
|
+
screen_resolution: z.ZodOptional<z.ZodEffects<z.ZodString, `${number}x${number}`, string>>;
|
|
45
|
+
release: z.ZodOptional<z.ZodString>;
|
|
46
|
+
language: z.ZodOptional<z.ZodString>;
|
|
47
|
+
timezone: z.ZodOptional<z.ZodString>;
|
|
48
|
+
environment: z.ZodOptional<z.ZodEnum<["development", "production"]>>;
|
|
49
|
+
source_url: z.ZodOptional<z.ZodString>;
|
|
50
|
+
source: z.ZodOptional<z.ZodEnum<["web", "app", "offline"]>>;
|
|
51
|
+
fbc: z.ZodOptional<z.ZodString>;
|
|
52
|
+
fbp: z.ZodOptional<z.ZodString>;
|
|
53
|
+
gclid: z.ZodOptional<z.ZodString>;
|
|
54
|
+
utm_source: z.ZodOptional<z.ZodString>;
|
|
55
|
+
utm_medium: z.ZodOptional<z.ZodString>;
|
|
56
|
+
utm_campaign: z.ZodOptional<z.ZodString>;
|
|
57
|
+
utm_term: z.ZodOptional<z.ZodString>;
|
|
58
|
+
utm_content: z.ZodOptional<z.ZodString>;
|
|
59
|
+
}, "strip", z.ZodTypeAny, {
|
|
60
|
+
os?: string | undefined;
|
|
61
|
+
os_name?: string | undefined;
|
|
62
|
+
os_version?: string | undefined;
|
|
63
|
+
browser?: string | undefined;
|
|
64
|
+
browser_name?: string | undefined;
|
|
65
|
+
browser_version?: string | undefined;
|
|
66
|
+
platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
|
|
67
|
+
device?: string | undefined;
|
|
68
|
+
device_id?: string | undefined;
|
|
69
|
+
device_type?: string | undefined;
|
|
70
|
+
device_vendor?: string | undefined;
|
|
71
|
+
device_pixel_ratio?: string | undefined;
|
|
72
|
+
screen_resolution?: `${number}x${number}` | undefined;
|
|
73
|
+
release?: string | undefined;
|
|
74
|
+
language?: string | undefined;
|
|
75
|
+
timezone?: string | undefined;
|
|
76
|
+
environment?: "development" | "production" | undefined;
|
|
77
|
+
source_url?: string | undefined;
|
|
78
|
+
source?: "web" | "app" | "offline" | undefined;
|
|
79
|
+
fbc?: string | undefined;
|
|
80
|
+
fbp?: string | undefined;
|
|
81
|
+
gclid?: string | undefined;
|
|
82
|
+
utm_source?: string | undefined;
|
|
83
|
+
utm_medium?: string | undefined;
|
|
84
|
+
utm_campaign?: string | undefined;
|
|
85
|
+
utm_term?: string | undefined;
|
|
86
|
+
utm_content?: string | undefined;
|
|
87
|
+
}, {
|
|
88
|
+
os?: string | undefined;
|
|
89
|
+
os_name?: string | undefined;
|
|
90
|
+
os_version?: string | undefined;
|
|
91
|
+
browser?: string | undefined;
|
|
92
|
+
browser_name?: string | undefined;
|
|
93
|
+
browser_version?: string | undefined;
|
|
94
|
+
platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
|
|
95
|
+
device?: string | undefined;
|
|
96
|
+
device_id?: string | undefined;
|
|
97
|
+
device_type?: string | undefined;
|
|
98
|
+
device_vendor?: string | undefined;
|
|
99
|
+
device_pixel_ratio?: string | undefined;
|
|
100
|
+
screen_resolution?: string | undefined;
|
|
101
|
+
release?: string | undefined;
|
|
102
|
+
language?: string | undefined;
|
|
103
|
+
timezone?: string | undefined;
|
|
104
|
+
environment?: "development" | "production" | undefined;
|
|
105
|
+
source_url?: string | undefined;
|
|
106
|
+
source?: "web" | "app" | "offline" | undefined;
|
|
107
|
+
fbc?: string | undefined;
|
|
108
|
+
fbp?: string | undefined;
|
|
109
|
+
gclid?: string | undefined;
|
|
110
|
+
utm_source?: string | undefined;
|
|
111
|
+
utm_medium?: string | undefined;
|
|
112
|
+
utm_campaign?: string | undefined;
|
|
113
|
+
utm_term?: string | undefined;
|
|
114
|
+
utm_content?: string | undefined;
|
|
115
|
+
}>;
|
|
116
|
+
properties: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
117
|
+
}, "strip", z.ZodTypeAny, {
|
|
118
|
+
name: string;
|
|
119
|
+
tags: {
|
|
120
|
+
os?: string | undefined;
|
|
121
|
+
os_name?: string | undefined;
|
|
122
|
+
os_version?: string | undefined;
|
|
123
|
+
browser?: string | undefined;
|
|
124
|
+
browser_name?: string | undefined;
|
|
125
|
+
browser_version?: string | undefined;
|
|
126
|
+
platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
|
|
127
|
+
device?: string | undefined;
|
|
128
|
+
device_id?: string | undefined;
|
|
129
|
+
device_type?: string | undefined;
|
|
130
|
+
device_vendor?: string | undefined;
|
|
131
|
+
device_pixel_ratio?: string | undefined;
|
|
132
|
+
screen_resolution?: `${number}x${number}` | undefined;
|
|
133
|
+
release?: string | undefined;
|
|
134
|
+
language?: string | undefined;
|
|
135
|
+
timezone?: string | undefined;
|
|
136
|
+
environment?: "development" | "production" | undefined;
|
|
137
|
+
source_url?: string | undefined;
|
|
138
|
+
source?: "web" | "app" | "offline" | undefined;
|
|
139
|
+
fbc?: string | undefined;
|
|
140
|
+
fbp?: string | undefined;
|
|
141
|
+
gclid?: string | undefined;
|
|
142
|
+
utm_source?: string | undefined;
|
|
143
|
+
utm_medium?: string | undefined;
|
|
144
|
+
utm_campaign?: string | undefined;
|
|
145
|
+
utm_term?: string | undefined;
|
|
146
|
+
utm_content?: string | undefined;
|
|
147
|
+
};
|
|
148
|
+
properties: Record<string, string | number | boolean | null>;
|
|
149
|
+
}, {
|
|
150
|
+
name: string;
|
|
151
|
+
tags: {
|
|
152
|
+
os?: string | undefined;
|
|
153
|
+
os_name?: string | undefined;
|
|
154
|
+
os_version?: string | undefined;
|
|
155
|
+
browser?: string | undefined;
|
|
156
|
+
browser_name?: string | undefined;
|
|
157
|
+
browser_version?: string | undefined;
|
|
158
|
+
platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
|
|
159
|
+
device?: string | undefined;
|
|
160
|
+
device_id?: string | undefined;
|
|
161
|
+
device_type?: string | undefined;
|
|
162
|
+
device_vendor?: string | undefined;
|
|
163
|
+
device_pixel_ratio?: string | undefined;
|
|
164
|
+
screen_resolution?: string | undefined;
|
|
165
|
+
release?: string | undefined;
|
|
166
|
+
language?: string | undefined;
|
|
167
|
+
timezone?: string | undefined;
|
|
168
|
+
environment?: "development" | "production" | undefined;
|
|
169
|
+
source_url?: string | undefined;
|
|
170
|
+
source?: "web" | "app" | "offline" | undefined;
|
|
171
|
+
fbc?: string | undefined;
|
|
172
|
+
fbp?: string | undefined;
|
|
173
|
+
gclid?: string | undefined;
|
|
174
|
+
utm_source?: string | undefined;
|
|
175
|
+
utm_medium?: string | undefined;
|
|
176
|
+
utm_campaign?: string | undefined;
|
|
177
|
+
utm_term?: string | undefined;
|
|
178
|
+
utm_content?: string | undefined;
|
|
179
|
+
};
|
|
180
|
+
properties: Record<string, string | number | boolean | null>;
|
|
181
|
+
}>;
|
|
182
|
+
declare const createVisitorSchema: z.ZodObject<{
|
|
183
|
+
device_id: z.ZodString;
|
|
184
|
+
properties: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
185
|
+
}, "strip", z.ZodTypeAny, {
|
|
186
|
+
device_id: string;
|
|
187
|
+
properties: Record<string, string | number | boolean | null>;
|
|
188
|
+
}, {
|
|
189
|
+
device_id: string;
|
|
190
|
+
properties: Record<string, string | number | boolean | null>;
|
|
191
|
+
}>;
|
|
192
|
+
|
|
193
|
+
export { TrackProperties, TrackTags, VisitorProperties, createTrackEventSchema, createVisitorSchema, getVisitor, setVisitor, setupAnalytics, track };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import retry from 'axios-retry';
|
|
3
3
|
import { TokenBucket } from 'limiter';
|
|
4
|
+
import { z } from 'zod';
|
|
4
5
|
|
|
5
6
|
const config = {
|
|
6
7
|
http: null,
|
|
@@ -8,14 +9,16 @@ const config = {
|
|
|
8
9
|
storage: null,
|
|
9
10
|
getTags: null,
|
|
10
11
|
getDeviceId: null,
|
|
11
|
-
|
|
12
|
+
thirdPartyTrackers: [],
|
|
13
|
+
thirdPartyUserSetters: []
|
|
12
14
|
};
|
|
13
15
|
function setupAnalytics(init) {
|
|
14
16
|
config.release = init.release;
|
|
15
17
|
config.storage = init.storage;
|
|
16
18
|
config.getTags = init.getTags;
|
|
17
19
|
config.getDeviceId = init.getDeviceId;
|
|
18
|
-
config.
|
|
20
|
+
config.thirdPartyTrackers = init.thirdPartyTrackers ?? [];
|
|
21
|
+
config.thirdPartyUserSetters = init.thirdPartyUserSetters ?? [];
|
|
19
22
|
config.http = axios.create({ baseURL: init.endpoint, withCredentials: true, adapter: "fetch" });
|
|
20
23
|
retry(config.http, { retries: 5, retryDelay: retry.exponentialDelay });
|
|
21
24
|
}
|
|
@@ -50,11 +53,12 @@ async function setVisitor(properties) {
|
|
|
50
53
|
const dto = { properties };
|
|
51
54
|
const { id } = await getVisitor();
|
|
52
55
|
const response = await config.http.patch(`/visitors/${id}`, dto);
|
|
56
|
+
config.thirdPartyUserSetters.forEach((setter) => setter(properties));
|
|
53
57
|
visitor = response.data;
|
|
54
58
|
return response.data;
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
const defaultOptions = {
|
|
61
|
+
const defaultOptions = { enableThirdPartyTracking: true };
|
|
58
62
|
const REQUEST_TOKENS = 2;
|
|
59
63
|
const tokenBucket = new TokenBucket({
|
|
60
64
|
bucketSize: 20,
|
|
@@ -72,8 +76,8 @@ async function trackAsync(name, properties, trackOptions = defaultOptions) {
|
|
|
72
76
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
73
77
|
};
|
|
74
78
|
const { data } = await config.http.post(`/events`, dto);
|
|
75
|
-
if (!trackOptions.
|
|
76
|
-
config.
|
|
79
|
+
if (!trackOptions.enableThirdPartyTracking || !config.thirdPartyTrackers) return;
|
|
80
|
+
config.thirdPartyTrackers.forEach((tracker) => tracker(name, properties, data.id));
|
|
77
81
|
trackOptions.onSucceed?.(data);
|
|
78
82
|
} catch (e) {
|
|
79
83
|
if (e instanceof Error) {
|
|
@@ -86,4 +90,48 @@ function track(name, properties, trackOptions = defaultOptions) {
|
|
|
86
90
|
trackAsync(name, properties, trackOptions).catch(console.error);
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
|
|
93
|
+
const createTrackEventSchema = z.object({
|
|
94
|
+
name: z.string().trim().min(1).max(64),
|
|
95
|
+
tags: z.object({
|
|
96
|
+
os: z.string().optional(),
|
|
97
|
+
os_name: z.string().optional(),
|
|
98
|
+
os_version: z.string().optional(),
|
|
99
|
+
browser: z.string().optional(),
|
|
100
|
+
browser_name: z.string().optional(),
|
|
101
|
+
browser_version: z.string().optional(),
|
|
102
|
+
platform: z.enum(["ios", "android", "web", "macos", "windows", "linux", "unknown"]).optional(),
|
|
103
|
+
device: z.string().optional(),
|
|
104
|
+
device_id: z.string().trim().min(1).max(36).optional(),
|
|
105
|
+
device_type: z.string().optional(),
|
|
106
|
+
device_vendor: z.string().optional(),
|
|
107
|
+
device_pixel_ratio: z.string().optional(),
|
|
108
|
+
screen_resolution: z.string().regex(/^\d+x\d+$/).transform((v) => v).optional(),
|
|
109
|
+
release: z.string().optional(),
|
|
110
|
+
language: z.string().optional(),
|
|
111
|
+
timezone: z.string().optional(),
|
|
112
|
+
environment: z.enum(["development", "production"]).optional(),
|
|
113
|
+
source_url: z.string().optional(),
|
|
114
|
+
source: z.enum(["web", "app", "offline"]).optional(),
|
|
115
|
+
fbc: z.string().optional(),
|
|
116
|
+
fbp: z.string().optional(),
|
|
117
|
+
gclid: z.string().optional(),
|
|
118
|
+
utm_source: z.string().optional(),
|
|
119
|
+
utm_medium: z.string().optional(),
|
|
120
|
+
utm_campaign: z.string().optional(),
|
|
121
|
+
utm_term: z.string().optional(),
|
|
122
|
+
utm_content: z.string().optional()
|
|
123
|
+
}),
|
|
124
|
+
properties: z.record(
|
|
125
|
+
z.string().trim().min(1).max(128),
|
|
126
|
+
z.union([z.string(), z.number(), z.boolean(), z.null()])
|
|
127
|
+
)
|
|
128
|
+
});
|
|
129
|
+
const createVisitorSchema = z.object({
|
|
130
|
+
device_id: z.string().trim().min(1).max(36),
|
|
131
|
+
properties: z.record(
|
|
132
|
+
z.string().trim().min(1).max(128),
|
|
133
|
+
z.union([z.string(), z.number(), z.boolean(), z.null()])
|
|
134
|
+
)
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export { createTrackEventSchema, createVisitorSchema, getVisitor, setVisitor, setupAnalytics, track };
|
|
@@ -231,7 +231,7 @@ type AllowedPropertyValues = string | number | boolean | null;
|
|
|
231
231
|
type EventName = Lowercase<string>;
|
|
232
232
|
type CustomEventProperties = Record<Lowercase<string>, AllowedPropertyValues>;
|
|
233
233
|
type TrackProperties<T extends EventName = EventName> = T extends keyof StandardEventProperties ? StandardEventProperties[T] : CustomEventProperties;
|
|
234
|
-
type
|
|
234
|
+
type ThirdPartyTracker = <T extends EventName = EventName>(name: T, properties?: TrackProperties<T>, event_id?: string) => void;
|
|
235
235
|
interface PlatformInfo {
|
|
236
236
|
os?: string;
|
|
237
237
|
os_name?: string;
|
|
@@ -267,8 +267,19 @@ interface ThirdPartyFields {
|
|
|
267
267
|
*/
|
|
268
268
|
fbc?: string;
|
|
269
269
|
fbp?: string;
|
|
270
|
+
/**
|
|
271
|
+
* Google Analytics fields
|
|
272
|
+
*/
|
|
273
|
+
gclid?: string;
|
|
274
|
+
}
|
|
275
|
+
interface UTMParams {
|
|
276
|
+
utm_source?: string;
|
|
277
|
+
utm_medium?: string;
|
|
278
|
+
utm_campaign?: string;
|
|
279
|
+
utm_term?: string;
|
|
280
|
+
utm_content?: string;
|
|
270
281
|
}
|
|
271
|
-
interface TrackTags extends PlatformInfo, DeviceInfo, EnvironmentInfo, SourceInfo, ThirdPartyFields {
|
|
282
|
+
interface TrackTags extends PlatformInfo, DeviceInfo, EnvironmentInfo, SourceInfo, ThirdPartyFields, UTMParams {
|
|
272
283
|
[key: string]: string | undefined;
|
|
273
284
|
}
|
|
274
285
|
interface TrackEventResponse {
|
|
@@ -278,11 +289,12 @@ interface TrackEventResponse {
|
|
|
278
289
|
* */
|
|
279
290
|
id: string;
|
|
280
291
|
}
|
|
281
|
-
type VisitorProperties = Record<Lowercase<string>, AllowedPropertyValues
|
|
292
|
+
type VisitorProperties = Record<Lowercase<string>, AllowedPropertyValues>;
|
|
282
293
|
interface Visitor {
|
|
283
294
|
id: string;
|
|
284
295
|
device_id: string;
|
|
285
296
|
properties: VisitorProperties;
|
|
286
297
|
}
|
|
298
|
+
type ThirdPartyUserSetter = (properties: VisitorProperties) => void;
|
|
287
299
|
|
|
288
|
-
export type { EventName as E, TrackTags as T, Visitor as V,
|
|
300
|
+
export type { EventName as E, TrackTags as T, Visitor as V, ThirdPartyTracker as a, ThirdPartyUserSetter as b, TrackProperties as c, TrackEventResponse as d, VisitorProperties as e };
|
package/dist/web.d.mts
CHANGED
package/dist/web.mjs
CHANGED
|
@@ -11,6 +11,7 @@ function getDeviceId() {
|
|
|
11
11
|
}
|
|
12
12
|
function getTags(release) {
|
|
13
13
|
const parser = Bowser.getParser(window.navigator.userAgent);
|
|
14
|
+
const params = new URLSearchParams(window.location.search);
|
|
14
15
|
const os = parser.getOS();
|
|
15
16
|
const browser = parser.getBrowser();
|
|
16
17
|
const platform = parser.getPlatform();
|
|
@@ -34,7 +35,13 @@ function getTags(release) {
|
|
|
34
35
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
35
36
|
environment: process.env.NODE_ENV === "development" ? "development" : "production",
|
|
36
37
|
fbc: parsed._fbc,
|
|
37
|
-
fbp: parsed._fbp
|
|
38
|
+
fbp: parsed._fbp,
|
|
39
|
+
gclid: params.get("gclid") ?? void 0,
|
|
40
|
+
utm_source: params.get("utm_source") ?? void 0,
|
|
41
|
+
utm_medium: params.get("utm_medium") ?? void 0,
|
|
42
|
+
utm_campaign: params.get("utm_campaign") ?? void 0,
|
|
43
|
+
utm_term: params.get("utm_term") ?? void 0,
|
|
44
|
+
utm_content: params.get("utm_content") ?? void 0
|
|
38
45
|
};
|
|
39
46
|
return tags;
|
|
40
47
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shware/analytics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "tsc --watch",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"bowser": "^2.11.0",
|
|
23
23
|
"cookie": "^1.0.2",
|
|
24
24
|
"limiter": "^3.0.0",
|
|
25
|
-
"uuid": "^11.1.0"
|
|
25
|
+
"uuid": "^11.1.0",
|
|
26
|
+
"zod": "^3.24.2"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@types/node": "^22.13.10",
|