@sessionlens/sdk 0.2.0 → 0.3.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.
package/README.md CHANGED
@@ -45,6 +45,125 @@ sessionLens.sessionLensIdentify({
45
45
  sessionLens.sessionLensReset();
46
46
  ```
47
47
 
48
+ ## Identity and anonymous tracking (0.3.0+)
49
+
50
+ `user_id` is optional. Without it, events are tracked against a persistent anonymous device id
51
+ (`localStorage._sl_device_id`). Link the device to a person when you know who they are:
52
+
53
+ ```javascript
54
+ await sessionLens.sessionLensInit({ sdk_key: '...', org_id: '...' }); // no user_id needed
55
+
56
+ sessionLens.sessionLensTrack({ event_name: 'pricing_viewed' }); // anonymous
57
+
58
+ sessionLens.identify('user-123', { name: 'Ada Lovelace', email: 'ada@example.com', plan: 'pro' });
59
+ sessionLens.setUserProperties({ company: 'Analytical Engines' }); // name/email map to core profile fields
60
+
61
+ sessionLens.reset(); // on logout: new anonymous identity
62
+ ```
63
+
64
+ - Identifying again on the same device (a different user) links the device to the new user; past events keep their original attribution.
65
+ - `alias(newId, oldId)` is deprecated and simply calls `identify(newId)`.
66
+ - Passing `user_id` to `sessionLensInit` still works and behaves like an implicit `identify()`.
67
+ - Each event is automatically enriched with `$device_id`, `$distinct_id`, `$insert_id`, `$library`, `$library_version`, `$current_url`, `$pathname`, `$referrer`, `$initial_referrer`, UTM parameters, screen size and client browser/OS hints. Browser, OS and geo (from the request IP) are derived on the server.
68
+ - Trait limits: at most 100 keys, keys up to 128 characters, values up to 4 KB. Trait keys may not start with `$` or contain `.`; offending keys are dropped without failing the call.
69
+
70
+ ### Advanced Usage with Custom Configuration
71
+
72
+ ```javascript
73
+ import sessionLens from '@sessionlens/sdk';
74
+
75
+ // Initialize with custom API configuration
76
+ await sessionLens.sessionLensInit({
77
+ sdk_key: 'your-sdk-key-here',
78
+ user_id: 'user123',
79
+ org_id: 'org456',
80
+ api_base_url: 'https://dev-api.sessionlens.com/v1', // Custom API base URL
81
+ endpoints: {
82
+ events: '/custom/events/ingest', // Custom events endpoint
83
+ identify: '/custom/user/identify', // Custom identify endpoint
84
+ reset: '/custom/session/reset', // Custom reset endpoint
85
+ validate: '/custom/org/validate' // Custom validation endpoint
86
+ },
87
+ debug: true
88
+ });
89
+
90
+ // Track events with rich properties
91
+ sessionLens.sessionLensTrack({
92
+ event_name: 'user_action',
93
+ properties: {
94
+ action_type: 'form_submit',
95
+ form_name: 'contact_form',
96
+ fields_completed: 5,
97
+ time_spent: 120, // seconds
98
+ user_segment: 'premium',
99
+ referrer: document.referrer,
100
+ user_agent: navigator.userAgent
101
+ },
102
+ timestamp: Date.now() // Optional: auto-generated if not provided
103
+ });
104
+
105
+ // Identify user with comprehensive properties
106
+ sessionLens.sessionLensIdentify({
107
+ user_id: 'user123',
108
+ properties: {
109
+ email: 'user@example.com',
110
+ name: 'John Doe',
111
+ plan: 'premium',
112
+ company: 'Acme Corp',
113
+ signup_date: '2024-01-15',
114
+ last_login: new Date().toISOString(),
115
+ preferences: {
116
+ theme: 'dark',
117
+ language: 'en',
118
+ notifications: true
119
+ }
120
+ }
121
+ });
122
+ ```
123
+
124
+ ### Environment-Specific Configuration
125
+
126
+ ```javascript
127
+ import sessionLens from '@sessionlens/sdk';
128
+
129
+ // Determine environment
130
+ const environment = process.env.NODE_ENV || 'development';
131
+
132
+ // Environment-specific configuration
133
+ const configs = {
134
+ development: {
135
+ api_base_url: 'https://dev-api.sessionlens.com/v1',
136
+ debug: true
137
+ },
138
+ staging: {
139
+ api_base_url: 'https://staging-api.sessionlens.com/v1',
140
+ debug: true
141
+ },
142
+ production: {
143
+ api_base_url: 'https://api.sessionlens.com/v1',
144
+ debug: false
145
+ }
146
+ };
147
+
148
+ // Initialize with environment-specific config
149
+ await sessionLens.sessionLensInit({
150
+ sdk_key: 'your-sdk-key-here',
151
+ user_id: 'user123',
152
+ org_id: 'org456',
153
+ ...configs[environment]
154
+ });
155
+
156
+ // Track environment info
157
+ sessionLens.sessionLensTrack({
158
+ event_name: 'app_initialized',
159
+ properties: {
160
+ environment: environment,
161
+ version: '1.0.0',
162
+ timestamp: Date.now()
163
+ }
164
+ });
165
+ ```
166
+
48
167
  ### React Integration
49
168
 
50
169
  ```jsx
@@ -86,6 +205,103 @@ function App() {
86
205
  }
87
206
  ```
88
207
 
208
+ ### React Integration with Custom Configuration
209
+
210
+ ```jsx
211
+ import React from 'react';
212
+ import { useSessionLens, useSessionLensTrack, useSessionLensIdentify } from '@sessionlens/sdk';
213
+
214
+ function App() {
215
+ // Environment-specific configuration
216
+ const environment = process.env.NODE_ENV || 'development';
217
+
218
+ const configs = {
219
+ development: {
220
+ api_base_url: 'https://dev-api.sessionlens.com/v1',
221
+ debug: true
222
+ },
223
+ staging: {
224
+ api_base_url: 'https://staging-api.sessionlens.com/v1',
225
+ debug: true
226
+ },
227
+ production: {
228
+ api_base_url: 'https://api.sessionlens.com/v1',
229
+ debug: false
230
+ }
231
+ };
232
+
233
+ // Initialize SDK with environment-specific config
234
+ useSessionLens({
235
+ sdk_key: 'your-sdk-key-here',
236
+ user_id: 'user123',
237
+ org_id: 'org456',
238
+ ...configs[environment]
239
+ });
240
+
241
+ const track = useSessionLensTrack();
242
+ const identify = useSessionLensIdentify();
243
+
244
+ const handlePageView = () => {
245
+ track({
246
+ event_name: 'page_view',
247
+ properties: {
248
+ page: window.location.pathname,
249
+ referrer: document.referrer,
250
+ environment: environment,
251
+ timestamp: Date.now()
252
+ }
253
+ });
254
+ };
255
+
256
+ const handleUserAction = (action, details) => {
257
+ track({
258
+ event_name: 'user_action',
259
+ properties: {
260
+ action_type: action,
261
+ ...details,
262
+ user_agent: navigator.userAgent,
263
+ screen_resolution: `${screen.width}x${screen.height}`
264
+ }
265
+ });
266
+ };
267
+
268
+ const handleUserLogin = (userData) => {
269
+ identify({
270
+ user_id: userData.id,
271
+ properties: {
272
+ email: userData.email,
273
+ name: userData.name,
274
+ login_method: userData.loginMethod,
275
+ last_login: new Date().toISOString(),
276
+ preferences: userData.preferences
277
+ }
278
+ });
279
+ };
280
+
281
+ // Track page view on component mount
282
+ React.useEffect(() => {
283
+ handlePageView();
284
+ }, []);
285
+
286
+ return (
287
+ <div>
288
+ <button onClick={() => handleUserAction('button_click', { button: 'header_cta' })}>
289
+ Track Event
290
+ </button>
291
+ <button onClick={() => handleUserLogin({
292
+ id: 'user123',
293
+ email: 'user@example.com',
294
+ name: 'John Doe',
295
+ loginMethod: 'email',
296
+ preferences: { theme: 'dark' }
297
+ })}>
298
+ Identify User
299
+ </button>
300
+ </div>
301
+ );
302
+ }
303
+ ```
304
+
89
305
  ## Configuration
90
306
 
91
307
  ### Runtime Configuration
@@ -1,14 +1,16 @@
1
1
  export interface SessionLensConfig {
2
2
  sdk_key: string;
3
- user_id: string;
3
+ user_id?: string;
4
4
  org_id: string;
5
5
  project_id?: string;
6
+ app?: string;
6
7
  debug?: boolean;
7
8
  api_base_url?: string;
8
9
  endpoints?: {
9
10
  events?: string;
10
11
  identify?: string;
11
12
  reset?: string;
13
+ userProperties?: string;
12
14
  validate?: string;
13
15
  };
14
16
  }
@@ -17,10 +19,11 @@ interface SessionInfo {
17
19
  client_id: string;
18
20
  org_id: string;
19
21
  project_id: string;
20
- user_id: string;
22
+ user_id?: string;
21
23
  started_at: number;
22
24
  last_activity: number;
23
25
  }
26
+ export declare function restartSession(): void;
24
27
  export declare function sessionLensInit(config: SessionLensConfig): Promise<void>;
25
28
  export declare function sessionLensGetConfig(): SessionLensConfig;
26
29
  export declare function getSessionInfo(): SessionInfo;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * System properties attached to every event. All browser access is guarded so this
3
+ * is safe to call during SSR (only identity/library fields are returned there).
4
+ * The server re-derives browser/os/geo authoritatively; the client UA fields are a fallback.
5
+ */
6
+ export declare function buildSystemProperties(clientTimestamp: number): Record<string, any>;
@@ -0,0 +1,14 @@
1
+ export interface Identity {
2
+ device_id: string;
3
+ distinct_id: string;
4
+ user_id?: string;
5
+ }
6
+ export declare function getDeviceId(): string;
7
+ /** Current identity. Anonymous visitors have distinct_id === device_id and no user_id. */
8
+ export declare function getIdentity(): Identity;
9
+ /** Persist a user identity. From now on events carry distinct_id = userId. */
10
+ export declare function setUser(userId: string): void;
11
+ /** Forget the user and start a fresh anonymous identity on a new device id. */
12
+ export declare function resetIdentity(): Identity;
13
+ /** Referrer of the very first page load, persisted once ('$direct' when there was none). */
14
+ export declare function getInitialReferrer(currentReferrer: string): string;
@@ -1,4 +1,19 @@
1
+ import { SessionLensConfig } from './config';
1
2
  import { SessionLensEvent, SessionLensUser } from '../types';
3
+ /**
4
+ * Initialize the SDK. If `config.user_id` is supplied (pre-0.3 integrations) it is treated
5
+ * as an implicit identify() - sent only when it differs from the persisted identity.
6
+ */
7
+ export declare function sessionLensInit(config: SessionLensConfig): Promise<void>;
2
8
  export declare function sessionLensTrack(event: SessionLensEvent): void;
9
+ /** Link the current device to `userId` (last identify wins) and optionally set traits. */
10
+ export declare function identify(userId: string, traits?: Record<string, any>): void;
11
+ /** @deprecated Aliasing is subsumed by identify(); `oldId` is ignored (the device link is implicit). */
12
+ export declare function alias(newId: string, _oldId?: string): void;
13
+ /** Set traits on the current profile (`name` and `email` map to core profile fields). */
14
+ export declare function setUserProperties(traits: Record<string, any>): void;
15
+ /** Legacy signature: sessionLensIdentify({ user_id, properties }). */
3
16
  export declare function sessionLensIdentify(user: SessionLensUser): void;
4
- export declare function sessionLensReset(): void;
17
+ /** Forget the user, start an anonymous identity on a new device id and a new session. */
18
+ export declare function reset(): void;
19
+ export declare const sessionLensReset: typeof reset;
package/dist/index.d.ts CHANGED
@@ -2,12 +2,18 @@ import { SessionLensConfig } from './core/config';
2
2
  import { SessionLensEvent, SessionLensUser, SessionLensInstance } from './types';
3
3
  export * from './types';
4
4
  export { SessionLensConfig } from './core/config';
5
+ export { SDK_VERSION } from './version';
5
6
  export * from './integrations/react';
6
7
  declare class SessionLens implements SessionLensInstance {
7
8
  sessionLensInit(config: SessionLensConfig): Promise<void>;
8
9
  sessionLensTrack(event: SessionLensEvent): void;
9
10
  sessionLensIdentify(user: SessionLensUser): void;
10
11
  sessionLensReset(): void;
12
+ identify(userId: string, traits?: Record<string, any>): void;
13
+ /** @deprecated Use identify(). */
14
+ alias(newId: string, oldId?: string): void;
15
+ setUserProperties(traits: Record<string, any>): void;
16
+ reset(): void;
11
17
  }
12
18
  declare const sessionLens: SessionLens;
13
19
  export default sessionLens;