HTML Snippet
Add Statsig to web pages with an HTML script tag
Set up the SDK
Install the SDK
Install the Statsig SDK by adding a script tag to the head of your HTML file:
js<script src="https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js?apikey=YOUR_CLIENT_API_KEY" crossorigin="anonymous" > </script>Replace
YOUR_CLIENT_API_KEYwith your actual client API key from Project Settings > API Keys.Initialize the SDK
The HTML snippet wraps an instance of the Statsig JavaScript SDK. Providing your client API key in the URL auto-initializes the SDK, as shown in the installation step.
Move to manual initialization if you need any of the following:
- custom user properties
- custom initialization options
- check gates, configs, experiments, or log events
To manually initialize an instance of the SDK, remove the key parameter from the script tag and do the following:
jsconst { StatsigClient, runStatsigAutoCapture, runStatsigSessionReplay } = window.Statsig; const client = new StatsigClient( '<CLIENT-SDK-KEY>', { userID: 'a-user' } ); runStatsigSessionReplay(client); runStatsigAutoCapture(client); await client.initializeAsync(); // check gates, configs, experiments, or log eventsYou can also access the
StatsigClientinstance you create throughwindow.Statsig, so you can reference it globally.Your
StatsigClientinstance provides access to all methods in the JavaScript SDK. Go to that documentation for initialization details and core methods.
Can I customize the initialization logic
Yes. Remove the client API key from the URL. The HTML snippet is the JavaScript SDK. Providing an API key in the URL auto-initializes an instance. To skip auto-initialization, omit the key and create your own instance. Go to the JavaScript SDK Getting Started Guide for details.
Creating an instance using the HTML snippet differs from installing through npm:
const { StatsigClient, runStatsigAutoCapture, runStatsigSessionReplay } = window.Statsig;
const client = new StatsigClient(
'<CLIENT-SDK-KEY>',
{ userID: 'a-user' }
);
runStatsigSessionReplay(client);
runStatsigAutoCapture(client);
await client.initializeAsync();
// check gates, configs, experiments, or log events
Was this helpful?