Present hosted Pillow studies with the browser SDK
Use presentStudy(...) to prepare and open a hosted study for the current browser user. For the concepts behind this, see About presenting studies.These presentation APIs are part of the @trypillow/web package.
Launch studies are audience-targeted studies configured in the dashboard. Instead of hard-coding which study to show, call presentLaunchStudyIfAvailable() at a safe moment in your app and the backend decides which study (if any) to present.
await sdk.presentLaunchStudyIfAvailable();
If no launch study is pending for the current user, the call is a no-op.Good places to call it:
After the app finishes initializing and the main view is mounted
On route changes in a single-page app
After a user action where a modal or overlay is appropriate
The browser SDK does not auto-present launch studies. You must call presentLaunchStudyIfAvailable() yourself. The heartbeat only refreshes which study is pending; it never opens the widget on its own.
The SDK refreshes the pending launch study assignment whenever it heartbeats with the backend, on initialization and when the tab returns to the foreground (throttled to at most once per minute). presentLaunchStudyIfAvailable() uses the latest assignment and clears it after presenting.
When you configure a distribution in the dashboard, the server sends display settings (mode, size, backdrop) to the SDK. These are applied automatically. If you pass presentation options to presentLaunchStudyIfAvailable(), your values override the server defaults:
on(...) returns an unsubscribe function. Call it when you no longer need the listener, for example when a component unmounts.
const unsubscribe = sdk.on('studyFinish', ({ study }) => { console.log('Finished', study.id);});// Later, when you no longer need this listener:unsubscribe();
You can also remove a specific listener with off(...):
function handleFinish({ study }) { console.log('Finished', study.id);}sdk.on('studyFinish', handleFinish);// Later:sdk.off('studyFinish', handleFinish);