SDK reference

Every method on PulseClient, the config it takes, and the functions the package exports beside it.

pnpm add @thepulseapi/sdk

Session

MethodReturns
init(config)TrackResponse | nullStart a session and send the automatic signals once
getSession()SessionResponseCurrent session and inferred personas
getSessionId()stringThe current session id
resetSession()voidClear it and start fresh
onPersona(cb)voidSubscribe to persona updates

Signals and ranking

MethodReturns
track(signals)TrackResponseSend shopper signals
search(products, options?)SearchResponseRank products you pass in
rankedCatalog(options?)RankedCatalogResponseRank the catalogue already synced to PULSE
event(type, data?)EventResponseRecord a named event

search and rankedCatalog are not interchangeable. search ranks what you hand it; rankedCatalog ranks what PULSE already holds.

Exposure

Method
exposure(decisionId, items)voidRecord what was actually rendered, buffered
flushExposures()Promise<void>Send buffered exposures now

Config

apiKeyRequired

string

Publishable key, pk_live_...

organizationIdRequired

string

From the dashboard

endpointOptional

string

Override only for local development

Defaults to https://api.thepulseapi.com

autoTrackOptional

boolean

Capture device, referrer and time on init()

Defaults to true

sessionTtlMsOptional

number

Inactivity before a new session

Defaults to 1800000

anonymousIdOptional

string

Identity label only. Never reaches inference.

debugOptional

boolean

Verbose logging

Defaults to false

Beyond the client

PulseClient is most of the package and not all of it. These are exported too, for the cases where you need a signal without a client, or need to look at what the SDK cached.

Signal capture

init() calls captureAutoSignals() for you when autoTrack is on, so you only need these directly when you want a signal at a different moment, or want one the automatic set leaves out.

ExportReturns
captureAutoSignals()Signal[]Device, referrer and time in one call. What init() sends
captureDeviceSignal()Signal | nullDevice class from the user agent
captureReferrerSignal()Signal | nullWhere the visit came from
captureTimeSignal()SignalTime of day. Never null, there is always a clock
captureEnhancedReferrerSignals()Signal[]Campaign and channel detail beyond the plain referrer
captureDeviceInfoAsync(): PromiseDeviceInfoFuller device detail, including client hints
captureGeoInfo(): PromiseGeoInfo | nullCoarse location from the IP
geoToSignals(geo)Signal[]Turn a GeoInfo into signals
captureWeatherInfo(lat, lon, apiKey?): PromiseWeatherInfo | nullConditions at a point
weatherToSignals(weather)Signal[]Turn a WeatherInfo into signals
captureCulturalCalendar(countryCode)Signal[]Seasonal and cultural events active today
parseUserAgent(ua)DeviceInfoParse a user agent string you already have

captureWeatherInfo needs your own key

It calls OpenWeatherMap and returns null without an apiKey, so nothing breaks if you leave it out. The free tier is 1,000 calls a day, which is why the result is cached per session rather than fetched per page.

captureAutoSignals() is exported for one specific failure. init() sends the automatic signals exactly once and swallows its own error, so a session whose init() failed never re-sends them and stays thin for its whole life. These signals cost no network of their own, so calling this again on a later page load is cheaper than the alternative.

Session context

Device, location and weather are properties of the session rather than of the page: they cannot change between one page and the next. The SDK captures them once, keys the cache by session id so it cannot outlive the visitor it describes, and reuses it. You rarely need to touch this.

ExportReturns
readSessionContext(sessionId)SessionContext | nullThe cache, or null if it belongs to another session
writeSessionContext(sessionId, patch)voidMerge fields into it
isWeatherFresh(ctx, now?)booleanWhether the weather reading is still inside its TTL
clearSessionContext()voidDrop the cache
WEATHER_TTL_MSnumber10 minutes

Weather is the only field here with a TTL. Everything else is immutable for the session, but a 30-minute session can outlast a rain shower, and a weather:rain signal driving a recommendation after the sun came out is the engine being confidently wrong.

Cập nhật lần cuối Xem lịch sử thay đổi

Trong trang này