analyzer-analytics 0.1.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.
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @youranalytics/web-sdk Storage Utilities
3
+ * Safe browser storage with fallbacks for edge cases
4
+ */
5
+ import { type VisitorId, type SessionId } from './types';
6
+ type StorageType = 'localStorage' | 'sessionStorage';
7
+ /**
8
+ * Test if a storage type is available and working.
9
+ * Result is cached after first check.
10
+ *
11
+ * @param type - The storage type to check
12
+ * @returns true if storage is available and functional
13
+ */
14
+ export declare function isStorageAvailable(type: StorageType): boolean;
15
+ /**
16
+ * Reset storage availability cache.
17
+ * Useful for testing or when storage state might have changed.
18
+ */
19
+ export declare function resetStorageCache(): void;
20
+ /**
21
+ * Safely get item from localStorage.
22
+ *
23
+ * @param key - Storage key
24
+ * @returns Value or null if not found/error
25
+ */
26
+ export declare function getItem(key: string): string | null;
27
+ /**
28
+ * Safely set item in localStorage.
29
+ *
30
+ * @param key - Storage key
31
+ * @param value - Value to store
32
+ * @returns true if successful, false if failed
33
+ */
34
+ export declare function setItem(key: string, value: string): boolean;
35
+ /**
36
+ * Safely get item from sessionStorage.
37
+ *
38
+ * @param key - Storage key
39
+ * @returns Value or null if not found/error
40
+ */
41
+ export declare function getSessionItem(key: string): string | null;
42
+ /**
43
+ * Safely set item in sessionStorage.
44
+ *
45
+ * @param key - Storage key
46
+ * @param value - Value to store
47
+ * @returns true if successful, false if failed
48
+ */
49
+ export declare function setSessionItem(key: string, value: string): boolean;
50
+ /**
51
+ * Generate a unique ID using crypto.randomUUID or fallback.
52
+ * Format: timestamp_randomString
53
+ * Example: "1704067200000_x7k9m2p" or "1704067200000_a1b2c3d4-e5f6-..."
54
+ *
55
+ * @returns Unique identifier string
56
+ */
57
+ export declare function generateId(): string;
58
+ /**
59
+ * Get or create a visitor ID that persists across sessions.
60
+ * Uses localStorage with fallbacks to sessionStorage and memory.
61
+ *
62
+ * @returns Branded VisitorId
63
+ */
64
+ export declare function getVisitorId(): VisitorId;
65
+ /**
66
+ * Clear the cached visitor ID.
67
+ * Useful for testing or when user requests data deletion.
68
+ */
69
+ export declare function clearVisitorId(): void;
70
+ /**
71
+ * Get or create a session ID that resets when tab/window closes.
72
+ * Uses sessionStorage with fallback to memory.
73
+ *
74
+ * @returns Branded SessionId
75
+ */
76
+ export declare function getSessionId(): SessionId;
77
+ /**
78
+ * Clear the cached session ID.
79
+ * Useful for testing or forcing a new session.
80
+ */
81
+ export declare function clearSessionId(): void;
82
+ /** Storage keys exported for testing */
83
+ export declare const STORAGE_KEYS: {
84
+ readonly VISITOR_ID: "_analytics_vid";
85
+ readonly SESSION_ID: "_analytics_sid";
86
+ };
87
+ export {};
88
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAoC,KAAK,SAAS,EAAE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AA0D3F,KAAK,WAAW,GAAG,cAAc,GAAG,gBAAgB,CAAC;AAQrD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CA0B7D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAIxC;AAMD;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAalD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAe3D;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAazD;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAelE;AAMD;;;;;;GAMG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAenC;AAwCD;;;;;GAKG;AACH,wBAAgB,YAAY,IAAI,SAAS,CAqDxC;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAsBrC;AASD;;;;;GAKG;AACH,wBAAgB,YAAY,IAAI,SAAS,CA6BxC;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAarC;AAMD,wCAAwC;AACxC,eAAO,MAAM,YAAY;;;CAGf,CAAC"}
@@ -0,0 +1,197 @@
1
+ /**
2
+ * @youranalytics/web-sdk Type Definitions
3
+ * Comprehensive types for the analytics SDK
4
+ */
5
+ /**
6
+ * Unique visitor identifier that persists across sessions.
7
+ * Branded type prevents mixing with other string IDs.
8
+ */
9
+ export type VisitorId = string & {
10
+ readonly __brand: 'VisitorId';
11
+ };
12
+ /**
13
+ * Session identifier that resets on new browser session.
14
+ * Branded type prevents mixing with other string IDs.
15
+ */
16
+ export type SessionId = string & {
17
+ readonly __brand: 'SessionId';
18
+ };
19
+ /**
20
+ * Analytics SDK configuration options.
21
+ * Only `apiKey` is required; all other options have sensible defaults.
22
+ */
23
+ export type AnalyticsConfig = {
24
+ /** API key from analytics dashboard (required) */
25
+ readonly apiKey: string;
26
+ /** Analytics server endpoint */
27
+ readonly endpoint?: string;
28
+ /** Enable debug logging to console */
29
+ readonly debug?: boolean;
30
+ /** Max events before auto-flush */
31
+ readonly maxQueueSize?: number;
32
+ /** Flush interval in milliseconds */
33
+ readonly flushInterval?: number;
34
+ /** Auto-track page views on initialization and navigation */
35
+ readonly autoTrack?: boolean;
36
+ /** Enable adaptive network behavior (adjusts batching based on connection quality) */
37
+ readonly adaptiveNetwork?: boolean;
38
+ };
39
+ /**
40
+ * Resolved configuration with all defaults applied.
41
+ * Used internally after merging user config with defaults.
42
+ */
43
+ export type ResolvedConfig = {
44
+ readonly apiKey: string;
45
+ readonly endpoint: string;
46
+ readonly debug: boolean;
47
+ readonly maxQueueSize: number;
48
+ readonly flushInterval: number;
49
+ readonly autoTrack: boolean;
50
+ readonly adaptiveNetwork: boolean;
51
+ };
52
+ /**
53
+ * UTM parameters for campaign tracking.
54
+ * Extracted from URL query parameters.
55
+ */
56
+ export type UtmParams = {
57
+ /** Traffic source (e.g., 'google', 'newsletter') */
58
+ readonly source?: string;
59
+ /** Marketing medium (e.g., 'cpc', 'email') */
60
+ readonly medium?: string;
61
+ /** Campaign name */
62
+ readonly campaign?: string;
63
+ /** Paid search keyword */
64
+ readonly term?: string;
65
+ /** Ad content identifier */
66
+ readonly content?: string;
67
+ };
68
+ /**
69
+ * Raw event as sent to the analytics server.
70
+ * Contains all tracking data including context.
71
+ */
72
+ export type RawEvent = {
73
+ /** Event name (e.g., 'pageview', 'click', 'signup') */
74
+ readonly event: string;
75
+ /** Custom event properties */
76
+ readonly properties: Record<string, unknown>;
77
+ /** Client timestamp (milliseconds since epoch) */
78
+ readonly timestamp: number;
79
+ /** Unique visitor identifier (persists across sessions) */
80
+ readonly visitor_id: string;
81
+ /** Session identifier (resets on new browser session) */
82
+ readonly session_id: string;
83
+ /** Current page URL */
84
+ readonly page_url: string;
85
+ /** Page title */
86
+ readonly page_title?: string;
87
+ /** Referrer URL */
88
+ readonly referrer?: string;
89
+ /** User agent string */
90
+ readonly user_agent: string;
91
+ /** Screen width in pixels */
92
+ readonly screen_width: number;
93
+ /** Screen height in pixels */
94
+ readonly screen_height: number;
95
+ /** UTM parameters if present */
96
+ readonly utm?: UtmParams;
97
+ };
98
+ /**
99
+ * Queued event waiting to be flushed.
100
+ * Contains only user-provided data; context is added at flush time.
101
+ */
102
+ export type QueuedEvent = {
103
+ /** Event name */
104
+ readonly event: string;
105
+ /** Custom event properties */
106
+ readonly properties: Record<string, unknown>;
107
+ /** Timestamp when event was queued */
108
+ readonly timestamp: number;
109
+ };
110
+ /**
111
+ * Network connection type as reported by the Network Information API.
112
+ * Used for adaptive batching strategies.
113
+ */
114
+ export type ConnectionType = 'slow-2g' | '2g' | '3g' | '4g' | 'unknown';
115
+ /**
116
+ * Network information for adaptive behavior.
117
+ * Based on the Network Information API.
118
+ */
119
+ export type NetworkInfo = {
120
+ /** Effective connection type */
121
+ readonly effectiveType: ConnectionType;
122
+ /** Downlink speed in Mbps */
123
+ readonly downlink?: number;
124
+ /** Round-trip time in milliseconds */
125
+ readonly rtt?: number;
126
+ /** Whether the user has enabled data saver mode */
127
+ readonly saveData?: boolean;
128
+ };
129
+ /**
130
+ * Batch of events sent to the server.
131
+ * Includes API key for authentication.
132
+ */
133
+ export type EventBatch = {
134
+ /** API key for authentication */
135
+ readonly api_key: string;
136
+ /** Array of events in this batch */
137
+ readonly events: readonly RawEvent[];
138
+ };
139
+ /**
140
+ * Adaptive settings computed based on network conditions.
141
+ * Adjusts SDK behavior for optimal performance.
142
+ */
143
+ export type AdaptiveSettings = {
144
+ /** Maximum events to queue before forcing a flush */
145
+ readonly maxQueueSize: number;
146
+ /** Interval between automatic flushes in milliseconds */
147
+ readonly flushInterval: number;
148
+ /** Whether to compress event payloads */
149
+ readonly compressionEnabled: boolean;
150
+ };
151
+ /**
152
+ * SDK internal state.
153
+ */
154
+ export type SdkState = {
155
+ /** Whether the SDK has been initialized */
156
+ initialized: boolean;
157
+ /** Current event queue */
158
+ queue: QueuedEvent[];
159
+ /** Flush timer ID */
160
+ flushTimerId: ReturnType<typeof setTimeout> | null;
161
+ /** Current visitor ID */
162
+ visitorId: VisitorId | null;
163
+ /** Current session ID */
164
+ sessionId: SessionId | null;
165
+ };
166
+ /**
167
+ * Check if a value is a valid UTM params object.
168
+ */
169
+ export declare function isValidUtmParams(value: unknown): value is UtmParams;
170
+ /**
171
+ * Check if a value is a valid RawEvent.
172
+ * Performs runtime validation of event structure.
173
+ */
174
+ export declare function isValidEvent(value: unknown): value is RawEvent;
175
+ /**
176
+ * Check if a value is a valid AnalyticsConfig.
177
+ */
178
+ export declare function isValidConfig(value: unknown): value is AnalyticsConfig;
179
+ /**
180
+ * Check if a value is a valid QueuedEvent.
181
+ */
182
+ export declare function isValidQueuedEvent(value: unknown): value is QueuedEvent;
183
+ /**
184
+ * Create a branded VisitorId from a string.
185
+ * Use this when generating or parsing visitor IDs.
186
+ */
187
+ export declare function createVisitorId(id: string): VisitorId;
188
+ /**
189
+ * Create a branded SessionId from a string.
190
+ * Use this when generating or parsing session IDs.
191
+ */
192
+ export declare function createSessionId(id: string): SessionId;
193
+ /**
194
+ * Default configuration values.
195
+ */
196
+ export declare const DEFAULT_CONFIG: Omit<ResolvedConfig, 'apiKey'>;
197
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAEnE;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAMnE;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,gCAAgC;IAChC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAE3B,sCAAsC;IACtC,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IAEzB,mCAAmC;IACnC,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAE/B,qCAAqC;IACrC,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAEhC,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAE7B,sFAAsF;IACtF,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAC;CACpC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAA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NetworkInfo, ConnectionType } from './types';
6
+ /**
7
+ * Parse query string into key-value object.
8
+ * Uses URLSearchParams if available, falls back to manual parsing.
9
+ *
10
+ * @param search - Query string (with or without leading '?')
11
+ * @returns Parsed key-value pairs
12
+ *
13
+ * @example
14
+ * parseQueryString('?foo=bar&baz=qux')
15
+ * // Returns: { foo: 'bar', baz: 'qux' }
16
+ */
17
+ export declare function parseQueryString(search: string): Record<string, string>;
18
+ /**
19
+ * Extract UTM parameters from current URL query string.
20
+ * Handles both standard query strings and hash-based routing.
21
+ *
22
+ * @returns UTM parameters found in URL
23
+ *
24
+ * @example
25
+ * // URL: https://example.com?utm_source=google&utm_medium=cpc
26
+ * getUtmParams()
27
+ * // Returns: { source: 'google', medium: 'cpc' }
28
+ */
29
+ export declare function getUtmParams(): UtmParams;
30
+ /**
31
+ * Get detailed network quality information.
32
+ * Uses the Network Information API when available.
33
+ *
34
+ * @returns Network info or null if API not available
35
+ */
36
+ export declare function getConnectionInfo(): NetworkInfo | null;
37
+ /**
38
+ * Get simplified connection type.
39
+ * Returns 'unknown' if Network Information API is not available.
40
+ *
41
+ * @returns Connection type: 'slow-2g' | '2g' | '3g' | '4g' | 'unknown'
42
+ */
43
+ export declare function getConnectionType(): ConnectionType;
44
+ /**
45
+ * Detect if the current user agent is a bot or crawler.
46
+ * Used to skip tracking for automated traffic.
47
+ *
48
+ * @returns true if user agent appears to be a bot
49
+ */
50
+ export declare function isBot(): boolean;
51
+ /**
52
+ * Debounce function calls.
53
+ * Delays execution until after wait milliseconds have elapsed
54
+ * since the last time the function was invoked.
55
+ *
56
+ * @param func - Function to debounce
57
+ * @param wait - Milliseconds to wait
58
+ * @returns Debounced function
59
+ */
60
+ export declare function debounce<T extends (...args: unknown[]) => unknown>(func: T, wait: number): (...args: Parameters<T>) => void;
61
+ /**
62
+ * Throttle function calls.
63
+ * Ensures function runs at most once per specified time period.
64
+ *
65
+ * @param func - Function to throttle
66
+ * @param limit - Minimum milliseconds between calls
67
+ * @returns Throttled function
68
+ */
69
+ export declare function throttle<T extends (...args: unknown[]) => unknown>(func: T, limit: number): (...args: Parameters<T>) => void;
70
+ /**
71
+ * Check if navigator.sendBeacon is available.
72
+ * Beacon API allows reliable data sending even when page is unloading.
73
+ *
74
+ * @returns true if sendBeacon is available
75
+ */
76
+ export declare function supportsBeacon(): boolean;
77
+ /**
78
+ * Check if Fetch API is available.
79
+ *
80
+ * @returns true if fetch is available
81
+ */
82
+ export declare function supportsFetch(): boolean;
83
+ /**
84
+ * Check if CompressionStream is available for GZIP compression.
85
+ *
86
+ * @returns true if compression is available
87
+ */
88
+ export declare function supportsCompression(): boolean;
89
+ /**
90
+ * Reset capability cache.
91
+ * Useful for testing.
92
+ */
93
+ export declare function resetCapabilityCache(): void;
94
+ /**
95
+ * Sanitize event names for consistency and safety.
96
+ * - Converts to lowercase
97
+ * - Replaces spaces with underscores
98
+ * - Removes special characters (keeps alphanumeric and underscore)
99
+ * - Limits length to 100 characters
100
+ *
101
+ * @param name - Raw event name
102
+ * @returns Sanitized event name
103
+ */
104
+ export declare function sanitizeEventName(name: string): string;
105
+ /**
106
+ * Sanitize event properties for safe transmission.
107
+ * - Removes undefined values
108
+ * - Converts Dates to ISO strings
109
+ * - Limits string lengths
110
+ * - Limits object depth to prevent circular references
111
+ * - Handles NaN and Infinity
112
+ *
113
+ * @param props - Raw properties object
114
+ * @returns Sanitized properties
115
+ */
116
+ export declare function sanitizeProperties(props: Record<string, unknown>): Record<string, unknown>;
117
+ /**
118
+ * Get current page URL safely.
119
+ *
120
+ * @returns Current page URL or empty string
121
+ */
122
+ export declare function getPageUrl(): string;
123
+ /**
124
+ * Get current page title safely.
125
+ *
126
+ * @returns Current page title or undefined
127
+ */
128
+ export declare function getPageTitle(): string | undefined;
129
+ /**
130
+ * Get referrer URL safely.
131
+ *
132
+ * @returns Referrer URL or undefined
133
+ */
134
+ export declare function getReferrer(): string | undefined;
135
+ /**
136
+ * Get user agent string safely.
137
+ *
138
+ * @returns User agent or empty string
139
+ */
140
+ export declare function getUserAgent(): string;
141
+ /**
142
+ * Get screen dimensions safely.
143
+ *
144
+ * @returns Object with width and height
145
+ */
146
+ export declare function getScreenDimensions(): {
147
+ width: number;
148
+ height: number;
149
+ };
150
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAiBtE;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CA+CvE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,IAAI,SAAS,CAiCxC;AA+BD;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAI,WAAW,GAAG,IAAI,CAetD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,CAelD;AAgDD;;;;;GAKG;AACH,wBAAgB,KAAK,IAAI,OAAO,CAwB/B;AAMD;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAChE,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,MAAM,GACX,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAalC;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,EAChE,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,MAAM,GACZ,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,CAsBlC;AAMD;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,OAAO,CAWxC;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,OAAO,CASvC;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAS7C;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAI3C;AAeD;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAetD;AAuED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAUzB;AAMD;;;;GAIG;AACH,wBAAgB,UAAU,IAAI,MAAM,CASnC;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CASjD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,MAAM,GAAG,SAAS,CAShD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,CASrC;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAYvE"}
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "analyzer-analytics",
3
+ "version": "0.1.0",
4
+ "description": "Lightweight analytics tracker for web applications (<3KB gzipped)",
5
+ "type": "module",
6
+ "main": "dist/analytics.js",
7
+ "module": "dist/analytics.esm.js",
8
+ "types": "dist/index.d.ts",
9
+ "browser": "dist/analytics.min.js",
10
+ "unpkg": "dist/analytics.min.js",
11
+ "jsdelivr": "dist/analytics.min.js",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/analytics.esm.js",
16
+ "require": "./dist/analytics.js",
17
+ "default": "./dist/analytics.esm.js"
18
+ },
19
+ "./iife": {
20
+ "default": "./dist/analytics.js"
21
+ },
22
+ "./min": {
23
+ "default": "./dist/analytics.min.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "sideEffects": false,
32
+ "scripts": {
33
+ "dev": "bun build src/index.ts --outdir=dist --watch",
34
+ "build": "bun run clean && bun run build:all && bun run size-check",
35
+ "build:all": "bun run build:core && bun run build:full && bun run build:types",
36
+ "build:core": "bun build src/core.ts --outfile=dist/analytics.min.js --format=iife --target=browser --global-name=Analytics --minify",
37
+ "build:slim": "bun build src/slim.ts --outfile=dist/analytics.slim.min.js --format=iife --target=browser --global-name=Analytics --minify",
38
+ "build:full": "bun run build:esm && bun run build:iife && bun run build:full-min",
39
+ "build:esm": "bun build src/index.ts --outfile=dist/analytics.esm.js --format=esm --target=browser",
40
+ "build:iife": "bun build src/index.ts --outfile=dist/analytics.js --format=iife --target=browser --global-name=Analytics",
41
+ "build:full-min": "bun build src/index.ts --outfile=dist/analytics.full.min.js --format=iife --target=browser --global-name=Analytics --minify",
42
+ "build:types": "tsc --emitDeclarationOnly --declaration --declarationMap --outDir dist",
43
+ "build:analyze": "bun run build:min && echo '\\n📊 Bundle Analysis:' && bun run scripts/analyze-bundle.ts",
44
+ "clean": "rm -rf dist && mkdir -p dist",
45
+ "size-check": "bun run scripts/check-size.ts",
46
+ "test": "bun test",
47
+ "test:watch": "bun test --watch",
48
+ "test:coverage": "bun test --coverage",
49
+ "typecheck": "tsc --noEmit",
50
+ "lint": "tsc --noEmit && echo 'Typecheck passed'",
51
+ "prepublishOnly": "bun run build && bun run test"
52
+ },
53
+ "devDependencies": {
54
+ "typescript": "^5.3.0",
55
+ "@types/bun": "latest",
56
+ "bun-types": "latest"
57
+ },
58
+ "keywords": [
59
+ "analytics",
60
+ "tracking",
61
+ "lightweight",
62
+ "web",
63
+ "performance",
64
+ "privacy",
65
+ "pageview",
66
+ "events",
67
+ "beacon"
68
+ ],
69
+ "author": "gonalc",
70
+ "license": "MIT",
71
+ "repository": {
72
+ "type": "git",
73
+ "url": "git+https://github.com/gonalc/analyzer.git"
74
+ },
75
+ "bugs": {
76
+ "url": "https://github.com/gonalc/analyzer/issues"
77
+ },
78
+ "homepage": "https://github.com/gonalc/analyzer#readme",
79
+ "engines": {
80
+ "node": ">=16.0.0"
81
+ }
82
+ }