@armco/analytics 0.2.11 → 0.3.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.
Files changed (44) hide show
  1. package/core/analytics.d.ts +49 -0
  2. package/core/analytics.js +354 -0
  3. package/core/errors.d.ts +26 -0
  4. package/core/errors.js +54 -0
  5. package/core/types.d.ts +137 -0
  6. package/core/types.js +1 -0
  7. package/global-modules.d.ts +14 -0
  8. package/index.d.ts +24 -0
  9. package/index.js +24 -0
  10. package/package.json +6 -36
  11. package/plugins/auto-track/click.d.ts +15 -0
  12. package/plugins/auto-track/click.js +99 -0
  13. package/plugins/auto-track/error.d.ts +15 -0
  14. package/plugins/auto-track/error.js +65 -0
  15. package/plugins/auto-track/form.d.ts +13 -0
  16. package/plugins/auto-track/form.js +54 -0
  17. package/plugins/auto-track/page.d.ts +14 -0
  18. package/plugins/auto-track/page.js +72 -0
  19. package/plugins/enrichment/session.d.ts +18 -0
  20. package/plugins/enrichment/session.js +81 -0
  21. package/plugins/enrichment/user.d.ts +20 -0
  22. package/plugins/enrichment/user.js +159 -0
  23. package/plugins/node/http-request-tracking.d.ts +54 -0
  24. package/plugins/node/http-request-tracking.js +158 -0
  25. package/storage/cookie-storage.d.ts +8 -0
  26. package/storage/cookie-storage.js +60 -0
  27. package/storage/hybrid-storage.d.ts +12 -0
  28. package/storage/hybrid-storage.js +108 -0
  29. package/storage/local-storage.d.ts +8 -0
  30. package/storage/local-storage.js +65 -0
  31. package/storage/memory-storage.d.ts +9 -0
  32. package/storage/memory-storage.js +22 -0
  33. package/transport/beacon-transport.d.ts +16 -0
  34. package/transport/beacon-transport.js +50 -0
  35. package/transport/fetch-transport.d.ts +20 -0
  36. package/transport/fetch-transport.js +112 -0
  37. package/utils/config-loader.d.ts +6 -0
  38. package/utils/config-loader.js +168 -0
  39. package/utils/helpers.d.ts +15 -0
  40. package/utils/helpers.js +148 -0
  41. package/utils/logging.d.ts +17 -0
  42. package/utils/logging.js +54 -0
  43. package/utils/validation.d.ts +9 -0
  44. package/utils/validation.js +146 -0
