Browser SDK
The ResponseDock browser SDK (@responsedock/sdk-browser) is a lightweight, SSR-safe vanilla JavaScript SDK for launching contextual surveys in any web application. It has no runtime dependencies and ResponseDock hosts it for you as an ES module and as a classic script.
Installation
Load the hosted SDK straight from ResponseDock; no build step is needed:
<!-- ES module -->
<script type="module">
import { ResponseDock } from 'https://survey.responsedock.com/sdk/0.2/index.js';
ResponseDock.init({ projectKey: 'pk_live_xxxx' });
</script>
<!-- Classic script (tag managers, older CMSs): defines window.ResponseDock -->
<script src="https://survey.responsedock.com/sdk/0.2/responsedock.global.js"></script>/sdk/0.2/ receives every 0.2.x patch release. To pin an exact release, use /sdk/0.2.0/ instead. If your page sets a Content Security Policy, allow script-src https://survey.responsedock.com.
npm package coming soon. @responsedock/sdk-browser is not on npm yet. Until it is, load the hosted script above, also from bundled applications.
Initialisation
import { ResponseDock } from 'https://survey.responsedock.com/sdk/0.2/index.js';
ResponseDock.init({
projectKey: 'pk_live_xxxx', // required public key
apiUrl: 'https://api.responsedock.com', // optional, default shown
surveyUrl: 'https://survey.responsedock.com', // optional
environment: 'production', // 'production' | 'staging' | 'development'
allowedContextKeys: ['plan', 'trait.role'], // optional allowlist forwarded into a launch
autoTrack: { pageViews: true }, // optional automatic trigger events, off by default
debug: false, // enable console logging
callbacks: {
onReady: ({ surveyId, mode }) => console.log('ready', surveyId, mode),
onComplete: ({ surveyId }) => console.log('completed', surveyId),
onDismiss: ({ surveyId }) => console.log('dismissed', surveyId),
onError: ({ message }) => console.error(message),
},
});Identify a User
Call identify() before tracking events when you know the current user:
ResponseDock.identify({
externalUserId: 'user_123',
traits: { // optional arbitrary key-value
email: 'alice@example.com',
name: 'Alice',
plan: 'pro',
role: 'admin',
},
});Track Events
// Fire a named event with optional payload
ResponseDock.track('checkout_completed', {
orderId: 'ord_789',
total: 59.99,
plan: 'pro',
});The server evaluates the active trigger rules for the event. If a survey matches, the SDK opens it in a modal automatically; a survey a trigger opens never replaces one that is already open. Every event carries pageUrl, pagePath, pageTitle and referrer for trigger conditions.
Automatic Events
Automatic events are off by default. Turn on the ones you need with autoTrack and create trigger rules for their event names in the dashboard:
ResponseDock.init({
projectKey: 'pk_live_xxxx',
autoTrack: { pageViews: true, exitIntent: true, scrollDepth: [50, 90], delaySeconds: [30] },
});
ResponseDock.identify({ externalUserId: user.id }); // same tick as init(): the first page view includes it| Option | Event | Properties | Fires |
|---|---|---|---|
pageViews | $pageview | page properties | On load and on every SPA navigation (pushState, replaceState to a new URL, back/forward, #/ hash routes) |
scrollDepth | $scroll_depth | depth (percent) | Once per page for each threshold reached |
delaySeconds | $time_on_page | seconds | Once per page after each delay |
exitIntent | $exit_intent | none | Once per page when a mouse leaves through the top of the window (desktop only) |
Every automatic event goes through track(), so it carries the page properties and the server applies sampling, cooldowns and frequency caps as for any other event. Scroll thresholds, delay timers and exit intent restart on every SPA navigation. Call init() again without autoTrack to stop.
Embed a Survey Inline
<div id="survey-root" style="width:100%;min-height:400px"></div>
const embed = ResponseDock.embed('workspace-slug/post-event', '#survey-root');
// Clean up when your SPA removes this route.
embed?.destroy();The SDK follows signed resize messages from the renderer, so the iframe grows with its content and the host page owns vertical scrolling. A missing container reports through onError and returns null.
The embedded renderer supports file, photo, document, phone, country, date, integer, and decimal answers, author-configured confidentiality, and conditional question logic. These capabilities are configured in the survey builder and do not require host-side SDK code.
Keep using the ResponseDock survey URL for the iframe itself. If the survey has an external share URL, public share actions can point to your host page while the SDK renders the survey there.
Show a Modal Survey
ResponseDock.show('nps-survey');Floating Launcher
Add a floating feedback button that opens a survey in the modal:
const launcher = ResponseDock.launcher({
surveyId: 'workspace/feedback',
label: 'Feedback', // default "Feedback"
position: 'bottom-right', // or 'bottom-left'
theme: { primaryColor: '#0f766e', primaryTextColor: '#ffffff' },
});
launcher.open(); // open the survey, as a click would
launcher.destroy(); // remove the button (and close its modal)The launcher is a real <button> with aria-haspopup="dialog" and aria-expanded; pass ariaLabel when its accessible name should differ from the label. It opens the standard modal (focus trap, Escape and backdrop close), and focus goes back to the button when the modal closes. Button colours fall back to the init() theme, and it accepts every show() option, such as onComplete and autoCloseAfterMs. During server rendering it returns a handle with button: null.
Anonymous Visitors
The SDK creates a random anonymousId for each browser, keeps it in localStorage (or in memory when storage is blocked) and sends it with every event and survey launch. When no user is identified, sampling, holdouts, cooldowns and display caps apply to each anonymous visitor individually. Calling identify() makes the externalUserId the subject of those rules instead.
Frequency and Displays
Frequency rules are enforced on the server, not in the browser. When a trigger returns a survey, the server records a display for that user or visitor in the same request.
- Response-based rules (once per user, once ever, max responses per user) count completed responses only.
- Display-based rules (cooldown, once per context, once every X days, max displays per user) count the displays the server records.
- Anonymous visitors are capped by displays, because their responses can't be linked to the anonymous id.
See the trigger engine guide for every rule.
Reset
// Clear identified user, traits, theme, last event context and the anonymous id ResponseDock.reset();
Call reset() on logout. It deletes the stored anonymous id, so the next person on the device starts with a new one and isn't tied to the previous visitor's displays or sampling cohort.
Runtime theme changes
ResponseDock.updateTheme({
primaryColor: '#0F766E',
backgroundColor: '#FFFFFF',
surfaceColor: '#FFFFFF',
borderColor: '#D6D3D1',
});Theme updates are delivered only to ResponseDock frames and validated by the renderer. See the complete theme token reference.
Events (Callbacks)
| Callback | When it fires |
|---|---|
onReady | Survey loaded and rendered |
onComplete | Response submitted successfully |
onDismiss | Modal closed without completing |
onError | A request or survey load error occurred |
TypeScript
The SDK is written in TypeScript and its package ships full type declarations, so no separate @types/ package is needed. Type declarations become installable with the npm package (npm package coming soon).
Security
- The project key is public and may be used in browser code.
- Never expose a secret API key; privileged embed URLs must be created by your backend.
- Only explicitly allowed context keys are forwarded into a survey launch.
- Use the returned embed handle to remove its iframe and message listener during SPA cleanup.