@altertable/altertable-js 0.4.0 → 0.5.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Altertable
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,306 @@
1
+ # @altertable/altertable-js
2
+
3
+ JavaScript SDK for capturing and sending analytics events to Altertable.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @altertable/altertable-js
9
+ # or
10
+ pnpm add @altertable/altertable-js
11
+ # or
12
+ yarn add @altertable/altertable-js
13
+ # or
14
+ bun add @altertable/altertable-js
15
+ ```
16
+
17
+ ## Quick Start
18
+
19
+ ```javascript
20
+ import { altertable } from '@altertable/altertable-js';
21
+
22
+ // Initialize with your API key
23
+ altertable.init('YOUR_API_KEY');
24
+
25
+ // Track an event
26
+ altertable.track('Step Completed', {
27
+ step: 1,
28
+ });
29
+
30
+ // Identify a user
31
+ altertable.identify('u_01jza857w4f23s1hf2s61befmw', {
32
+ email: 'john.doe@example.com',
33
+ name: 'John Doe',
34
+ company: 'Acme Corp',
35
+ role: 'Software Engineer',
36
+ });
37
+
38
+ // Update user traits
39
+ altertable.updateTraits({
40
+ onboarding_completed: true,
41
+ });
42
+ ```
43
+
44
+ ## Features
45
+
46
+ - **Automatic page view tracking** – Captures page views automatically
47
+ - **Session management** – Handles visitor and session IDs automatically
48
+ - **Event queuing** – Queues events when offline or consent is pending
49
+ - **Privacy compliance** – Built-in tracking consent management
50
+ - **Multiple storage options** – localStorage, cookies, or both
51
+ - **TypeScript support** – Full TypeScript definitions included
52
+ - **Lightweight** – Minimal bundle size with no external dependencies
53
+
54
+ ## API Reference
55
+
56
+ ### Initialization
57
+
58
+ #### `altertable.init(apiKey, config?)`
59
+
60
+ Initializes the Altertable SDK with your API key and optional configuration.
61
+
62
+ **Parameters:**
63
+
64
+ | Parameter | Type | Required | Description |
65
+ | --------- | --------------------------------------- | -------- | ----------------------- |
66
+ | `apiKey` | `string` | Yes | Your Altertable API key |
67
+ | `config` | [`AltertableConfig`](#altertableconfig) | No | Configuration options |
68
+
69
+ **Example:**
70
+
71
+ ```javascript
72
+ altertable.init('YOUR_API_KEY', {
73
+ environment: 'development',
74
+ });
75
+ ```
76
+
77
+ ### Event Tracking
78
+
79
+ #### `altertable.track(event, properties?)`
80
+
81
+ Tracks a custom event with optional properties.
82
+
83
+ **Parameters:**
84
+
85
+ | Parameter | Type | Required | Default | Description |
86
+ | ------------ | ------------------------------------- | -------- | ------- | ----------------------- |
87
+ | `event` | `string` | Yes | - | The event name |
88
+ | `properties` | [`EventProperties`](#eventproperties) | No | `{}` | Custom event properties |
89
+
90
+ **Example:**
91
+
92
+ ```javascript
93
+ altertable.track('Purchase Completed', {
94
+ product_id: 'p_01jza8fr5efvgbxxdd1bwkd0m5',
95
+ amount: 29.99,
96
+ currency: 'USD',
97
+ });
98
+ ```
99
+
100
+ #### `altertable.page(url)`
101
+
102
+ Tracks a page view event.
103
+
104
+ When [`autoCapture`](#autocapture) is enabled, this method is automatically called when the page URL changes.
105
+
106
+ **Parameters:**
107
+
108
+ | Parameter | Type | Required | Description |
109
+ | --------- | -------- | -------- | ------------ |
110
+ | `url` | `string` | Yes | The page URL |
111
+
112
+ **Example:**
113
+
114
+ ```javascript
115
+ altertable.page('https://example.com/products');
116
+ ```
117
+
118
+ ### User Identification
119
+
120
+ #### `altertable.identify(userId, traits?)`
121
+
122
+ Identifies a user with their ID and optional traits.
123
+
124
+ **Parameters:**
125
+
126
+ | Parameter | Type | Required | Default | Description |
127
+ | --------- | --------------------------- | -------- | ------- | ---------------------------- |
128
+ | `userId` | `string` | Yes | - | The user's unique identifier |
129
+ | `traits` | [`UserTraits`](#usertraits) | No | `{}` | User properties |
130
+
131
+ **Example:**
132
+
133
+ ```javascript
134
+ altertable.identify('u_01jza857w4f23s1hf2s61befmw', {
135
+ email: 'john.doe@example.com',
136
+ name: 'John Doe',
137
+ company: 'Acme Corp',
138
+ role: 'Software Engineer',
139
+ });
140
+ ```
141
+
142
+ #### `altertable.updateTraits(traits)`
143
+
144
+ Updates user traits for the current user.
145
+
146
+ **Parameters:**
147
+
148
+ | Parameter | Type | Required | Description |
149
+ | --------- | --------------------------- | -------- | ------------------------- |
150
+ | `traits` | [`UserTraits`](#usertraits) | Yes | User properties to update |
151
+
152
+ **Example:**
153
+
154
+ ```javascript
155
+ altertable.updateTraits({
156
+ onboarding_completed: true,
157
+ });
158
+ ```
159
+
160
+ ### Session Management
161
+
162
+ #### `altertable.reset(options?)`
163
+
164
+ Resets visitor and session IDs.
165
+
166
+ **Parameters:**
167
+
168
+ | Parameter | Type | Required | Default | Description |
169
+ | ------------------------ | --------- | -------- | ------- | --------------------------- |
170
+ | `options` | `object` | No | `{}` | Reset options |
171
+ | `options.resetVisitorId` | `boolean` | No | `false` | Whether to reset visitor ID |
172
+ | `options.resetSessionId` | `boolean` | No | `true` | Whether to reset session ID |
173
+
174
+ **Example:**
175
+
176
+ ```javascript
177
+ // Reset session only (default)
178
+ altertable.reset();
179
+
180
+ // Reset both visitor and session
181
+ altertable.reset({
182
+ resetVisitorId: true,
183
+ resetSessionId: true,
184
+ });
185
+ ```
186
+
187
+ ### Configuration
188
+
189
+ #### `altertable.configure(updates)`
190
+
191
+ Updates the configuration after initialization.
192
+
193
+ **Parameters:**
194
+
195
+ | Parameter | Type | Required | Description |
196
+ | --------- | ------------------------------------------------ | -------- | --------------------- |
197
+ | `updates` | [`Partial<AltertableConfig>`](#altertableconfig) | Yes | Configuration updates |
198
+
199
+ **Example:**
200
+
201
+ ```javascript
202
+ altertable.configure({
203
+ trackingConsent: 'granted',
204
+ });
205
+ ```
206
+
207
+ ### Privacy & Consent
208
+
209
+ #### `altertable.getTrackingConsent()`
210
+
211
+ Returns the current tracking consent state.
212
+
213
+ **Returns:** [`TrackingConsentType`](#trackingconsenttype)
214
+
215
+ **Example:**
216
+
217
+ ```javascript
218
+ const consent = altertable.getTrackingConsent();
219
+ if (consent === 'granted') {
220
+ // Tracking is allowed
221
+ }
222
+ ```
223
+
224
+ ## Types
225
+
226
+ ### `AltertableConfig`
227
+
228
+ Configuration options for the Altertable SDK.
229
+
230
+ | Property | Type | Default | Description |
231
+ | ----------------- | --------------------------------------------- | ----------------------------- | ------------------------------------------------------ |
232
+ | `baseUrl` | `string` | `"https://api.altertable.ai"` | The base URL of the Altertable API |
233
+ | `environment` | `string` | `"production"` | The environment of the application |
234
+ | `autoCapture` | `boolean` | `true` | Whether to automatically capture page views and events |
235
+ | `release` | `string` | - | The release ID of the application |
236
+ | `debug` | `boolean` | `false` | Whether to log events to the console |
237
+ | `persistence` | [`StorageType`](#storagetype) | `"localStorage+cookie"` | The persistence strategy for storing IDs |
238
+ | `trackingConsent` | [`TrackingConsentType`](#trackingconsenttype) | `"pending"` | The tracking consent state |
239
+
240
+ ### `EventProperties`
241
+
242
+ Custom properties for events.
243
+
244
+ ```typescript
245
+ type EventProperties = Record<string, unknown>;
246
+ ```
247
+
248
+ **Example:**
249
+
250
+ ```javascript
251
+ {
252
+ product_id: 'p_01jza8fr5efvgbxxdd1bwkd0m5',
253
+ amount: 29.99,
254
+ currency: 'USD',
255
+ category: 'electronics',
256
+ user_type: 'premium'
257
+ }
258
+ ```
259
+
260
+ ### `UserTraits`
261
+
262
+ User properties for identification.
263
+
264
+ ```typescript
265
+ type UserTraits = Record<string, unknown>;
266
+ ```
267
+
268
+ **Example:**
269
+
270
+ ```javascript
271
+ {
272
+ email: 'john.doe@example.com',
273
+ name: 'John Doe',
274
+ company: 'Acme Corp',
275
+ role: 'Software Engineer',
276
+ plan: 'premium',
277
+ signup_date: '2024-01-15'
278
+ }
279
+ ```
280
+
281
+ ### `StorageType`
282
+
283
+ Available storage strategies.
284
+
285
+ | Value | Description |
286
+ | ----------------------- | ------------------------------------- |
287
+ | `"localStorage"` | Use localStorage only |
288
+ | `"sessionStorage"` | Use sessionStorage only |
289
+ | `"cookie"` | Use cookies only |
290
+ | `"memory"` | Use in-memory storage (not persisted) |
291
+ | `"localStorage+cookie"` | Use localStorage with cookie fallback |
292
+
293
+ ### `TrackingConsentType`
294
+
295
+ Available tracking consent states.
296
+
297
+ | Value | Description |
298
+ | ------------- | ------------------------------------- |
299
+ | `"granted"` | User has granted consent for tracking |
300
+ | `"denied"` | User has denied consent for tracking |
301
+ | `"pending"` | User hasn't made a decision yet |
302
+ | `"dismissed"` | User dismissed the consent prompt |
303
+
304
+ ## License
305
+
306
+ [MIT](./LICENSE)
package/dist/index.d.mts CHANGED
@@ -1,4 +1,19 @@
1
- interface Config {
1
+ declare const TrackingConsent: {
2
+ readonly DENIED: "denied";
3
+ readonly DISMISSED: "dismissed";
4
+ readonly GRANTED: "granted";
5
+ readonly PENDING: "pending";
6
+ };
7
+ type TrackingConsentType = (typeof TrackingConsent)[keyof typeof TrackingConsent];
8
+
9
+ type StorageType = 'localStorage' | 'sessionStorage' | 'cookie' | 'memory' | 'localStorage+cookie';
10
+
11
+ type EventProperties = Record<string, unknown>;
12
+ interface UserTraits extends Record<string, unknown> {
13
+ email?: string;
14
+ }
15
+
16
+ interface AltertableConfig {
2
17
  /**
3
18
  * The base URL of the Altertable API.
4
19
  * @default https://api.altertable.ai
@@ -19,27 +34,51 @@ interface Config {
19
34
  * This is helpful to identify the version of the application an event is coming from.
20
35
  */
21
36
  release?: string;
37
+ /**
38
+ * Whether to log events to the console.
39
+ * @default false
40
+ */
41
+ debug?: boolean;
42
+ /**
43
+ * The persistence strategy for storing IDs.
44
+ * @default "localStorage+cookie"
45
+ */
46
+ persistence?: StorageType;
47
+ /**
48
+ * The tracking consent state.
49
+ * @default "pending"
50
+ */
51
+ trackingConsent?: TrackingConsentType;
22
52
  }
23
- type EventProperties = Record<string, unknown>;
24
53
  declare class Altertable {
25
- private _lastUrl;
26
54
  private _apiKey;
55
+ private _cleanupAutoCapture;
27
56
  private _config;
28
- private _sessionId;
29
- private _visitorId;
30
- private _userId;
57
+ private _eventQueue;
58
+ private _isInitialized;
59
+ private _lastUrl;
60
+ private _logger;
31
61
  private _referrer;
62
+ private _sessionManager;
63
+ private _storage;
64
+ private _storageKey;
32
65
  constructor();
33
- init(apiKey: string, config?: Config): void;
34
- identify(userId: string): void;
66
+ init(apiKey: string, config?: AltertableConfig): () => void;
67
+ configure(updates: Partial<AltertableConfig>): void;
68
+ private _handleAutoCaptureChange;
69
+ identify(userId: string, traits?: UserTraits): void;
70
+ updateTraits(traits: UserTraits): void;
71
+ reset({ resetVisitorId, resetSessionId, }?: {
72
+ resetVisitorId?: boolean;
73
+ resetSessionId?: boolean;
74
+ }): void;
35
75
  page(url: string): void;
36
76
  track(event: string, properties?: EventProperties): void;
77
+ getTrackingConsent(): TrackingConsentType;
37
78
  private _checkForChanges;
79
+ private _getEventContext;
80
+ private _processEvent;
38
81
  private _request;
39
- private _getSessionId;
40
- private _getVisitorId;
41
- private _generateId;
42
- private _getViewport;
43
82
  }
44
83
 
45
84
  declare global {
@@ -49,4 +88,4 @@ declare global {
49
88
  }
50
89
  declare const altertable: Altertable;
51
90
 
52
- export { Altertable, altertable };
91
+ export { Altertable, type AltertableConfig, TrackingConsent, altertable };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,19 @@
1
- interface Config {
1
+ declare const TrackingConsent: {
2
+ readonly DENIED: "denied";
3
+ readonly DISMISSED: "dismissed";
4
+ readonly GRANTED: "granted";
5
+ readonly PENDING: "pending";
6
+ };
7
+ type TrackingConsentType = (typeof TrackingConsent)[keyof typeof TrackingConsent];
8
+
9
+ type StorageType = 'localStorage' | 'sessionStorage' | 'cookie' | 'memory' | 'localStorage+cookie';
10
+
11
+ type EventProperties = Record<string, unknown>;
12
+ interface UserTraits extends Record<string, unknown> {
13
+ email?: string;
14
+ }
15
+
16
+ interface AltertableConfig {
2
17
  /**
3
18
  * The base URL of the Altertable API.
4
19
  * @default https://api.altertable.ai
@@ -19,27 +34,51 @@ interface Config {
19
34
  * This is helpful to identify the version of the application an event is coming from.
20
35
  */
21
36
  release?: string;
37
+ /**
38
+ * Whether to log events to the console.
39
+ * @default false
40
+ */
41
+ debug?: boolean;
42
+ /**
43
+ * The persistence strategy for storing IDs.
44
+ * @default "localStorage+cookie"
45
+ */
46
+ persistence?: StorageType;
47
+ /**
48
+ * The tracking consent state.
49
+ * @default "pending"
50
+ */
51
+ trackingConsent?: TrackingConsentType;
22
52
  }
23
- type EventProperties = Record<string, unknown>;
24
53
  declare class Altertable {
25
- private _lastUrl;
26
54
  private _apiKey;
55
+ private _cleanupAutoCapture;
27
56
  private _config;
28
- private _sessionId;
29
- private _visitorId;
30
- private _userId;
57
+ private _eventQueue;
58
+ private _isInitialized;
59
+ private _lastUrl;
60
+ private _logger;
31
61
  private _referrer;
62
+ private _sessionManager;
63
+ private _storage;
64
+ private _storageKey;
32
65
  constructor();
33
- init(apiKey: string, config?: Config): void;
34
- identify(userId: string): void;
66
+ init(apiKey: string, config?: AltertableConfig): () => void;
67
+ configure(updates: Partial<AltertableConfig>): void;
68
+ private _handleAutoCaptureChange;
69
+ identify(userId: string, traits?: UserTraits): void;
70
+ updateTraits(traits: UserTraits): void;
71
+ reset({ resetVisitorId, resetSessionId, }?: {
72
+ resetVisitorId?: boolean;
73
+ resetSessionId?: boolean;
74
+ }): void;
35
75
  page(url: string): void;
36
76
  track(event: string, properties?: EventProperties): void;
77
+ getTrackingConsent(): TrackingConsentType;
37
78
  private _checkForChanges;
79
+ private _getEventContext;
80
+ private _processEvent;
38
81
  private _request;
39
- private _getSessionId;
40
- private _getVisitorId;
41
- private _generateId;
42
- private _getViewport;
43
82
  }
44
83
 
45
84
  declare global {
@@ -49,4 +88,4 @@ declare global {
49
88
  }
50
89
  declare const altertable: Altertable;
51
90
 
52
- export { Altertable, altertable };
91
+ export { Altertable, type AltertableConfig, TrackingConsent, altertable };