@@ -0,0 +1,146 @@
1
+ import { z } from "zod";
2
+ import { ValidationError } from "../core/errors";
3
+ const configSchema = z.object({
4
+ apiKey: z.string().optional(),
5
+ endpoint: z.string().url().optional(),
6
+ hostProjectName: z.string().optional(),
7
+ trackEvents: z.array(z.string()).optional(),
8
+ submissionStrategy: z.enum(["ONEVENT", "DEFER"]).optional(),
9
+ showConsentPopup: z.boolean().optional(),
10
+ logLevel: z.enum(["debug", "info", "warn", "error", "none"]).optional(),
11
+ samplingRate: z.number().min(0).max(1).optional(),
12
+ enableLocation: z.boolean().optional(),
13
+ enableAutoTrack: z.boolean().optional(),
14
+ respectDoNotTrack: z.boolean().optional(),
15
+ batchSize: z.number().positive().optional(),
16
+ flushInterval: z.number().positive().optional(),
17
+ maxRetries: z.number().nonnegative().optional(),
18
+ retryDelay: z.number().positive().optional(),
19
+ });
20
+ const userSchema = z.object({
21
+ email: z.string().email("Invalid email address"),
22
+ id: z.string().optional(),
23
+ name: z.string().optional(),
24
+ }).passthrough();
25
+ const pageViewSchema = z.object({
26
+ pageName: z.string().min(1, "Page name is required"),
27
+ url: z.string().url("Invalid URL"),
28
+ referrer: z.string().optional(),
29
+ title: z.string().optional(),
30
+ }).passthrough();
31
+ const clickEventSchema = z.object({
32
+ elementType: z.string().min(1, "Element type is required"),
33
+ elementId: z.string().optional(),
34
+ elementText: z.string().optional(),
35
+ elementClasses: z.array(z.string()).optional(),
36
+ elementPath: z.string().optional(),
37
+ href: z.string().optional(),
38
+ value: z.string().optional(),
39
+ }).passthrough();
40
+ const formEventSchema = z.object({
41
+ formId: z.string().optional(),
42
+ formName: z.string().optional(),
43
+ formAction: z.string().optional(),
44
+ formMethod: z.string().optional(),
45
+ }).passthrough();
46
+ const errorEventSchema = z.object({
47
+ errorMessage: z.string().min(1, "Error message is required"),
48
+ errorStack: z.string().optional(),
49
+ errorType: z.string().optional(),
50
+ }).passthrough();
51
+ export function validateConfig(config) {
52
+ try {
53
+ return configSchema.parse(config);
54
+ }
55
+ catch (error) {
56
+ if (error instanceof z.ZodError) {
57
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
58
+ throw new ValidationError(`Configuration validation failed: ${messages.join(", ")}`);
59
+ }
60
+ throw error;
61
+ }
62
+ }
63
+ export function validateUser(user) {
64
+ try {
65
+ return userSchema.parse(user);
66
+ }
67
+ catch (error) {
68
+ if (error instanceof z.ZodError) {
69
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
70
+ throw new ValidationError(`User validation failed: ${messages.join(", ")}`);
71
+ }
72
+ throw error;
73
+ }
74
+ }
75
+ export function validatePageView(data) {
76
+ try {
77
+ return pageViewSchema.parse(data);
78
+ }
79
+ catch (error) {
80
+ if (error instanceof z.ZodError) {
81
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
82
+ throw new ValidationError(`Page view validation failed: ${messages.join(", ")}`);
83
+ }
84
+ throw error;
85
+ }
86
+ }
87
+ export function validateClickEvent(data) {
88
+ try {
89
+ return clickEventSchema.parse(data);
90
+ }
91
+ catch (error) {
92
+ if (error instanceof z.ZodError) {
93
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
94
+ throw new ValidationError(`Click event validation failed: ${messages.join(", ")}`);
95
+ }
96
+ throw error;
97
+ }
98
+ }
99
+ export function validateFormEvent(data) {
100
+ try {
101
+ return formEventSchema.parse(data);
102
+ }
103
+ catch (error) {
104
+ if (error instanceof z.ZodError) {
105
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
106
+ throw new ValidationError(`Form event validation failed: ${messages.join(", ")}`);
107
+ }
108
+ throw error;
109
+ }
110
+ }
111
+ export function validateErrorEvent(data) {
112
+ try {
113
+ return errorEventSchema.parse(data);
114
+ }
115
+ catch (error) {
116
+ if (error instanceof z.ZodError) {
117
+ const messages = error.issues.map((err) => `${err.path.join(".")}: ${err.message}`);
118
+ throw new ValidationError(`Error event validation failed: ${messages.join(", ")}`);
119
+ }
120
+ throw error;
121
+ }
122
+ }
123
+ export function validateEventType(eventType) {
124
+ if (typeof eventType !== "string" || eventType.trim().length === 0) {
125
+ throw new ValidationError("Event type must be a non-empty string");
126
+ }
127
+ return eventType.trim();
128
+ }
129
+ export function sanitizeEventData(data) {
130
+ const sanitized = {};
131
+ for (const [key, value] of Object.entries(data)) {
132
+ if (typeof value === "function" || value === undefined) {
133
+ continue;
134
+ }
135
+ if (typeof value === "string") {
136
+ sanitized[key] = value.trim();
137
+ }
138
+ else if (Array.isArray(value)) {
139
+ sanitized[key] = value.map((item) => typeof item === "string" ? item.trim() : item);
140
+ }
141
+ else {
142
+ sanitized[key] = value;
143
+ }
144
+ }
145
+ return sanitized;
146
+ }