@shware/analytics 0.1.15 → 0.1.17

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 (55) hide show
  1. package/dist/index.cjs +17 -182
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +7 -501
  4. package/dist/index.d.ts +7 -501
  5. package/dist/index.mjs +5 -158
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/next/index.cjs +3 -3
  8. package/dist/next/index.cjs.map +1 -1
  9. package/dist/next/index.mjs +1 -1
  10. package/dist/next/index.mjs.map +1 -1
  11. package/dist/react-router/index.cjs +3 -3
  12. package/dist/react-router/index.cjs.map +1 -1
  13. package/dist/react-router/index.mjs +1 -1
  14. package/dist/react-router/index.mjs.map +1 -1
  15. package/dist/schema/index.cjs +85 -0
  16. package/dist/schema/index.cjs.map +1 -0
  17. package/dist/schema/index.d.cts +177 -0
  18. package/dist/schema/index.d.ts +177 -0
  19. package/dist/schema/index.mjs +58 -0
  20. package/dist/schema/index.mjs.map +1 -0
  21. package/dist/setup/index.cjs +63 -0
  22. package/dist/setup/index.cjs.map +1 -0
  23. package/dist/setup/index.d.cts +29 -0
  24. package/dist/setup/index.d.ts +29 -0
  25. package/dist/setup/index.mjs +27 -0
  26. package/dist/setup/index.mjs.map +1 -0
  27. package/dist/track/ga.cjs +102 -0
  28. package/dist/track/ga.cjs.map +1 -0
  29. package/dist/track/ga.d.cts +4 -0
  30. package/dist/track/ga.d.ts +4 -0
  31. package/dist/track/ga.mjs +76 -0
  32. package/dist/track/ga.mjs.map +1 -0
  33. package/dist/track/index.cjs +65 -0
  34. package/dist/track/index.cjs.map +1 -0
  35. package/dist/track/index.d.cts +10 -0
  36. package/dist/track/index.d.ts +10 -0
  37. package/dist/track/index.mjs +40 -0
  38. package/dist/track/index.mjs.map +1 -0
  39. package/dist/types/index.cjs +19 -0
  40. package/dist/types/index.cjs.map +1 -0
  41. package/dist/types/index.d.cts +327 -0
  42. package/dist/types/index.d.ts +327 -0
  43. package/dist/types/index.mjs +1 -0
  44. package/dist/types/index.mjs.map +1 -0
  45. package/dist/visitor/index.cjs +77 -0
  46. package/dist/visitor/index.cjs.map +1 -0
  47. package/dist/visitor/index.d.cts +6 -0
  48. package/dist/visitor/index.d.ts +6 -0
  49. package/dist/visitor/index.mjs +51 -0
  50. package/dist/visitor/index.mjs.map +1 -0
  51. package/dist/web/index.cjs.map +1 -1
  52. package/dist/web/index.d.cts +1 -50
  53. package/dist/web/index.d.ts +1 -50
  54. package/dist/web/index.mjs.map +1 -1
  55. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -1,501 +1,7 @@
