@v-tilt/browser 1.1.2 → 1.1.4

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/session.d.ts CHANGED
@@ -1,59 +1,41 @@
1
+ /**
2
+ * Session Manager - Handles session_id and window_id
3
+ *
4
+ * Uses shared StorageManager for consistent storage operations.
5
+ *
6
+ * Session ID: Unique per user session, expires after 30 minutes of inactivity
7
+ * Window ID: Unique per browser tab, persists across page reloads
8
+ */
1
9
  import { PersistenceMethod } from "./types";
2
10
  export declare class SessionManager {
3
- private storageMethod;
4
- private domain?;
11
+ private storage;
5
12
  private _windowId;
6
13
  constructor(storageMethod?: PersistenceMethod, domain?: string);
7
14
  /**
8
- * Check if using web storage (localStorage or sessionStorage)
9
- */
10
- private _isWebStorage;
11
- /**
12
- * Get storage object (localStorage or sessionStorage)
13
- */
14
- private _getStorage;
15
- /**
16
- * Create session data object with expiry
17
- */
18
- private _createSessionData;
19
- /**
20
- * Store session ID in web storage
21
- */
22
- private _storeSessionIdInWebStorage;
23
- /**
24
- * Get session ID from cookie
15
+ * Get session ID (always returns a value, generates if needed)
25
16
  */
26
- private getSessionIdFromCookie;
17
+ getSessionId(): string;
27
18
  /**
28
- * Set session ID in cookie
19
+ * Set session ID in storage
20
+ * Extends TTL if session_id exists, generates new one if not
29
21
  */
30
- private setSessionIdFromCookie;
22
+ setSessionId(): string;
31
23
  /**
32
- * Store session ID (in web storage or cookie)
24
+ * Reset session ID (generates new session on reset)
33
25
  */
34
- private _storeSessionId;
26
+ resetSessionId(): void;
35
27
  /**
36
28
  * Get session ID from storage (raw, can return null)
37
- * Private method used internally
38
29
  */
39
30
  private _getSessionIdRaw;
40
31
  /**
41
- * Get session ID (always returns a value, generates if needed)
42
- */
43
- getSessionId(): string;
44
- /**
45
- * Set session ID in storage
46
- * Extends TTL if session_id exists, generates new one if not
32
+ * Store session ID
47
33
  */
48
- setSessionId(): string;
34
+ private _storeSessionId;
49
35
  /**
50
36
  * Clear session ID from storage
51
37
  */
52
38
  private _clearSessionId;
53
- /**
54
- * Reset session ID (generates new session on reset)
55
- */
56
- resetSessionId(): void;
57
39
  /**
58
40
  * Get window ID
59
41
  * Window ID is unique per browser tab/window and persists across page reloads
@@ -65,11 +47,6 @@ export declare class SessionManager {
65
47
  * Stores in sessionStorage which is unique per tab
66
48
  */
67
49
  private _setWindowId;
68
- /**
69
- * Check if we can use sessionStorage for window_id
70
- * sessionStorage is unique per tab, perfect for window_id
71
- */
72
- private _canUseSessionStorage;
73
50
  /**
74
51
  * Initialize window ID
75
52
  * Detects tab duplication and handles window_id persistence
@@ -80,4 +57,8 @@ export declare class SessionManager {
80
57
  * This helps distinguish between page reloads and tab duplication
81
58
  */
82
59
  private _listenToUnload;
60
+ /**
61
+ * Update storage method at runtime
62
+ */
63
+ updateStorageMethod(method: PersistenceMethod, domain?: string): void;
83
64
  }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Unified Storage Manager - Following PostHog's PostHogPersistence pattern
