diff --git a/packages/altertable-js/README.md b/packages/altertable-js/README.md index e8eb67a..ee6f172 100644 --- a/packages/altertable-js/README.md +++ b/packages/altertable-js/README.md @@ -47,7 +47,7 @@ altertable.alias('new_user_id-019aca6a-1e42-71af-81a0-1e14bbe2ccbd'); ## Features - **Automatic page view tracking** – Captures page views automatically -- **Session management** – Handles visitor and session IDs automatically +- **Session management** – Handles anonymous and session IDs automatically - **Event queuing** – Queues events when offline or consent is pending - **Privacy compliance** – Built-in tracking consent management - **Multiple storage options** – localStorage, cookies, or both diff --git a/packages/altertable-js/src/constants.ts b/packages/altertable-js/src/constants.ts index 94a582b..b96657a 100644 --- a/packages/altertable-js/src/constants.ts +++ b/packages/altertable-js/src/constants.ts @@ -5,7 +5,7 @@ export const keyBuilder = createKeyBuilder(STORAGE_KEY_PREFIX, '.'); export const STORAGE_KEY_TEST = keyBuilder('check'); export const PREFIX_SESSION_ID = 'session'; -export const PREFIX_VISITOR_ID = 'visitor'; +export const PREFIX_ANONYMOUS_ID = 'anonymous'; export const PREFIX_DEVICE_ID = 'device'; const MINUTE_IN_MS = 1_000 * 60; diff --git a/packages/altertable-js/src/lib/logger.ts b/packages/altertable-js/src/lib/logger.ts index 2c66905..c2fd711 100644 --- a/packages/altertable-js/src/lib/logger.ts +++ b/packages/altertable-js/src/lib/logger.ts @@ -56,9 +56,9 @@ export function createLogger(prefix: string) { const [userValueLabel, userValueStyle] = createValueElement( payload.distinct_id ?? 'Not set' ); - const [visitorLabel, visitorLabelStyle] = - createEventLabelElement('Visitor ID'); - const [visitorValueLabel, visitorValueStyle] = createValueElement( + const [anonymousLabel, anonymousLabelStyle] = + createEventLabelElement('Anonymous ID'); + const [anonymousValueLabel, anonymousValueStyle] = createValueElement( payload.anonymous_id ?? 'Not set' ); const [sessionLabel, sessionLabelStyle] = @@ -73,9 +73,9 @@ export function createLogger(prefix: string) { userValueStyle ); console.log( - `%c${visitorLabel} %c${visitorValueLabel}`, - visitorLabelStyle, - visitorValueStyle + `%c${anonymousLabel} %c${anonymousValueLabel}`, + anonymousLabelStyle, + anonymousValueStyle ); console.log( `%c${sessionLabel} %c${sessionValueLabel}`, diff --git a/packages/altertable-js/src/lib/sessionManager.ts b/packages/altertable-js/src/lib/sessionManager.ts index 994ca17..0c02ce4 100644 --- a/packages/altertable-js/src/lib/sessionManager.ts +++ b/packages/altertable-js/src/lib/sessionManager.ts @@ -1,17 +1,17 @@ import { + PREFIX_ANONYMOUS_ID, PREFIX_DEVICE_ID, PREFIX_SESSION_ID, - PREFIX_VISITOR_ID, SESSION_EXPIRATION_TIME_MS, TrackingConsent, TrackingConsentType, } from '../constants'; import type { + AnonymousId, DeviceId, DistinctId, SessionId, UserId, - VisitorId, } from '../types'; import { generateId } from './generateId'; import { Logger } from './logger'; @@ -20,7 +20,7 @@ import { type StorageApi } from './storage'; type SessionData = { deviceId: DeviceId; distinctId: DistinctId; - anonymousId: VisitorId | null; + anonymousId: AnonymousId | null; sessionId: SessionId; lastEventAt: string | null; trackingConsent: TrackingConsentType; @@ -61,7 +61,7 @@ export class SessionManager { this._sessionData = { deviceId: parsedData.deviceId || this._generateDeviceId(), - distinctId: parsedData.distinctId || this._generateVisitorId(), + distinctId: parsedData.distinctId || this._generateAnonymousId(), sessionId: parsedData.sessionId || this._generateSessionId(), anonymousId: parsedData.anonymousId || null, lastEventAt: parsedData.lastEventAt || null, @@ -91,7 +91,7 @@ export class SessionManager { return this._sessionData.distinctId; } - getAnonymousId(): VisitorId | null { + getAnonymousId(): AnonymousId | null { return this._sessionData.anonymousId; } @@ -103,13 +103,13 @@ export class SessionManager { * * When transitioning from anonymous to identified, we preserve the anonymous ID * to enable identity merging on the backend. This allows: - * - Linking pre-identification events (anonymous visitor ID) to post-identification events (user ID) + * - Linking pre-identification events (anonymous ID) to post-identification events (user ID) * - Merging user profiles so anonymous browsing behavior is associated with the identified user * - Maintaining a complete user journey from first visit through identification * * **State Transitions:** - * - **Anonymous:** `anonymousId = null`, `distinctId = visitorId`, `isIdentified() = false` - * - **Identified:** `anonymousId = previous visitorId`, `distinctId = userId`, `isIdentified() = true` + * - **Anonymous:** `anonymousId = null`, `distinctId = anonymousId`, `isIdentified() = false` + * - **Identified:** `anonymousId = previous distinctId`, `distinctId = userId`, `isIdentified() = true` */ isIdentified(): boolean { return Boolean(this._sessionData.anonymousId); @@ -124,7 +124,7 @@ export class SessionManager { } identify(userId: UserId): void { - this._sessionData.anonymousId = this._sessionData.distinctId as VisitorId; + this._sessionData.anonymousId = this._sessionData.distinctId as AnonymousId; this._sessionData.distinctId = userId; this._persistToStorage(); } @@ -168,7 +168,7 @@ export class SessionManager { this._sessionData.sessionId = this._generateSessionId(); this._sessionData.anonymousId = null; - this._sessionData.distinctId = this._generateVisitorId(); + this._sessionData.distinctId = this._generateAnonymousId(); this._sessionData.lastEventAt = null; this._persistToStorage(); } @@ -177,7 +177,7 @@ export class SessionManager { return { anonymousId: null, deviceId: this._generateDeviceId(), - distinctId: this._generateVisitorId(), + distinctId: this._generateAnonymousId(), lastEventAt: null, sessionId: this._generateSessionId(), trackingConsent: this._defaultTrackingConsent, @@ -192,8 +192,8 @@ export class SessionManager { return generateId(PREFIX_DEVICE_ID); } - private _generateVisitorId(): VisitorId { - return generateId(PREFIX_VISITOR_ID); + private _generateAnonymousId(): AnonymousId { + return generateId(PREFIX_ANONYMOUS_ID); } private _shouldRenewSession(): boolean { diff --git a/packages/altertable-js/src/types.ts b/packages/altertable-js/src/types.ts index 6123f5b..ad90c2a 100644 --- a/packages/altertable-js/src/types.ts +++ b/packages/altertable-js/src/types.ts @@ -5,9 +5,9 @@ export type EventType = 'track' | 'identify' | 'alias'; export type EventProperties = Record; export type UserId = string; -export type DistinctId = StringWithAutocomplete; +export type DistinctId = StringWithAutocomplete; export type DeviceId = `device-${string}`; -export type VisitorId = `visitor-${string}`; +export type AnonymousId = `anonymous-${string}`; export type SessionId = `session-${string}`; export type Environment = StringWithAutocomplete< 'production' | 'development' | 'staging' @@ -21,7 +21,7 @@ export type AltertableContext = { environment: Environment; device_id: DeviceId; distinct_id: DistinctId; - anonymous_id: VisitorId | null; + anonymous_id: AnonymousId | null; session_id: SessionId; }; diff --git a/packages/altertable-js/test/core.test.ts b/packages/altertable-js/test/core.test.ts index 5e21c7a..d3712ec 100644 --- a/packages/altertable-js/test/core.test.ts +++ b/packages/altertable-js/test/core.test.ts @@ -9,9 +9,9 @@ import { } from '../../../test-utils/networkMode'; import { EVENT_PAGEVIEW, + PREFIX_ANONYMOUS_ID, PREFIX_DEVICE_ID, PREFIX_SESSION_ID, - PREFIX_VISITOR_ID, PROPERTY_LIB, PROPERTY_LIB_VERSION, PROPERTY_REFERER, @@ -28,11 +28,11 @@ import { UserId, UserTraits } from '../src/types'; const REGEXP_DATE_ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/; const REGEXP_SESSION_ID = new RegExp(`^${PREFIX_SESSION_ID}-`); const REGEXP_DEVICE_ID = new RegExp(`^${PREFIX_DEVICE_ID}-`); -const REGEXP_VISITOR_ID = new RegExp(`^${PREFIX_VISITOR_ID}-`); +const REGEXP_ANONYMOUS_ID = new RegExp(`^${PREFIX_ANONYMOUS_ID}-`); function createSessionData(overrides: Record = {}) { return JSON.stringify({ - distinctId: 'visitor-test-uuid-1-1234567890', + distinctId: 'anonymous-test-uuid-1-1234567890', anonymousId: null, sessionId: 'session-test-uuid-2-1234567890', deviceId: 'device-test-uuid-3-1234567890', @@ -134,7 +134,7 @@ describe('Altertable', () => { event: EVENT_PAGEVIEW, timestamp: expect.stringMatching(REGEXP_DATE_ISO), device_id: expect.stringMatching(REGEXP_DEVICE_ID), - distinct_id: expect.stringMatching(REGEXP_VISITOR_ID), + distinct_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), anonymous_id: null, session_id: expect.stringMatching(REGEXP_SESSION_ID), environment: 'production', @@ -165,7 +165,7 @@ describe('Altertable', () => { payload: { event: 'eventName', device_id: expect.stringMatching(REGEXP_DEVICE_ID), - distinct_id: expect.stringMatching(REGEXP_VISITOR_ID), + distinct_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), timestamp: expect.stringMatching(REGEXP_DATE_ISO), anonymous_id: null, session_id: expect.stringMatching(REGEXP_SESSION_ID), @@ -611,7 +611,7 @@ describe('Altertable', () => { device_id: expect.stringMatching(REGEXP_DEVICE_ID), traits: {}, distinct_id: userId, - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), }, }); }); @@ -629,7 +629,7 @@ describe('Altertable', () => { device_id: expect.stringMatching(REGEXP_DEVICE_ID), traits, distinct_id: userId, - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), }, }); }); @@ -747,7 +747,7 @@ describe('Altertable', () => { device_id: expect.stringMatching(REGEXP_DEVICE_ID), traits: { email: 'user@example.com' }, distinct_id: userId, - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), }, }); @@ -759,7 +759,7 @@ describe('Altertable', () => { device_id: expect.stringMatching(REGEXP_DEVICE_ID), traits: { email: 'user@example.com' }, distinct_id: userId, - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), }, }); }); @@ -799,7 +799,7 @@ describe('Altertable', () => { device_id: expect.stringMatching(REGEXP_DEVICE_ID), traits: newTraits, distinct_id: 'user123', - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), session_id: expect.stringMatching(REGEXP_SESSION_ID), }, }); @@ -881,7 +881,7 @@ describe('Altertable', () => { environment: expect.any(String), device_id: expect.stringMatching(REGEXP_DEVICE_ID), new_user_id: 'user456', - distinct_id: expect.stringMatching(REGEXP_VISITOR_ID), + distinct_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), anonymous_id: null, }) ); @@ -933,7 +933,7 @@ describe('Altertable', () => { }).toRequestApi('/alias', { payload: expect.objectContaining({ new_user_id: 'user456', - distinct_id: expect.stringMatching(REGEXP_VISITOR_ID), + distinct_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), anonymous_id: null, }), }); @@ -954,7 +954,7 @@ describe('Altertable', () => { payload: expect.objectContaining({ new_user_id: 'user456', distinct_id: 'user123', - anonymous_id: expect.stringMatching(REGEXP_VISITOR_ID), + anonymous_id: expect.stringMatching(REGEXP_ANONYMOUS_ID), }), }); }); @@ -973,10 +973,10 @@ describe('Altertable', () => { }); it('persists session ID across page reloads', () => { - const testVisitorId = 'visitor-test-uuid-1-1234567890'; + const testAnonymousId = 'anonymous-test-uuid-1-1234567890'; const testSessionId = 'session-test-uuid-2-1234567890'; const existingSessionData = createSessionData({ - visitorId: testVisitorId, + anonymousId: testAnonymousId, sessionId: testSessionId, }); @@ -999,13 +999,13 @@ describe('Altertable', () => { describe('session renewal', () => { it('regenerates session ID when event sent after 30 minutes', () => { vi.useFakeTimers(); - const testVisitorId = 'visitor-test-uuid-3-1234567890'; + const testAnonymousId = 'anonymous-test-uuid-3-1234567890'; const testSessionId = 'session-test-uuid-4-1234567890'; const thirtyMinutesAgo = new Date( Date.now() - 30 * 60 * 1000 - 1000 ).toISOString(); const existingSessionData = createSessionData({ - visitorId: testVisitorId, + anonymousId: testAnonymousId, sessionId: testSessionId, lastEventAt: thirtyMinutesAgo, }); @@ -1045,13 +1045,13 @@ describe('Altertable', () => { it('does not regenerate session ID when event sent within 30 minutes', () => { vi.useFakeTimers(); - const testVisitorId = 'visitor-test-uuid-5-1234567890'; + const testAnonymousId = 'anonymous-test-uuid-5-1234567890'; const testSessionId = 'session-test-uuid-6-1234567890'; const twentyNineMinutesAgo = new Date( Date.now() - 29 * 60 * 1000 ).toISOString(); const existingSessionData = createSessionData({ - visitorId: testVisitorId, + anonymousId: testAnonymousId, sessionId: testSessionId, lastEventAt: twentyNineMinutesAgo, }); @@ -1099,7 +1099,7 @@ describe('Altertable', () => { altertable.reset(); const distinctId = altertable['_sessionManager'].getDistinctId(); - expect(distinctId).toMatch(REGEXP_VISITOR_ID); + expect(distinctId).toMatch(REGEXP_ANONYMOUS_ID); const anonymousId = altertable['_sessionManager'].getAnonymousId(); expect(anonymousId).toBeNull(); }); @@ -1120,7 +1120,7 @@ describe('Altertable', () => { expect(newSessionId).toMatch(REGEXP_SESSION_ID); const newDistinctId = altertable['_sessionManager'].getDistinctId(); - expect(newDistinctId).toMatch(REGEXP_VISITOR_ID); + expect(newDistinctId).toMatch(REGEXP_ANONYMOUS_ID); const newAnonymousId = altertable['_sessionManager'].getAnonymousId(); expect(newAnonymousId).toBeNull(); }); @@ -1174,7 +1174,7 @@ describe('Altertable', () => { ]; const storedData = JSON.parse(lastCall[1]); expect(storedData).toMatchObject({ - anonymousId: expect.stringMatching(REGEXP_VISITOR_ID), + anonymousId: expect.stringMatching(REGEXP_ANONYMOUS_ID), sessionId: expect.stringMatching(REGEXP_SESSION_ID), distinctId: 'user123', lastEventAt: null, @@ -1182,7 +1182,7 @@ describe('Altertable', () => { }); it('recovers storage data on initialization', () => { - const testAnonymousId = 'visitor-test-uuid-3-1234567890'; + const testAnonymousId = 'anonymous-test-uuid-3-1234567890'; const testSessionId = 'session-test-uuid-4-1234567890'; const existingData = createSessionData({ anonymousId: testAnonymousId, @@ -1233,7 +1233,7 @@ describe('Altertable', () => { const distinctId = altertable['_sessionManager'].getDistinctId(); const sessionId = altertable['_sessionManager'].getSessionId(); const anonymousId = altertable['_sessionManager'].getAnonymousId(); - expect(distinctId).toMatch(REGEXP_VISITOR_ID); + expect(distinctId).toMatch(REGEXP_ANONYMOUS_ID); expect(sessionId).toMatch(REGEXP_SESSION_ID); expect(anonymousId).toBeNull(); }); @@ -1635,7 +1635,7 @@ describe('Altertable', () => { // 6. Reset (should clear state) altertable.reset(); expect(altertable['_sessionManager'].getDistinctId()).toMatch( - REGEXP_VISITOR_ID + REGEXP_ANONYMOUS_ID ); expect(altertable['_sessionManager'].getAnonymousId()).toBeNull(); expect(altertable['_sessionManager'].getSessionId()).toMatch( diff --git a/packages/altertable-js/test/logger.test.ts b/packages/altertable-js/test/logger.test.ts index aa42cc0..98aa38c 100644 --- a/packages/altertable-js/test/logger.test.ts +++ b/packages/altertable-js/test/logger.test.ts @@ -96,7 +96,7 @@ describe('Logger', () => { device_id: 'device-123', distinct_id: 'user123', session_id: 'session-123', - anonymous_id: 'visitor-123', + anonymous_id: 'anonymous-123', environment: 'development', properties: { key1: 'value1', @@ -134,7 +134,7 @@ describe('Logger', () => { ); }); - it('handles undefined user_id', () => { + it('handles undefined distinct_id', () => { const logger = createLogger('TestLogger'); const payloadWithoutUser: TrackPayload = { ...mockEventPayload, @@ -150,29 +150,31 @@ describe('Logger', () => { ); }); - it('logs visitor information', () => { + it('logs anonymous information', () => { const logger = createLogger('TestLogger'); logger.logEvent(mockEventPayload, { trackingConsent: 'granted' }); expect(console.log).toHaveBeenCalledWith( - '%cVisitor ID %cvisitor-123', + '%cAnonymous ID %canonymous-123', expect.any(String), expect.any(String) ); }); - it('handles undefined visitor_id', () => { + it('handles undefined anonymous_id', () => { const logger = createLogger('TestLogger'); - const payloadWithoutVisitor: TrackPayload = { + const payloadWithoutAnonymousId: TrackPayload = { ...mockEventPayload, anonymous_id: undefined, }; - logger.logEvent(payloadWithoutVisitor, { trackingConsent: 'granted' }); + logger.logEvent(payloadWithoutAnonymousId, { + trackingConsent: 'granted', + }); expect(console.log).toHaveBeenCalledWith( - '%cVisitor ID %cNot set', + '%cAnonymous ID %cNot set', expect.any(String), expect.any(String) ); @@ -327,7 +329,7 @@ describe('Logger', () => { }, device_id: 'device-123', distinct_id: 'user123', - anonymous_id: 'visitor-123' as const, + anonymous_id: 'anonymous-123' as const, }; it('logs identify event with all components', () => { @@ -376,13 +378,13 @@ describe('Logger', () => { ); }); - it('logs visitor information', () => { + it('logs anonymous information', () => { const logger = createLogger('TestLogger'); logger.logIdentify(mockIdentifyPayload, { trackingConsent: 'granted' }); expect(console.log).toHaveBeenCalledWith( - '%cAnonymous ID %cvisitor-123', + '%cAnonymous ID %canonymous-123', expect.any(String), expect.any(String) ); @@ -469,7 +471,7 @@ describe('Logger', () => { new_user_id: 'user--456', device_id: 'device-123', distinct_id: 'user123', - anonymous_id: 'visitor-123' as const, + anonymous_id: 'anonymous-123' as const, }; it('logs alias event with all components', () => { diff --git a/packages/altertable-js/test/requester.test.ts b/packages/altertable-js/test/requester.test.ts index 41ab94e..02be4c9 100644 --- a/packages/altertable-js/test/requester.test.ts +++ b/packages/altertable-js/test/requester.test.ts @@ -21,7 +21,7 @@ function createTrackEventPayload( environment: 'test', device_id: 'device-0197d9df-3c3b-734e-96dd-dfda52b0167c', distinct_id: 'u_01jzcxxwcgfzztabq1e3dk1y8q', - anonymous_id: 'visitor-0197d9df-3c3b-734e-96dd-dfda52b0167c', + anonymous_id: 'anonymous-0197d9df-3c3b-734e-96dd-dfda52b0167c', session_id: 'session-0197d9df-4e77-72cb-bf0a-e35b3f1f5425', ...overrides, }; @@ -35,7 +35,7 @@ function createIdentifyEventPayload( environment: 'test', device_id: 'device-0197d9df-3c3b-734e-96dd-dfda52b0167c', distinct_id: 'u_01jzcxxwcgfzztabq1e3dk1y8q', - anonymous_id: 'visitor-0197d9df-3c3b-734e-96dd-dfda52b0167c', + anonymous_id: 'anonymous-0197d9df-3c3b-734e-96dd-dfda52b0167c', ...overrides, }; } diff --git a/packages/altertable-js/test/sessionManager.test.ts b/packages/altertable-js/test/sessionManager.test.ts index 53ce58c..b494614 100644 --- a/packages/altertable-js/test/sessionManager.test.ts +++ b/packages/altertable-js/test/sessionManager.test.ts @@ -5,9 +5,9 @@ import { StorageMock, } from '../../../test-utils/mocks/storageMock'; import { + PREFIX_ANONYMOUS_ID, PREFIX_DEVICE_ID, PREFIX_SESSION_ID, - PREFIX_VISITOR_ID, SESSION_EXPIRATION_TIME_MS, TrackingConsent, } from '../src/constants'; @@ -54,7 +54,7 @@ describe('SessionManager with tracking consent', () => { mockStorage.getItem.mockReturnValue( JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -77,7 +77,7 @@ describe('SessionManager with tracking consent', () => { mockStorage.getItem.mockReturnValue( JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -153,7 +153,7 @@ describe('SessionManager with tracking consent', () => { it('should handle tracking consent in session data recovery', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - anonymousId: 'visitor-test-1', + anonymousId: 'anonymous-test-1', sessionId: 'session-test-1', distinctId: 'user123', lastEventAt: '2023-01-01T00:00:00.000Z', @@ -179,7 +179,7 @@ describe('SessionManager with tracking consent', () => { it('should handle missing tracking consent in stored data', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -202,7 +202,7 @@ describe('SessionManager with tracking consent', () => { it('should handle null tracking consent in stored data', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -225,7 +225,7 @@ describe('SessionManager with tracking consent', () => { it('should handle undefined tracking consent in stored data', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -259,9 +259,9 @@ describe('SessionManager with tracking consent', () => { expect(sessionManager.isIdentified()).toBe(false); expect(sessionManager.getAnonymousId()).toBe(null); - // distinctId should be a visitor ID when anonymous - const visitorId = sessionManager.getDistinctId(); - expect(visitorId).toMatch(PREFIX_VISITOR_ID); + // distinctId should be a anonymous ID when anonymous + const anonymousId = sessionManager.getDistinctId(); + expect(anonymousId).toMatch(PREFIX_ANONYMOUS_ID); }); }); @@ -269,7 +269,7 @@ describe('SessionManager with tracking consent', () => { it('should return false when anonymousId is null (anonymous state)', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor_test-1', // visitor ID when anonymous + distinctId: 'anonymous-test-1', // anonymous ID when anonymous anonymousId: null, sessionId: 'session-test-1', lastEventAt: null, @@ -288,14 +288,14 @@ describe('SessionManager with tracking consent', () => { expect(sessionManager.isIdentified()).toBe(false); expect(sessionManager.getAnonymousId()).toBe(null); - expect(sessionManager.getDistinctId()).toBe('visitor_test-1'); + expect(sessionManager.getDistinctId()).toBe('anonymous-test-1'); }); it('should return true when anonymousId is set (identified state)', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', distinctId: 'user123', // user ID after identification - anonymousId: 'visitor_test-1', // previous anonymous visitor ID + anonymousId: 'anonymous-test-1', // previous anonymous ID sessionId: 'session-test-1', lastEventAt: null, trackingConsent: TrackingConsent.PENDING, @@ -312,14 +312,14 @@ describe('SessionManager with tracking consent', () => { sessionManager.init(); expect(sessionManager.isIdentified()).toBe(true); - expect(sessionManager.getAnonymousId()).toBe('visitor_test-1'); + expect(sessionManager.getAnonymousId()).toBe('anonymous-test-1'); expect(sessionManager.getDistinctId()).toBe('user123'); }); it('should return false when anonymousId is missing from stored data', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor_test-1', + distinctId: 'anonymous-test-1', sessionId: 'session-test-1', lastEventAt: null, trackingConsent: TrackingConsent.PENDING, @@ -356,12 +356,12 @@ describe('SessionManager with tracking consent', () => { expect(sessionManager.isIdentified()).toBe(false); expect(sessionManager.getAnonymousId()).toBe(null); const initialDistinctId = sessionManager.getDistinctId(); - expect(initialDistinctId).toMatch(PREFIX_VISITOR_ID); + expect(initialDistinctId).toMatch(PREFIX_ANONYMOUS_ID); // Identify the user sessionManager.identify('user123'); - // Now identified: anonymousId stores the previous visitor ID + // Now identified: anonymousId stores the previous anonymous ID expect(sessionManager.isIdentified()).toBe(true); expect(sessionManager.getAnonymousId()).toBe(initialDistinctId); expect(sessionManager.getDistinctId()).toBe('user123'); @@ -382,7 +382,7 @@ describe('SessionManager with tracking consent', () => { expect(sessionManager.getSessionId()).toMatch(PREFIX_SESSION_ID); expect(sessionManager.getDeviceId()).toMatch(PREFIX_DEVICE_ID); - expect(sessionManager.getDistinctId()).toMatch(PREFIX_VISITOR_ID); + expect(sessionManager.getDistinctId()).toMatch(PREFIX_ANONYMOUS_ID); expect(sessionManager.getAnonymousId()).toBe(null); expect(sessionManager.getLastEventAt()).toBe(null); expect(mockStorage.setItem).toHaveBeenCalled(); @@ -391,7 +391,7 @@ describe('SessionManager with tracking consent', () => { it('should load data from storage when available', () => { const storedData = JSON.stringify({ deviceId: 'device-test-1', - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', anonymousId: null, sessionId: 'session-test-1', lastEventAt: '2023-01-01T00:00:00.000Z', @@ -409,7 +409,7 @@ describe('SessionManager with tracking consent', () => { sessionManager.init(); expect(sessionManager.getDeviceId()).toBe('device-test-1'); - expect(sessionManager.getDistinctId()).toBe('visitor-test-1'); + expect(sessionManager.getDistinctId()).toBe('anonymous-test-1'); expect(sessionManager.getSessionId()).toBe('session-test-1'); expect(sessionManager.getLastEventAt()).toBe('2023-01-01T00:00:00.000Z'); }); @@ -435,7 +435,7 @@ describe('SessionManager with tracking consent', () => { it('should generate missing IDs when partial data exists', () => { const storedData = JSON.stringify({ - distinctId: 'visitor-test-1', + distinctId: 'anonymous-test-1', // deviceId and sessionId are missing }); @@ -449,7 +449,7 @@ describe('SessionManager with tracking consent', () => { }); sessionManager.init(); - expect(sessionManager.getDistinctId()).toBe('visitor-test-1'); + expect(sessionManager.getDistinctId()).toBe('anonymous-test-1'); expect(sessionManager.getDeviceId()).toMatch(PREFIX_DEVICE_ID); expect(sessionManager.getSessionId()).toMatch(PREFIX_SESSION_ID); }); @@ -480,7 +480,7 @@ describe('SessionManager with tracking consent', () => { it('should return distinct ID', () => { const distinctId = sessionManager.getDistinctId(); - expect(distinctId).toMatch(PREFIX_VISITOR_ID); + expect(distinctId).toMatch(PREFIX_ANONYMOUS_ID); expect(typeof distinctId).toBe('string'); }); @@ -489,7 +489,7 @@ describe('SessionManager with tracking consent', () => { sessionManager.identify('user123'); const anonymousId = sessionManager.getAnonymousId(); - expect(anonymousId).toMatch(PREFIX_VISITOR_ID); + expect(anonymousId).toMatch(PREFIX_ANONYMOUS_ID); }); it('should return last event timestamp', () => { @@ -600,7 +600,7 @@ describe('SessionManager with tracking consent', () => { expect(sessionManager.isIdentified()).toBe(false); expect(sessionManager.getAnonymousId()).toBe(null); - expect(sessionManager.getDistinctId()).toMatch(PREFIX_VISITOR_ID); + expect(sessionManager.getDistinctId()).toMatch(PREFIX_ANONYMOUS_ID); expect(sessionManager.getSessionId()).not.toBe(initialSessionId); expect(sessionManager.getSessionId()).toMatch(PREFIX_SESSION_ID); expect(sessionManager.getLastEventAt()).toBe(null); diff --git a/packages/altertable-react/README.md b/packages/altertable-react/README.md index b80e4cc..33635e4 100644 --- a/packages/altertable-react/README.md +++ b/packages/altertable-react/README.md @@ -81,7 +81,7 @@ function SignupPage() { ## Features - **Automatic page view tracking** – Captures page views automatically -- **Session management** – Handles visitor and session IDs automatically +- **Session management** – Handles anonymous and session IDs automatically - **Event queuing** – Queues events when offline or consent is pending - **Privacy compliance** – Built-in tracking consent management - **Multiple storage options** – localStorage, cookies, or both