@v-tilt/browser 1.0.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 (64) hide show
  1. package/dist/array.js +2 -0
  2. package/dist/array.js.map +1 -0
  3. package/dist/array.no-external.js +2 -0
  4. package/dist/array.no-external.js.map +1 -0
  5. package/dist/config.d.ts +17 -0
  6. package/dist/constants.d.ts +10 -0
  7. package/dist/entrypoints/array.d.ts +1 -0
  8. package/dist/entrypoints/array.no-external.d.ts +1 -0
  9. package/dist/entrypoints/main.cjs.d.ts +4 -0
  10. package/dist/entrypoints/module.es.d.ts +3 -0
  11. package/dist/entrypoints/module.no-external.es.d.ts +4 -0
  12. package/dist/geolocation.d.ts +5 -0
  13. package/dist/main.js +2 -0
  14. package/dist/main.js.map +1 -0
  15. package/dist/module.d.ts +214 -0
  16. package/dist/module.js +2 -0
  17. package/dist/module.js.map +1 -0
  18. package/dist/module.no-external.d.ts +214 -0
  19. package/dist/module.no-external.js +2 -0
  20. package/dist/module.no-external.js.map +1 -0
  21. package/dist/session.d.ts +26 -0
  22. package/dist/tracking.d.ts +112 -0
  23. package/dist/types.d.ts +67 -0
  24. package/dist/user-manager.d.ts +152 -0
  25. package/dist/utils/globals.d.ts +4 -0
  26. package/dist/utils/index.d.ts +30 -0
  27. package/dist/utils.d.ts +21 -0
  28. package/dist/vtilt.d.ts +178 -0
  29. package/dist/web-vitals.d.ts +11 -0
  30. package/lib/config.d.ts +17 -0
  31. package/lib/config.js +76 -0
  32. package/lib/constants.d.ts +10 -0
  33. package/lib/constants.js +463 -0
  34. package/lib/entrypoints/array.d.ts +1 -0
  35. package/lib/entrypoints/array.js +3 -0
  36. package/lib/entrypoints/array.no-external.d.ts +1 -0
  37. package/lib/entrypoints/array.no-external.js +4 -0
  38. package/lib/entrypoints/main.cjs.d.ts +4 -0
  39. package/lib/entrypoints/main.cjs.js +29 -0
  40. package/lib/entrypoints/module.es.d.ts +3 -0
  41. package/lib/entrypoints/module.es.js +22 -0
  42. package/lib/entrypoints/module.no-external.es.d.ts +4 -0
  43. package/lib/entrypoints/module.no-external.es.js +23 -0
  44. package/lib/geolocation.d.ts +5 -0
  45. package/lib/geolocation.js +26 -0
  46. package/lib/session.d.ts +26 -0
  47. package/lib/session.js +115 -0
  48. package/lib/tracking.d.ts +112 -0
  49. package/lib/tracking.js +326 -0
  50. package/lib/types.d.ts +67 -0
  51. package/lib/types.js +2 -0
  52. package/lib/user-manager.d.ts +152 -0
  53. package/lib/user-manager.js +565 -0
  54. package/lib/utils/globals.d.ts +4 -0
  55. package/lib/utils/globals.js +5 -0
  56. package/lib/utils/index.d.ts +30 -0
  57. package/lib/utils/index.js +89 -0
  58. package/lib/utils.d.ts +21 -0
  59. package/lib/utils.js +57 -0
  60. package/lib/vtilt.d.ts +178 -0
  61. package/lib/vtilt.js +403 -0
  62. package/lib/web-vitals.d.ts +11 -0
  63. package/lib/web-vitals.js +67 -0
  64. package/package.json +61 -0