3
+ *
4
+ * Single class handles all storage operations for both sessions and users.
5
+ * Reduces code duplication and ensures consistent cookie handling.
6
+ *
7
+ * Storage methods:
8
+ * - cookie: Browser cookies (cross-subdomain support)
9
+ * - localStorage: Persistent local storage
10
+ * - sessionStorage: Tab-specific storage (cleared on tab close)
11
+ * - localStorage+cookie: Both for redundancy
12
+ * - memory: In-memory only (no persistence)
13
+ */
14
+ import { PersistenceMethod } from "./types";
15
+ export declare const SESSION_COOKIE_MAX_AGE = 1800;
16
+ export declare const USER_COOKIE_MAX_AGE = 31536000;
17
+ export interface StorageOptions {
18
+ method: PersistenceMethod;
19
+ domain?: string;
20
+ secure?: boolean;
21
+ sameSite?: "Strict" | "Lax" | "None";
22
+ }
23
+ export interface StorageItem<T = string> {
24
+ value: T;
25
+ expiry?: number;
26
+ }
27
+ /**
28
+ * Unified Storage Manager
29
+ * Provides consistent storage operations across all persistence methods
30
+ */
31
+ export declare class StorageManager {
32
+ private method;
33
+ private domain?;
34
+ private secure;
35
+ private sameSite;
36
+ private memoryStorage;
37
+ constructor(options: StorageOptions);
38
+ /**
39
+ * Get a value from storage
40
+ */
41
+ get(key: string): string | null;
42
+ /**
43
+ * Set a value in storage
44
+ * @param maxAge - Cookie max age in seconds (ignored for localStorage/sessionStorage)
45
+ */
46
+ set(key: string, value: string, maxAge?: number): void;
47
+ /**
48
+ * Remove a value from storage
49
+ */
50
+ remove(key: string): void;
51
+ /**
52
+ * Get JSON value with expiry check
53
+ * Returns null if expired or not found
54
+ */
55
+ getWithExpiry<T>(key: string): T | null;
56
+ /**
57
+ * Set JSON value with optional expiry
58
+ * @param ttlMs - Time to live in milliseconds
59
+ */
60
+ setWithExpiry<T>(key: string, value: T, ttlMs?: number): void;
61
+ /**
62
+ * Get JSON value (no expiry check)
63
+ */
64
+ getJSON<T>(key: string): T | null;
65
+ /**
66
+ * Set JSON value
67
+ */
68
+ setJSON<T>(key: string, value: T, maxAge?: number): void;
69
+ private getCookie;
70
+ private setCookie;
71
+ private removeCookie;
72
+ private usesLocalStorage;
73
+ private usesSessionStorage;
74
+ /**
75
+ * Check if sessionStorage is available
76
+ */
77
+ canUseSessionStorage(): boolean;
78
+ /**
79
+ * Get direct access to sessionStorage (for window_id which always uses sessionStorage)
80
+ */
81
+ getSessionStorage(): Storage | null;
82
+ /**
83
+ * Update storage method at runtime
84
+ */
85
+ setMethod(method: PersistenceMethod): void;
86
+ /**
87
+ * Get current storage method
88
+ */
89
+ getMethod(): PersistenceMethod;
90
+ }
91
+ /**
92
+ * Create a shared storage instance
93
+ * Use this for creating storage managers with consistent settings
94
+ */
95
+ export declare function createStorageManager(method: PersistenceMethod, domain?: string): StorageManager;
@@ -1,7 +1,18 @@
1
+ /**
2
+ * User Manager - Handles user identity and properties
3
+ *
4
+ * Uses shared StorageManager for consistent storage operations.
5
+ *
6
+ * Manages:
7
+ * - anonymous_id: Generated ID for anonymous users
8
+ * - distinct_id: User-provided ID after identification
9
+ * - device_id: Persistent device identifier
10
+ * - user_properties: Custom properties set via identify/setUserProperties
11
+ * - user_state: "anonymous" or "identified"
12
+ */
1
13
  import { UserIdentity, AliasEvent, PersistenceMethod } from "./types";
