Smartlook Client for Frontend Integration
The `smartlook-client` is the official JavaScript client library designed for integrating Smartlook's user session recording and analytics into web applications. Currently at stable version 10.0.0, this package is under active development with frequent major and minor releases, typically introducing new features or improvements every few months. Key differentiators include robust support for advanced network recording, enabling the capture of request/response bodies and headers, and a flexible interceptor system to precisely control and obscure sensitive data across various capture points like URLs, network calls, errors, focus events, and clicks. It provides granular control over data capture regions, cookie usage, and integrates seamlessly with Smartlook's Relay Proxy for self-hosted data routing. The client also supports modern web features such as 1st party iframes and shadow DOM, ensuring comprehensive recording coverage across complex web interfaces.
Common errors
-
Uncaught TypeError: Smartlook.init is not a function
cause Attempting to call `init` before the Smartlook client is fully loaded or imported, or using an incorrect import method in a mixed CommonJS/ESM project.fixEnsure `import Smartlook from 'smartlook-client'` is at the top of your module, or verify that the script loading order correctly places Smartlook initialization after the library is available. For CJS environments with ESM interop, `const Smartlook = require('smartlook-client').default;` might be necessary, though `import` is preferred for new code. -
Smartlook: Missing or invalid project key provided to init().
cause `Smartlook.init()` was called without a valid project key string as its first argument.fixProvide your project's Smartlook key: `Smartlook.init('YOUR_SMARTLOOK_PROJECT_KEY', { ...params })`. -
Smartlook recording not visible for content loaded within an iframe.
cause By default, Smartlook attempts to connect with a parent window when loaded in an iframe, which can lead to delays or missed recordings if the parent doesn't also record the same project or if communication fails.fixWhen loading your application in an iframe, initialize Smartlook with the `standalone: true` option to prevent connection attempts with the parent: `Smartlook.init(key, { standalone: true })`.
Warnings
- breaking Version 7.0.0 introduced significant breaking changes by adding support for a new Record API and completely removing the old Consent API. Integrations relying on the previous Consent API must be updated.
- breaking Version 8.0.0 removed support for the legacy web SDK. This version also added official support for the Relay proxy.
- breaking Version 9.0.0 added full ESM build support. While CommonJS `require` might still function, modern applications should transition to `import` syntax for optimal compatibility and future-proofing.
- breaking In version 10.0.0, the `relayProxyUrl` parameter now strictly respects the protocol specified in the URL. If your relay proxy URL previously omitted a protocol or used an incorrect one, it might now fail.
- gotcha Setting `cookies: false` in the `init` parameters disables storing recording metadata in cookies, relying only on local storage. This will block the ability to connect visitors between different domains and subdomains.
- gotcha The `region` parameter determines where data will be captured and stored. It should only be changed if explicitly instructed by your Smartlook sales manager to ensure data residency compliance.
Install
-
npm install smartlook-client -
yarn add smartlook-client -
pnpm add smartlook-client
Imports
- Smartlook
const Smartlook = require('smartlook-client')import Smartlook from 'smartlook-client'
- Interceptors
import type { Interceptors } from 'smartlook-client' - Smartlook.init
SmartlookClient.init(key, params)
Smartlook.init(key, params)
Quickstart
import Smartlook from 'smartlook-client';
// For a real application, retrieve your Smartlook key from environment variables
const SMARTLOOK_KEY = process.env.SMARTLOOK_KEY ?? 'YOUR_SMARTLOOK_PROJECT_KEY';
if (SMARTLOOK_KEY === 'YOUR_SMARTLOOK_PROJECT_KEY') {
console.warn("Please replace 'YOUR_SMARTLOOK_PROJECT_KEY' with your actual Smartlook project key for proper functionality.");
}
Smartlook.init(SMARTLOOK_KEY, {
region: 'eu', // Specify your data residency region ('eu' or 'us')
cookies: true, // true by default; set to false to disable cookie storage for metadata
advancedNetwork: {
websockets: true, // Enable recording of WebSocket traffic
allowedUrls: [/^https?:\/\/api\.example\.com\/.*/], // Regex patterns for URLs to record bodies/headers
allowedHeaders: ['x-custom-request-id'] // Non-standard headers to record
},
interceptors: {
click: (data, context) => {
// Prevent recording clicks on elements marked with data-no-smartlook-click
if (context.target instanceof HTMLElement && context.target.dataset.noSmartlookClick) {
return false;
}
// Modify click data, e.g., obscure text content
return { ...data, text: 'CLICK_OBSCURED' };
},
network: (request, response) => {
// Obscure sensitive response bodies from specific endpoints
if (response && response.url.includes('/sensitive-data')) {
return { ...response, body: '[OBSCURED_SENSITIVE_DATA]' };
}
return response;
},
error: (data, context) => {
// Filter out known or non-critical errors
if (data.message?.includes('Expected informational error')) {
return; // Do not record this error
}
// Annotate recorded errors
return { ...data, message: `[Smartlook Intercepted] ${data.message}` };
}
},
relayProxyUrl: 'https://my-proxy-domain.com/' // Optional: URL for self-hosted relay proxy
});
console.log('Smartlook client initialized and configured.');
// Example: Identifying a visitor with custom properties
Smartlook.identify('user-unique-id-789', {
name: 'Jane Doe',
email: 'jane.doe@example.com',
plan: 'premium'
});
// Example: Tracking a custom event
Smartlook.track('SignUpCompleted', { method: 'email', campaign: 'spring2024' });