1
- import { z } from 'zod';
2
-
3
- interface Item {
4
- item_brand?: string;
5
- item_id?: string;
6
- item_name?: string;
7
- item_category?: string;
8
- item_category2?: string;
9
- item_category3?: string;
10
- item_category4?: string;
11
- item_category5?: string;
12
- item_list_id?: string;
13
- item_list_name?: string;
14
- item_location_id?: string;
15
- item_variant?: string;
16
- quantity?: number;
17
- price?: number;
18
- }
19
- /**
20
- * ref: https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag
21
- * ref: https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event
22
- * */
23
- type StandardEventProperties = {
24
- add_payment_info: {
25
- items?: Item[];
26
- currency?: string;
27
- value?: number;
28
- coupon?: string;
29
- payment_type?: string;
30
- };
31
- add_shipping_info: {
32
- items?: Item[];
33
- currency?: string;
34
- value?: number;
35
- coupon?: string;
36
- shipping_tier?: string;
37
- };
38
- add_to_cart: {
39
- items?: Item[];
40
- currency?: string;
41
- value?: number;
42
- };
43
- add_to_wishlist: {
44
- items?: Item[];
45
- currency?: string;
46
- value?: number;
47
- };
48
- begin_checkout: {
49
- currency?: string;
50
- value?: number;
51
- coupon?: string;
52
- items?: Item[];
53
- [key: string]: any;
54
- };
55
- close_convert_lead: {
56
- currency: string;
57
- value: number;
58
- };
59
- close_unconvert_lead: {
60
- currency: string;
61
- value: number;
62
- unconvert_lead_reason?: string;
63
- };
64
- disqualify_lead: {
65
- currency: string;
66
- value: number;
67
- disqualified_lead_reason?: string;
68
- };
69
- earn_virtual_currency: {
70
- virtual_currency_name: string;
71
- value: number;
72
- };
73
- generate_lead: {
74
- currency?: string;
75
- value?: number;
76
- };
77
- join_group: {
78
- group_id: string;
79
- };
80
- level_end: {
81
- level: number;
82
- success?: string;
83
- };
84
- level_start: {
85
- level: number;
86
- };
87
- level_up: {
88
- level: number;
89
- character?: string;
90
- };
91
- login: {
92
- method: string;
93
- };
94
- post_score: {
95
- score: number;
96
- level?: number;
97
- character?: string;
98
- };
99
- purchase: {
100
- affiliation?: string;
101
- coupon?: string;
102
- currency?: string;
103
- items?: Item[];
104
- shipping?: number;
105
- tax?: number;
106
- value?: number;
107
- transaction_id?: string;
108
- [key: string]: any;
109
- };
110
- qualify_lead: {
111
- currency: string;
112
- value: number;
113
- };
114
- refund: {
115
- affiliation?: string;
116
- coupon?: string;
117
- currency?: string;
118
- items?: Item[];
119
- shipping?: number;
120
- tax?: number;
121
- value?: number;
122
- transaction_id?: string;
123
- };
124
- remove_from_cart: {
125
- items?: Item[];
126
- value?: number;
127
- currency?: string;
128
- };
129
- search: {
130
- search_term: string;
131
- number_of_nights?: number;
132
- number_of_rooms?: number;
133
- number_of_passengers?: number;
134
- origin?: string;
135
- destination?: string;
136
- start_date?: string;
137
- end_date?: string;
138
- travel_class?: string;
139
- };
140
- select_content: {
141
- content_type: string;
142
- item_id: string;
143
- };
144
- select_item: {
145
- items?: Item[];
146
- content_type: string;
147
- item_list_id: string;
148
- item_list_name: string;
149
- };
150
- select_promotion: {
151
- creative_name: string;
152
- creative_slot: string;
153
- items?: Item[];
154
- location_id: string;
155
- promotion_id: string;
156
- promotion_name: string;
157
- };
158
- share: {
159
- content_type: string;
160
- item_id: string;
161
- method: string;
162
- activity_type?: string | null;
163
- post_id?: string;
164
- };
165
- sign_up: {
166
- method: string;
167
- };
168
- spend_virtual_currency: {
169
- item_name: string;
170
- virtual_currency_name: string;
171
- value: number;
172
- };
173
- tutorial_begin: undefined;
174
- tutorial_complete: undefined;
175
- unlock_achievement: {
176
- achievement_id: string;
177
- };
178
- view_cart: {
179
- items?: Item[];
180
- currency?: string;
181
- value?: number;
182
- };
183
- view_item: {
184
- items?: Item[];
185
- currency?: string;
186
- value?: number;
187
- };
188
- view_item_list: {
189
- items?: Item[];
190
- item_list_id?: string;
191
- item_list_name?: string;
192
- };
193
- view_promotion: {
194
- items?: Item[];
195
- location_id?: string;
196
- creative_name?: string;
197
- creative_slot?: string;
198
- promotion_id?: string;
199
- promotion_name?: string;
200
- };
201
- working_lead: {
202
- currency: string;
203
- value: number;
204
- lead_status?: string;
205
- };
206
- ad_impression: {
207
- value?: number;
208
- currency?: string;
209
- ad_format?: string;
210
- ad_platform?: string;
211
- ad_source?: string;
212
- ad_unit_name?: string;
213
- };
214
- app_open: undefined;
215
- campaign_details: {
216
- source: string;
217
- medium: string;
218
- campaign: string;
219
- term?: string;
220
- content?: string;
221
- aclid?: string;
222
- cp1?: string;
223
- };
224
- screen_view: {
225
- screen_name?: string;
226
- screen_class?: string;
227
- };
228
- view_search_results: {
229
- search_term: string;
230
- };
231
- };
232
- type AllowedPropertyValues = string | number | boolean | null;
233
- type EventName = Lowercase<string>;
234
- type CustomEventProperties = Record<Lowercase<string>, AllowedPropertyValues>;
235
- type TrackProperties<T extends EventName = EventName> = T extends keyof StandardEventProperties ? StandardEventProperties[T] : CustomEventProperties;
236
- type ThirdPartyTracker = <T extends EventName = EventName>(name: T, properties?: TrackProperties<T>, event_id?: string) => void;
237
- interface PlatformInfo {
238
- os?: string;
239
- os_name?: string;
240
- os_version?: string;
241
- browser?: string;
242
- browser_name?: string;
243
- browser_version?: string;
244
- platform?: 'ios' | 'android' | 'web' | 'macos' | 'windows' | 'linux' | 'unknown';
245
- }
246
- interface DeviceInfo {
247
- device?: string;
248
- device_id?: string;
249
- device_type?: string;
250
- device_vendor?: string;
251
- device_pixel_ratio?: string;
252
- screen_resolution?: `${number}x${number}`;
253
- }
254
- interface EnvironmentInfo {
255
- release?: string;
256
- language?: string;
257
- timezone?: string;
258
- environment?: 'development' | 'production';
259
- }
260
- interface SourceInfo {
261
- source_url?: string;
262
- source?: 'app' | 'web' | 'offline';
263
- }
264
- interface ThirdPartyFields {
265
- /**
266
- * Meta pixel fields
267
- * ref: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters#fbc
268
- * Stored in the _fbc/_fbp browser cookie under your domain
269
- */
270
- fbc?: string;
271
- fbp?: string;
272
- /**
273
- * Google Analytics fields
274
- */
275
- gclid?: string;
276
- }
277
- interface UTMParams {
278
- utm_source?: string;
279
- utm_medium?: string;
280
- utm_campaign?: string;
281
- utm_term?: string;
282
- utm_content?: string;
283
- }
284
- interface TrackTags extends PlatformInfo, DeviceInfo, EnvironmentInfo, SourceInfo, ThirdPartyFields, UTMParams {
285
- [key: string]: string | undefined;
286
- }
287
- interface TrackEventResponse {
288
- /**
289
- * track event id
290
- * some tracking system will use event_id and event_name for deduplication
291
- * */
292
- id: string;
293
- }
294
- type VisitorProperties = Record<Lowercase<string>, AllowedPropertyValues>;
295
- interface Visitor {
296
- id: string;
297
- device_id: string;
298
- properties: VisitorProperties;
299
- }
300
- type ThirdPartyUserSetter = (properties: VisitorProperties) => void;
301
-
302
- interface Storage {
303
- getItem: (key: string) => (string | null) | Promise<string | null>;
304
- setItem: (key: string, value: string) => void | Promise<void>;
305
- }
306
- interface Options {
307
- release: string;
308
- storage: Storage;
309
- endpoint: string;
310
- getTags: () => TrackTags | Promise<TrackTags>;
311
- getDeviceId: () => string | Promise<string>;
312
- thirdPartyTrackers?: ThirdPartyTracker[];
313
- thirdPartyUserSetters?: ThirdPartyUserSetter[];
314
- }
315
- declare function setupAnalytics(init: Options): void;
316
-
317
- interface TrackOptions {
318
- enableThirdPartyTracking?: boolean;
319
- onSucceed?: (response?: TrackEventResponse) => void;
320
- onError?: (error: unknown) => void;
321
- }
322
- declare function track<T extends EventName = EventName>(name: T, properties?: TrackProperties<T>, trackOptions?: TrackOptions): void;
323
-
324
- declare function getVisitor(): Promise<Visitor>;
325
- declare function setVisitor(properties: VisitorProperties): Promise<Visitor>;
326
-
327
- declare const createTrackEventSchema: z.ZodObject<{
328
- name: z.ZodString;
329
- visitor_id: z.ZodBigInt;
330
- tags: z.ZodObject<{
331
- os: z.ZodOptional<z.ZodString>;
332
- os_name: z.ZodOptional<z.ZodString>;
333
- os_version: z.ZodOptional<z.ZodString>;
334
- browser: z.ZodOptional<z.ZodString>;
335
- browser_name: z.ZodOptional<z.ZodString>;
336
- browser_version: z.ZodOptional<z.ZodString>;
337
- platform: z.ZodOptional<z.ZodEnum<["ios", "android", "web", "macos", "windows", "linux", "unknown"]>>;
338
- device: z.ZodOptional<z.ZodString>;
339
- device_id: z.ZodOptional<z.ZodString>;
340
- device_type: z.ZodOptional<z.ZodString>;
341
- device_vendor: z.ZodOptional<z.ZodString>;
342
- device_pixel_ratio: z.ZodOptional<z.ZodString>;
343
- screen_resolution: z.ZodOptional<z.ZodEffects<z.ZodString, `${number}x${number}`, string>>;
344
- release: z.ZodOptional<z.ZodString>;
345
- language: z.ZodOptional<z.ZodString>;
346
- timezone: z.ZodOptional<z.ZodString>;
347
- environment: z.ZodOptional<z.ZodEnum<["development", "production"]>>;
348
- source_url: z.ZodOptional<z.ZodString>;
349
- source: z.ZodOptional<z.ZodEnum<["web", "app", "offline"]>>;
350
- fbc: z.ZodOptional<z.ZodString>;
351
- fbp: z.ZodOptional<z.ZodString>;
352
- gclid: z.ZodOptional<z.ZodString>;
353
- utm_source: z.ZodOptional<z.ZodString>;
354
- utm_medium: z.ZodOptional<z.ZodString>;
355
- utm_campaign: z.ZodOptional<z.ZodString>;
356
- utm_term: z.ZodOptional<z.ZodString>;
357
- utm_content: z.ZodOptional<z.ZodString>;
358
- }, "strip", z.ZodTypeAny, {
359
- os?: string | undefined;
360
- os_name?: string | undefined;
361
- os_version?: string | undefined;
362
- browser?: string | undefined;
363
- browser_name?: string | undefined;
364
- browser_version?: string | undefined;
365
- platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
366
- device?: string | undefined;
367
- device_id?: string | undefined;
368
- device_type?: string | undefined;
369
- device_vendor?: string | undefined;
370
- device_pixel_ratio?: string | undefined;
371
- screen_resolution?: `${number}x${number}` | undefined;
372
- release?: string | undefined;
373
- language?: string | undefined;
374
- timezone?: string | undefined;
375
- environment?: "development" | "production" | undefined;
376
- source_url?: string | undefined;
377
- source?: "web" | "app" | "offline" | undefined;
378
- fbc?: string | undefined;
379
- fbp?: string | undefined;
380
- gclid?: string | undefined;
381
- utm_source?: string | undefined;
382
- utm_medium?: string | undefined;
383
- utm_campaign?: string | undefined;
384
- utm_term?: string | undefined;
385
- utm_content?: string | undefined;
386
- }, {
387
- os?: string | undefined;
388
- os_name?: string | undefined;
389
- os_version?: string | undefined;
390
- browser?: string | undefined;
391
- browser_name?: string | undefined;
392
- browser_version?: string | undefined;
393
- platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
394
- device?: string | undefined;
395
- device_id?: string | undefined;
396
- device_type?: string | undefined;
397
- device_vendor?: string | undefined;
398
- device_pixel_ratio?: string | undefined;
399
- screen_resolution?: string | undefined;
400
- release?: string | undefined;
401
- language?: string | undefined;
402
- timezone?: string | undefined;
403
- environment?: "development" | "production" | undefined;
404
- source_url?: string | undefined;
405
- source?: "web" | "app" | "offline" | undefined;
406
- fbc?: string | undefined;
407
- fbp?: string | undefined;
408
- gclid?: string | undefined;
409
- utm_source?: string | undefined;
410
- utm_medium?: string | undefined;
411
- utm_campaign?: string | undefined;
412
- utm_term?: string | undefined;
413
- utm_content?: string | undefined;
414
- }>;
415
- properties: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>, Record<string, string | number | boolean | null>, Record<string, string | number | boolean | null>>;
416
- }, "strip", z.ZodTypeAny, {
417
- visitor_id: bigint;
418
- name: string;
419
- tags: {
420
- os?: string | undefined;
421
- os_name?: string | undefined;
422
- os_version?: string | undefined;
423
- browser?: string | undefined;
424
- browser_name?: string | undefined;
425
- browser_version?: string | undefined;
426
- platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
427
- device?: string | undefined;
428
- device_id?: string | undefined;
429
- device_type?: string | undefined;
430
- device_vendor?: string | undefined;
431
- device_pixel_ratio?: string | undefined;
432
- screen_resolution?: `${number}x${number}` | undefined;
433
- release?: string | undefined;
434
- language?: string | undefined;
435
- timezone?: string | undefined;
436
- environment?: "development" | "production" | undefined;
437
- source_url?: string | undefined;
438
- source?: "web" | "app" | "offline" | undefined;
439
- fbc?: string | undefined;
440
- fbp?: string | undefined;
441
- gclid?: string | undefined;
442
- utm_source?: string | undefined;
443
- utm_medium?: string | undefined;
444
- utm_campaign?: string | undefined;
445
- utm_term?: string | undefined;
446
- utm_content?: string | undefined;
447
- };
448
- properties: Record<string, string | number | boolean | null>;
449
- }, {
450
- visitor_id: bigint;
451
- name: string;
452
- tags: {
453
- os?: string | undefined;
454
- os_name?: string | undefined;
455
- os_version?: string | undefined;
456
- browser?: string | undefined;
457
- browser_name?: string | undefined;
458
- browser_version?: string | undefined;
459
- platform?: "ios" | "android" | "web" | "macos" | "windows" | "linux" | "unknown" | undefined;
460
- device?: string | undefined;
461
- device_id?: string | undefined;
462
- device_type?: string | undefined;
463
- device_vendor?: string | undefined;
464
- device_pixel_ratio?: string | undefined;
465
- screen_resolution?: string | undefined;
466
- release?: string | undefined;
467
- language?: string | undefined;
468
- timezone?: string | undefined;
469
- environment?: "development" | "production" | undefined;
470
- source_url?: string | undefined;
471
- source?: "web" | "app" | "offline" | undefined;
472
- fbc?: string | undefined;
473
- fbp?: string | undefined;
474
- gclid?: string | undefined;
475
- utm_source?: string | undefined;
476
- utm_medium?: string | undefined;
477
- utm_campaign?: string | undefined;
478
- utm_term?: string | undefined;
479
- utm_content?: string | undefined;
480
- };
481
- properties: Record<string, string | number | boolean | null>;
482
- }>;
483
- declare const createVisitorSchema: z.ZodObject<{
484
- device_id: z.ZodString;
485
- properties: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>, Record<string, string | number | boolean | null>, Record<string, string | number | boolean | null>>;
486
- }, "strip", z.ZodTypeAny, {
487
- device_id: string;
488
- properties: Record<string, string | number | boolean | null>;
489
- }, {
490
- device_id: string;
491
- properties: Record<string, string | number | boolean | null>;
492
- }>;
493
- declare const updateVisitorSchema: z.ZodObject<{
494
- properties: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>, Record<string, string | number | boolean | null>, Record<string, string | number | boolean | null>>;
495
- }, "strip", z.ZodTypeAny, {
496
- properties: Record<string, string | number | boolean | null>;
497
- }, {
498
- properties: Record<string, string | number | boolean | null>;
499
- }>;
500
-
501
- export { type TrackProperties, type TrackTags, type VisitorProperties, createTrackEventSchema, createVisitorSchema, getVisitor, setVisitor, setupAnalytics, track, updateVisitorSchema };
1
+ export { setupAnalytics } from './setup/index.js';
2
+ export { track } from './track/index.js';
3
+ export { getVisitor, setVisitor } from './visitor/index.js';
4
+ export { createTrackEventSchema, createVisitorSchema, updateVisitorSchema } from './schema/index.js';
5
+ export { TrackProperties, TrackTags, VisitorProperties } from './types/index.js';
6
+ import 'axios';
7
+ import 'zod';
package/dist/index.mjs CHANGED
@@ -1,161 +1,8 @@
1
- // src/setup.ts
2
- import axios from "axios";
3
- import retry from "axios-retry";
4
- var config = {
5
- http: null,
6
- release: "0.0.0",
7
- storage: null,
8
- getTags: null,
9
- getDeviceId: null,
10
- thirdPartyTrackers: [],
11
- thirdPartyUserSetters: []
12
- };
13
- function setupAnalytics(init) {
14
- config.release = init.release;
15
- config.storage = init.storage;
16
- config.getTags = init.getTags;
17
- config.getDeviceId = init.getDeviceId;
18
- config.thirdPartyTrackers = init.thirdPartyTrackers ?? [];
19
- config.thirdPartyUserSetters = init.thirdPartyUserSetters ?? [];
20
- config.http = axios.create({ baseURL: init.endpoint, withCredentials: true, adapter: "fetch" });
21
- retry(config.http, { retries: 5, retryDelay: retry.exponentialDelay });
22
- }
23
-
24
- // src/track.ts
25
- import { TokenBucket } from "limiter";
26
-
27
- // src/visitor.ts
28
- var key = "visitor_id";
29
- async function createVisitor() {
30
- const dto = {
31
- device_id: await config.getDeviceId(),
32
- properties: await config.getTags()
33
- };
34
- const response = await config.http.post(`/visitors`, dto);
35
- return response.data;
36
- }
37
- async function getOrCreateVisitor() {
38
- const visitorId = await config.storage.getItem(key);
39
- if (visitorId) {
40
- try {
41
- const response = await config.http.get(`/visitors/${visitorId}`);
42
- return response.data;
43
- } catch (e) {
44
- const visitor2 = await createVisitor();
45
- await config.storage.setItem(key, visitor2.id);
46
- return visitor2;
47
- }
48
- } else {
49
- const visitor2 = await createVisitor();
50
- await config.storage.setItem(key, visitor2.id);
51
- return visitor2;
52
- }
53
- }
54
- var visitor = null;
55
- var visitorFetcher = null;
56
- async function getVisitor() {
57
- if (visitor) return visitor;
58
- if (visitorFetcher) return visitorFetcher;
59
- visitorFetcher = getOrCreateVisitor();
60
- visitor = await visitorFetcher;
61
- visitorFetcher = null;
62
- return visitor;
63
- }
64
- async function setVisitor(properties) {
65
- const dto = { properties };
66
- const { id } = await getVisitor();
67
- const response = await config.http.patch(`/visitors/${id}`, dto);
68
- config.thirdPartyUserSetters.forEach((setter) => setter(properties));
69
- visitor = response.data;
70
- return response.data;
71
- }
72
-
73
- // src/track.ts
74
- var defaultOptions = { enableThirdPartyTracking: true };
75
- var REQUEST_TOKENS = 2;
76
- var tokenBucket = new TokenBucket({
77
- bucketSize: 20,
78
- interval: "second",
79
- tokensPerInterval: 1
80
- });
81
- async function trackAsync(name, properties, trackOptions = defaultOptions) {
82
- var _a, _b;
83
- try {
84
- await tokenBucket.removeTokens(REQUEST_TOKENS);
85
- const dto = {
86
- name,
87
- properties,
88
- tags: await config.getTags(),
89
- visitor_id: (await getVisitor()).id,
90
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
91
- };
92
- const { data } = await config.http.post(`/events`, dto);
93
- if (!trackOptions.enableThirdPartyTracking || !config.thirdPartyTrackers) return;
94
- config.thirdPartyTrackers.forEach((tracker) => tracker(name, properties, data.id));
95
- (_a = trackOptions.onSucceed) == null ? void 0 : _a.call(trackOptions, data);
96
- } catch (e) {
97
- if (e instanceof Error) {
98
- console.log("Failed to send track event:", e.message);
99
- }
100
- (_b = trackOptions.onError) == null ? void 0 : _b.call(trackOptions, e);
101
- }
102
- }
103
- function track(name, properties, trackOptions = defaultOptions) {
104
- trackAsync(name, properties, trackOptions).catch(console.error);
105
- }
106
-
107
- // src/schema.ts
108
- import { z } from "zod";
109
- var createTrackEventSchema = z.object({
110
- name: z.string().trim().min(1).max(64),
111
- visitor_id: z.coerce.bigint(),
112
- tags: z.object({
113
- os: z.string().optional(),
114
- os_name: z.string().optional(),
115
- os_version: z.string().optional(),
116
- browser: z.string().optional(),
117
- browser_name: z.string().optional(),
118
- browser_version: z.string().optional(),
119
- platform: z.enum(["ios", "android", "web", "macos", "windows", "linux", "unknown"]).optional(),
120
- device: z.string().optional(),
121
- device_id: z.string().trim().min(1).max(36).optional(),
122
- device_type: z.string().optional(),
123
- device_vendor: z.string().optional(),
124
- device_pixel_ratio: z.string().optional(),
125
- screen_resolution: z.string().regex(/^\d+x\d+$/).transform((v) => v).optional(),
126
- release: z.string().optional(),
127
- language: z.string().optional(),
128
- timezone: z.string().optional(),
129
- environment: z.enum(["development", "production"]).optional(),
130
- source_url: z.string().optional(),
131
- source: z.enum(["web", "app", "offline"]).optional(),
132
- fbc: z.string().optional(),
133
- fbp: z.string().optional(),
134
- gclid: z.string().optional(),
135
- utm_source: z.string().optional(),
136
- utm_medium: z.string().optional(),
137
- utm_campaign: z.string().optional(),
138
- utm_term: z.string().optional(),
139
- utm_content: z.string().optional()
140
- }),
141
- properties: z.record(
142
- z.string().trim().min(1).max(128),
143
- z.union([z.string().max(512), z.number(), z.boolean(), z.null()])
144
- ).refine((data) => Object.keys(data).length <= 64)
145
- });
146
- var createVisitorSchema = z.object({
147
- device_id: z.string().trim().min(1).max(36),
148
- properties: z.record(
149
- z.string().trim().min(1).max(128),
150
- z.union([z.string().max(512), z.number(), z.boolean(), z.null()])
151
- ).refine((data) => Object.keys(data).length <= 64)
152
- });
153
- var updateVisitorSchema = z.object({
154
- properties: z.record(
155
- z.string().trim().min(1).max(128),
156
- z.union([z.string().max(512), z.number(), z.boolean(), z.null()])
157
- ).refine((data) => Object.keys(data).length <= 64)
158
- });
1
+ // src/index.ts
2
+ import { setupAnalytics } from "./setup/index.mjs";
3
+ import { track } from "./track/index.mjs";
4
+ import { getVisitor, setVisitor } from "./visitor/index.mjs";
5
+ import { createTrackEventSchema, createVisitorSchema, updateVisitorSchema } from "./schema/index.mjs";
159
6
  export {
160
7
  createTrackEventSchema,
161
8
  createVisitorSchema,