@@ -0,0 +1,178 @@
1
+ import { VTiltConfig, EventPayload } from "./types";
2
+ export declare class VTilt {
3
+ private configManager;
4
+ private trackingManager;
5
+ private webVitalsManager;
6
+ private initialized;
7
+ constructor(config?: Partial<VTiltConfig>);
8
+ /**
9
+ * Initialize VTilt tracking
10
+ */
11
+ init(): void;
12
+ /**
13
+ * Track a custom event
14
+ */
15
+ trackEvent(name: string, payload?: EventPayload): void;
16
+ /**
17
+ * Identify a user with PostHog-style property operations
18
+ * Copied from PostHog's identify method signature
19
+ *
20
+ * @example
21
+ * ```js
22
+ * // Basic identify with properties
23
+ * vTilt.identify('user_123',
24
+ * { name: 'John Doe', email: 'john@example.com' }, // $set properties
25
+ * { first_login: new Date().toISOString() } // $set_once properties
26
+ * )
27
+ * ```
28
+ *
29
+ * @example
30
+ * ```js
31
+ * // If no $set is provided, all properties are treated as $set
32
+ * vTilt.identify('user_123', { name: 'John Doe', email: 'john@example.com' })
33
+ * ```
34
+ */
35
+ identify(distinctId?: string, userPropertiesToSet?: Record<string, any>, userPropertiesToSetOnce?: Record<string, any>): void;
36
+ /**
37
+ * Set user properties with PostHog-style operations
38
+ * Sets properties on the person profile associated with the current distinct_id
39
+ *
40
+ * @example
41
+ * ```js
42
+ * // Set properties that can be updated
43
+ * vTilt.setUserProperties({ name: 'John Doe', email: 'john@example.com' })
44
+ * ```
45
+ *
46
+ * @example
47
+ * ```js
48
+ * // Set properties with $set and $set_once operations
49
+ * vTilt.setUserProperties(
50
+ * { name: 'John Doe', last_login: new Date().toISOString() }, // $set properties
51
+ * { first_login: new Date().toISOString() } // $set_once properties
52
+ * )
53
+ * ```
54
+ *
55
+ * @param userPropertiesToSet Optional: Properties to set (can be updated)
56
+ * @param userPropertiesToSetOnce Optional: Properties to set once (preserves first value)
57
+ */
58
+ setUserProperties(userPropertiesToSet?: Record<string, any>, userPropertiesToSetOnce?: Record<string, any>): void;
59
+ /**
60
+ * Reset user identity (logout)
61
+ * PostHog behavior: Clears all user data, generates new anonymous ID
62
+ *
63
+ * @param reset_device_id - If true, also resets device_id. Default: false
64
+ *
65
+ * @example
66
+ * // Reset on user logout
67
+ * vtilt.resetUser()
68
+ *
69
+ * @example
70
+ * // Reset and generate new device ID
71
+ * vtilt.resetUser(true)
72
+ */
73
+ resetUser(reset_device_id?: boolean): void;
74
+ /**
75
+ * Get current user identity
76
+ */
77
+ getUserIdentity(): import("./types").UserIdentity;
78
+ /**
79
+ * Get current device ID
80
+ */
81
+ getDeviceId(): string;
82
+ /**
83
+ * Get current user state
84
+ */
85
+ getUserState(): "anonymous" | "identified";
86
+ /**
87
+ * Create an alias to link two distinct IDs
88
+ * PostHog behavior: Links anonymous session to account on signup
89
+ *
90
+ * @param alias - A unique identifier that you want to use for this user in the future
91
+ * @param original - The current identifier being used for this user (optional, defaults to current distinct_id)
92
+ *
93
+ * @example
94
+ * // Link anonymous user to account on signup
95
+ * vtilt.createAlias('user_12345')
96
+ *
97
+ * @example
98
+ * // Explicit alias with original ID
99
+ * vtilt.createAlias('user_12345', 'anonymous_abc123')
100
+ */
101
+ createAlias(alias: string, original?: string): void;
102
+ /**
103
+ * Setup page tracking with history API support
104
+ */
105
+ private setupPageTracking;
106
+ /**
107
+ * Track page hit
108
+ */
109
+ private trackPageHit;
110
+ /**
111
+ * Setup visibility change tracking for prerendered pages
112
+ */
113
+ private setupVisibilityTracking;
114
+ /**
115
+ * Get current configuration
116
+ */
117
+ getConfig(): VTiltConfig;
118
+ /**
119
+ * Get current session ID
120
+ */
121
+ getSessionId(): string | null;
122
+ /**
123
+ * Update configuration
124
+ */
125
+ updateConfig(config: Partial<VTiltConfig>): void;
126
+ /**
127
+ * _execute_array() deals with processing any VTilt function
128
+ * calls that were called before the VTilt library was loaded
129
+ * (and are thus stored in an array so they can be called later)
130
+ *
131
+ * Note: we fire off all the VTilt function calls BEFORE we fire off
132
+ * tracking calls. This is so identify/setUserProperties/updateConfig calls
133
+ * can properly modify early tracking calls.
134
+ *
135
+ * @param {Array} array Array of queued calls in format [methodName, ...args]
136
+ */
137
+ _execute_array(array: any[]): void;
138
+ /**
139
+ * Called when DOM is loaded - processes queued requests
140
+ */
141
+ _dom_loaded(): void;
142
+ }
143
+ /**
144
+ * Initialize VTilt as a module (similar to PostHog's init_as_module)
145
+ * Returns an uninitialized VTilt instance that the user must call init() on
146
+ */
147
+ export declare function init_as_module(): VTilt;
148
+ /**
149
+ * Initialize VTilt from snippet (similar to PostHog's init_from_snippet)
150
+ * Processes queued calls from the snippet stub and replaces it with real instance
151
+ *
152
+ * The snippet uses some clever tricks to allow deferred loading of array.js (this code)
153
+ *
154
+ * window.vt is an array which the queue of calls made before the lib is loaded
155
+ * It has methods attached to it to simulate the vt object so for instance
156
+ *
157
+ * window.vt.init("projectId", {token: "token", host: "host"})
158
+ * window.vt.trackEvent("my-event", {foo: "bar"})
159
+ *
160
+ * ... will mean that window.vt will look like this:
161
+ * window.vt == [
162
+ * ["trackEvent", "my-event", {foo: "bar"}]
163
+ * ]
164
+ *
165
+ * window.vt._i == [
166
+ * ["projectId", {token: "token", host: "host"}, "vt"]
167
+ * ]
168
+ *
169
+ * If a name is given to the init function then the same as above is true but as a sub-property on the object:
170
+ *
171
+ * window.vt.init("projectId", {token: "token"}, "vt2")
172
+ * window.vt.vt2.trackEvent("my-event", {foo: "bar"})
173
+ *
174
+ * window.vt.vt2 == [
175
+ * ["trackEvent", "my-event", {foo: "bar"}]
176
+ * ]
177
+ */
178
+ export declare function init_from_snippet(): void;
@@ -0,0 +1,11 @@
1
+ import { VTiltConfig } from "./types";
2
+ import { TrackingManager } from "./tracking";
3
+ export declare class WebVitalsManager {
4
+ private trackingManager;
5
+ private webVitals;
6
+ constructor(config: VTiltConfig, trackingManager: TrackingManager);
7
+ /**
8
+ * Initialize web vitals tracking
9
+ */
10
+ private initializeWebVitals;
11
+ }
@@ -0,0 +1,17 @@
1
+ import { VTiltConfig } from "./types";
2
+ export declare class ConfigManager {
3
+ private config;
4
+ constructor(config?: Partial<VTiltConfig>);
5
+ /**
6
+ * Parse configuration from script attributes
7
+ */
8
+ private parseConfigFromScript;
9
+ /**
10
+ * Get the current configuration
11
+ */
12
+ getConfig(): VTiltConfig;
13
+ /**
14
+ * Update configuration
15
+ */
16
+ updateConfig(newConfig: Partial<VTiltConfig>): void;
17
+ }
package/lib/config.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigManager = void 0;
4
+ class ConfigManager {
5
+ constructor(config = {}) {
6
+ this.config = this.parseConfigFromScript(config);
7
+ }
8
+ /**
9
+ * Parse configuration from script attributes
10
+ */
11
+ parseConfigFromScript(initialConfig) {
12
+ if (!document.currentScript) {
13
+ return {
14
+ projectId: initialConfig.projectId || "",
15
+ token: initialConfig.token || "",
16
+ ...initialConfig,
17
+ };
18
+ }
19
+ const script = document.currentScript;
20
+ const config = {
21
+ projectId: "", // Required field with default
22
+ token: "", // Required field with default
23
+ ...initialConfig,
24
+ };
25
+ // Parse basic attributes
26
+ config.host = script.getAttribute("data-host") || initialConfig.host;
27
+ config.proxy = script.getAttribute("data-proxy") || initialConfig.proxy;
28
+ config.proxyUrl =
29
+ script.getAttribute("data-proxy-url") || initialConfig.proxyUrl;
30
+ config.token =
31
+ script.getAttribute("data-token") || initialConfig.token || "";
32
+ config.projectId =
33
+ script.getAttribute("data-project-id") || initialConfig.projectId || "";
34
+ config.domain = script.getAttribute("data-domain") || initialConfig.domain;
35
+ config.storage =
36
+ script.getAttribute("data-storage") || initialConfig.storage;
37
+ config.stringifyPayload =
38
+ script.getAttribute("data-stringify-payload") !== "false";
39
+ config.webVitals =
40
+ script.getAttribute("web-vitals") === "true" ||
41
+ script.getAttribute("data-web-vitals") === "true" ||
42
+ initialConfig.webVitals;
43
+ // Check for conflicting proxy configurations
44
+ if (config.proxy && config.proxyUrl) {
45
+ console.error("Error: Both data-proxy and data-proxy-url are specified. Please use only one of them.");
46
+ throw new Error("Both data-proxy and data-proxy-url are specified. Please use only one of them.");
47
+ }
48
+ // Parse global attributes
49
+ config.globalAttributes = { ...initialConfig.globalAttributes };
50
+ // NamedNodeMap is not iterable, so use Array.from to iterate over attributes
51
+ for (const attr of Array.from(script.attributes)) {
52
+ if (attr.name.startsWith("tb_")) {
53
+ config.globalAttributes[attr.name.slice(3).replace(/-/g, "_")] =
54
+ attr.value;
55
+ }
56
+ if (attr.name.startsWith("data-tb-")) {
57
+ config.globalAttributes[attr.name.slice(8).replace(/-/g, "_")] =
58
+ attr.value;
59
+ }
60
+ }
61
+ return config;
62
+ }
63
+ /**
64
+ * Get the current configuration
65
+ */
66
+ getConfig() {
67
+ return { ...this.config };
68
+ }
69
+ /**
70
+ * Update configuration
71
+ */
72
+ updateConfig(newConfig) {
73
+ this.config = { ...this.config, ...newConfig };
74
+ }
75
+ }
76
+ exports.ConfigManager = ConfigManager;
@@ -0,0 +1,10 @@
1
+ import { StorageMethods } from "./types";
2
+ export declare const STORAGE_KEY = "vt_session_id";
3
+ export declare const ANONYMOUS_ID_KEY = "vt_anonymous_id";
4
+ export declare const DISTINCT_ID_KEY = "vt_distinct_id";
5
+ export declare const DEVICE_ID_KEY = "vt_device_id";
6
+ export declare const USER_PROPERTIES_KEY = "vt_user_properties";
7
+ export declare const USER_STATE_KEY = "vt_user_state";
8
+ export declare const STORAGE_METHODS: StorageMethods;
9
+ export declare const TIMEZONES: Record<string, string>;
10
+ export declare const ATTRIBUTES_TO_MASK: string[];