Public API Reference
Runtime Access Models
- Instance API (
npm):
const widget = new Selwise();
await widget.init(...);
- Global API (
CDNalways, npm optional withexposeGlobal: true):
window.Selwise?.track(...);
- React Native API (
@selwise/react-native, headless):
const sdk = new Selwise();
await sdk.init({ siteKey, apiUrl, apiKey });
Lifecycle Methods
| Method | Signature | Availability | Notes |
|---|---|---|---|
init | init(config): Promise<void> | Web + RN | RN throws for missing siteKey/apiKey. |
destroy | destroy(): void | Web + RN | Stops timers/listeners and flush attempts. |
Core Tracking and Identity
| Method | Signature | Availability | Notes |
|---|---|---|---|
track | track(name: string, args?): void | Web + RN | Queues canonical events for batch ingestion. |
trackOrder | trackOrder(order): Promise<void> | Web + RN | Sends order payload for revenue attribution. |
identify | identify(userId: string, metadata?): Promise<{ success: boolean; merged?: boolean }> | Web + RN | Links anonymous and known identities. |
setTraits | setTraits(traits): Promise<boolean> | Web + RN | Updates user profile traits. |
getSiteUserId | getSiteUserId(): string | undefined | Web only | Current identified user in web runtime. |
Session and Journey
| Method | Signature | Availability | Notes |
|---|---|---|---|
getVisitorId | getVisitorId(): string | Web + RN | Stable visitor identifier. |
getSessionId | getSessionId(): string | Web + RN | Current session identifier. |
getJourneyId | getJourneyId(): string | null | Web + RN | Returns null when journey expires/invalid. |
getJourneyState | getJourneyState(): { journeyId: string | null; age: number | null; isValid: boolean } | Web + RN | Snapshot for debugging journey lifecycle. |
endJourney | endJourney(): void | Web + RN | Ends current journey context and starts a new one lazily. |
Experiments and Data Layer
| Method | Signature | Availability | Notes |
|---|---|---|---|
getVariant | getVariant(experimentId: string): any | Web + RN | Returns full assignment payload. |
getExperimentVariant | getExperimentVariant(experimentId: string): string | null | Web + RN | Returns assigned variant id when available. |
getActiveExperiments | getActiveExperiments(): any[] | Web + RN | Snapshot of active experiment assignments. |
pushDataLayer | pushDataLayer(data): void | Web + RN | Web: tracked data-layer integration. RN: local snapshot only. |
getDataLayer | getDataLayer(): any[] | Web + RN | Returns current in-memory/persisted data layer. |
Consent Methods
| Method | Signature | Availability | Notes |
|---|---|---|---|
getConsentState | getConsentState(): ConsentState | null | Web + RN | Reads consent category state. |
grantConsent | grantConsent(categories?): void | Web + RN | Updates local state and syncs public consent endpoint. |
revokeConsent | revokeConsent(categories?): void | Web + RN | Revokes selected consent categories. |
React Native Headless Methods
| Method | Signature | Request Contract | Error Behavior |
|---|---|---|---|
flush | flush(): Promise<void> | Sends queued events to POST /public/sites/:siteKey/events/batch. | Returns early if uninitialized or already flushing. |
setScreenContext | setScreenContext({ name, path, referrer? }): void | Persists screen context used by track/order payloads. | No-op if uninitialized. |
getSiteConfig | getSiteConfig(): Promise<any> | GET /public/sites/:siteKey/config. | Throws if uninitialized or request fails. |
getSearchConfig | getSearchConfig(): Promise<any> | GET /public/sites/:siteKey/search-config. | Throws if uninitialized or request fails. |
search | search(query, options?): Promise<any> | GET /public/sites/:siteKey/search with query params. | Throws if uninitialized or request fails. |
getSearchSuggestions | getSearchSuggestions(): Promise<any> | GET /public/sites/:siteKey/search/suggestions. | Throws if uninitialized or request fails. |
getRecommendationWidgets | getRecommendationWidgets({ pageUrl? }): Promise<any> | GET /public/sites/:siteKey/recommendations. | Throws if uninitialized or request fails. |
getRecommendationProducts | getRecommendationProducts(widgetId, context?): Promise<any> | POST /public/sites/:siteKey/recommendations/:widgetId/products. | Throws if uninitialized or request fails. |
Mobile Header Contract
RN SDK requests include:
x-selwise-api-keyx-selwise-client-platformx-selwise-client-version
See Mobile Auth for scope enforcement and guard behavior.
Availability Matrix
| Method Family | CDN (window.Selwise) | npm instance (@selwise/widget) | React Native (@selwise/react-native) |
|---|---|---|---|
| Lifecycle | Yes | Yes | Yes |
| Tracking and identity | Yes | Yes | Yes |
getSiteUserId | Yes | Yes | No |
| Web widget/campaign rendering | Yes | Yes | No |
| Search/recommendation data fetch | Yes | Yes | Yes |
Global window.Selwise binding | Yes | Optional (exposeGlobal) | No |