2
14
  export declare class UserManager {
3
- private storageMethod;
4
- private domain?;
15
+ private storage;
5
16
  private userIdentity;
6
17
  private _cachedPersonProperties;
7
18
  constructor(storageMethod?: PersistenceMethod, domain?: string);
@@ -21,57 +32,31 @@ export declare class UserManager {
21
32
  * Get current user properties
22
33
  */
23
34
  getUserProperties(): Record<string, any>;
35
+ /**
36
+ * Get the effective ID for event tracking
37
+ */
38
+ getEffectiveId(): string;
39
+ /**
40
+ * Get current device ID
41
+ */
42
+ getDeviceId(): string;
43
+ /**
44
+ * Get current user state
45
+ */
46
+ getUserState(): "anonymous" | "identified";
24
47
  /**
25
48
  * Identify a user with distinct ID and properties
26
49
  */
27
50
  identify(newDistinctId?: string, userPropertiesToSet?: Record<string, any>, userPropertiesToSetOnce?: Record<string, any>): void;
28
51
  /**
29
52
  * Set user properties without changing distinct ID
30
- * Sets properties on the person profile associated with the current distinct_id
31
- *
32
- * @example
33
- * ```js
34
- * // Set properties that can be updated
35
- * vt.setUserProperties({ name: 'John Doe', email: 'john@example.com' })
36
- * ```
37
- *
38
- * @example
39
- * ```js
40
- * // Set properties with $set and $set_once operations
41
- * vt.setUserProperties(
42
- * { name: 'John Doe', last_login: new Date().toISOString() }, // $set properties
43
- * { first_login: new Date().toISOString() } // $set_once properties
44
- * )
45
- * ```
46
- *
47
- * @param userPropertiesToSet Optional: Properties to set (can be updated)
48
- * @param userPropertiesToSetOnce Optional: Properties to set once (preserves first value)
49
53
  */
50
54
  setUserProperties(userPropertiesToSet?: Record<string, any>, userPropertiesToSetOnce?: Record<string, any>): boolean;
51
- /**
52
- * Get hash for person properties
53
- * Used for deduplication of identical setUserProperties calls
54
- */
55
- private getPersonPropertiesHash;
56
55
  /**
57
56
  * Reset user identity (logout)
58
57
  * Generates new anonymous ID, clears user data, optionally resets device ID
59
- *
60
- * @param reset_device_id - If true, also resets device_id. Default: false (preserves device_id)
61
58
  */
62
59
  reset(reset_device_id?: boolean): void;
63
- /**
64
- * Get the effective ID for event tracking
65
- */
66
- getEffectiveId(): string;
67
- /**
68
- * Get current device ID
69
- */
70
- getDeviceId(): string;
71
- /**
72
- * Get current user state
73
- */
74
- getUserState(): "anonymous" | "identified";
75
60
  /**
76
61
  * Update distinct ID (internal use - for VTilt)
77
62
  */
@@ -88,29 +73,26 @@ export declare class UserManager {
88
73
  * Set device ID if not already set (internal use - for VTilt)
89
74
  */
90
75
  ensureDeviceId(deviceId: string): void;
76
+ /**
77
+ * Create an alias to link two distinct IDs
78
+ */
79
+ createAlias(alias: string, original?: string): AliasEvent | null;
91
80
  /**
92
81
  * Check if distinct ID is string-like (hardcoded string) - public for validation
93
- * This is a wrapper to expose the private method
94
82
  */
95
83
  isDistinctIdStringLikePublic(distinctId: string): boolean;
96
84
  /**
97
- * Create an alias to link two distinct IDs
98
- * If original is not provided, uses current distinct_id
99
- * If alias matches original, returns null (caller should use identify instead)
100
- *
101
- * @param alias - A unique identifier that you want to use for this user in the future
102
- * @param original - The current identifier being used for this user (optional, defaults to current distinct_id)
103
- * @returns AliasEvent if alias was created, null if alias matches original or invalid
85
+ * Set initial person info
104
86
  */
105
- createAlias(alias: string, original?: string): AliasEvent | null;
87
+ set_initial_person_info(maskPersonalDataProperties?: boolean, customPersonalDataProperties?: string[]): void;
106
88
  /**
107
- * Validate distinct ID to prevent hardcoded strings
89
+ * Get initial props
108
90
  */
109
- private isValidDistinctId;
91
+ get_initial_props(): Record<string, any>;
110
92
  /**
111
- * Check if distinct ID is string-like (hardcoded string)
93
+ * Update referrer info
112
94
  */
113
- private isDistinctIdStringLike;
95
+ update_referrer_info(): void;
114
96
  /**
115
97
  * Load user identity from storage
116
98
  */
@@ -119,26 +101,6 @@ export declare class UserManager {
119
101
  * Save user identity to storage
120
102
  */
121
103
  private saveUserIdentity;
122
- /**
123
- * Generate a new anonymous ID
124
- */
125
- private generateAnonymousId;
126
- /**
127
- * Generate a new device ID
128
- */
129
- private generateDeviceId;
130
- /**
131
- * Get stored value from storage
132
- */
133
- private getStoredValue;
134
- /**
135
- * Set stored value in storage
136
- */
137
- private setStoredValue;
138
- /**
139
- * Remove stored value from storage
140
- */
141
- private removeStoredValue;
142
104
  /**
143
105
  * Get user properties from storage
144
106
  */
@@ -148,36 +110,31 @@ export declare class UserManager {
148
110
  */
149
111
  private setStoredUserProperties;
150
112
  /**
151
- * Get cookie value
113
+ * Register a value once (only if not already set)
152
114
  */
153
- private getCookieValue;
115
+ private register_once;
154
116
  /**
155
- * Set cookie value
117
+ * Generate a new anonymous ID
156
118
  */
157
- private setCookieValue;
119
+ private generateAnonymousId;
158
120
  /**
159
- * Remove cookie value
121
+ * Generate a new device ID
160
122
  */
161
- private removeCookieValue;
123
+ private generateDeviceId;
162
124
  /**
163
- * Register a value once (only if not already set)
164
- * Stores properties in localStorage only if they don't already exist
125
+ * Get hash for person properties (for deduplication)
165
126
  */
166
- private register_once;
127
+ private getPersonPropertiesHash;
167
128
  /**
168
- * Set initial person info
169
- * Stores referrer and URL info on first visit for generating $initial_* properties
129
+ * Validate distinct ID
170
130
  */
171
- set_initial_person_info(maskPersonalDataProperties?: boolean, customPersonalDataProperties?: string[]): void;
131
+ private isValidDistinctId;
172
132
  /**
173
- * Get initial props
174
- * Generates $initial_* properties from stored initial person info
175
- * These are sent with events as $set_once to preserve first values
133
+ * Check if distinct ID is string-like (hardcoded string)
176
134
  */
177
- get_initial_props(): Record<string, any>;
135
+ private isDistinctIdStringLike;
178
136
  /**
179
- * Update referrer info
180
- * Stores current referrer information if not already stored
137
+ * Update storage method at runtime
181
138
  */
182
- update_referrer_info(): void;
139
+ updateStorageMethod(method: PersistenceMethod, domain?: string): void;
183
140
  }
package/lib/session.d.ts CHANGED
@@ -1,59 +1,41 @@
1
+ /**
2
+ * Session Manager - Handles session_id and window_id
3
+ *
4
+ * Uses shared StorageManager for consistent storage operations.
5
+ *
6
+ * Session ID: Unique per user session, expires after 30 minutes of inactivity
7
+ * Window ID: Unique per browser tab, persists across page reloads
8
+ */
1
9
  import { PersistenceMethod } from "./types";
2
10
  export declare class SessionManager {
3
- private storageMethod;
4
- private domain?;
11
+ private storage;
5
12
  private _windowId;
6
13
  constructor(storageMethod?: PersistenceMethod, domain?: string);
7
14
  /**
8
- * Check if using web storage (localStorage or sessionStorage)
9
- */
10
- private _isWebStorage;
11
- /**
12
- * Get storage object (localStorage or sessionStorage)
13
- */
14
- private _getStorage;
15
- /**
16
- * Create session data object with expiry
17
- */
18
- private _createSessionData;
19
- /**
20
- * Store session ID in web storage
21
- */
22
- private _storeSessionIdInWebStorage;
23
- /**
24
- * Get session ID from cookie
15
+ * Get session ID (always returns a value, generates if needed)
25
16
  */
26
- private getSessionIdFromCookie;
17
+ getSessionId(): string;
27
18
  /**
28
- * Set session ID in cookie
19
+ * Set session ID in storage
20
+ * Extends TTL if session_id exists, generates new one if not
29
21
  */
30
- private setSessionIdFromCookie;
22
+ setSessionId(): string;
31
23
  /**
32
- * Store session ID (in web storage or cookie)
24
+ * Reset session ID (generates new session on reset)
33
25
  */
34
- private _storeSessionId;
26
+ resetSessionId(): void;
35
27
  /**
36
28
  * Get session ID from storage (raw, can return null)
37
- * Private method used internally
38
29
  */
39
30
  private _getSessionIdRaw;
40
31
  /**
41
- * Get session ID (always returns a value, generates if needed)
42
- */
43
- getSessionId(): string;
44
- /**
45
- * Set session ID in storage
46
- * Extends TTL if session_id exists, generates new one if not
32
+ * Store session ID
47
33
  */
48
- setSessionId(): string;
34
+ private _storeSessionId;
49
35
  /**
50
36
  * Clear session ID from storage
51
37
  */
52
38
  private _clearSessionId;
53
- /**
54
- * Reset session ID (generates new session on reset)
55
- */
56
- resetSessionId(): void;
57
39
  /**
58
40
  * Get window ID
59
41
  * Window ID is unique per browser tab/window and persists across page reloads
@@ -65,11 +47,6 @@ export declare class SessionManager {
65
47
  * Stores in sessionStorage which is unique per tab
66
48
  */
67
49
  private _setWindowId;
68
- /**
69
- * Check if we can use sessionStorage for window_id
70
- * sessionStorage is unique per tab, perfect for window_id
71
- */
72
- private _canUseSessionStorage;
73
50
  /**
74
51
  * Initialize window ID
75
52
  * Detects tab duplication and handles window_id persistence
@@ -80,4 +57,8 @@ export declare class SessionManager {
80
57
  * This helps distinguish between page reloads and tab duplication
81
58
  */
82
59
  private _listenToUnload;
60
+ /**
61
+ * Update storage method at runtime
62
+ */
63
+ updateStorageMethod(method: PersistenceMethod, domain?: string): void;
83
64
  }