Build Your First Feature

Walk through creating a feature gate, targeting audiences, and rolling out your first feature with the JavaScript SDK.

A feature gate controls who sees a new feature and rolls it out gradually without deploying code. This tutorial creates a feature gate, targets mobile platforms and internal testers, then checks it live with the JavaScript SDK in your browser console.

A feature gate is a Boolean feature flag that returns true or false for each user. In Statsig, Feature Flag is the umbrella term for any configuration you change without deploying code, including feature gates and Dynamic Configs.

  1. Create a feature gate

    Navigate to the Feature Gates page and click Create. In the Create New Feature Gate dialog, enter a name and an optional description, keep User ID as the ID type, set a target application, and click Create.

    Statsig generates the gate ID from the name and shows it under the name field. Your code checks the gate by that ID. Enter mobile registration to get the gate ID mobile_registration that this tutorial uses, and confirm the Gate ID before you click Create.

    Create New Feature Gate dialog with the gate ID mobile_registration generated from the name

  2. Target mobile platforms

    New gates return false until you add a rule. Click Add New Rule and give the rule a name. Set Criteria to Operating System, set Operator to Any of (Case Insensitive), and select Android and iOS. Set Pass to 100%, then save your changes. If your project requires reviews, click Submit for Review.

    Mobile OS rule targeting Android and iOS with 100% pass

  3. Add an internal testing rule

    Add a second rule for your team. Click Add New Rule, set Criteria to Email and Operator to Contains any of, and enter your company domain. Set Pass to 100% and save your changes. Employees can now use the feature on any device.

    Gate with mobile and email rules

  4. Generate a client API key

    Go to Project Settings → API Keys and copy the Client API key. Keep server secret keys on backends only, and use console API keys for programmatic configuration work.

  5. Load the JavaScript SDK

    Statsig supports many platforms. Refer to Client SDKs for alternatives. This walkthrough uses the browser SDK so you can experiment directly in DevTools.

    Paste the snippet below into the browser console on any site to load the SDK from jsDelivr:

    js
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/@statsig/js-client@3/build/statsig-js-client+session-replay+web-analytics.min.js';
    document.head.appendChild(script);
    

    Injecting the SDK via DevTools

  6. Initialize and check the gate

    Replace YOUR_SDK_KEY with the client key from Step 4 and run:

    js
    const client = new window.Statsig.StatsigClient('YOUR_SDK_KEY', {});
    await client.initializeAsync();
    

    Then call:

    js
    client.checkGate('mobile_registration');
    

    You should see false because the current session isn’t mobile and doesn’t use the employee email domain.

  7. Simulate a mobile environment

    Enable the mobile device toolbar in Chrome DevTools and then re-evaluate the user to pick up the new environment and re-check the gate:

    js
    await client.updateUserAsync({});
    client.checkGate('mobile_registration');
    

    The gate should now return true for the mobile profile.

    Gate returning true for mobile rule

  8. Test the employee backdoor

    Switch DevTools back to the desktop view and update the user with a company email:

    js
    await client.updateUserAsync({ email: 'teammate@statsig.com' });
    client.checkGate('mobile_registration');
    

    The gate passes again because of the email rule.

    Gate returning true via email rule

  9. Flush exposures and inspect diagnostics

    js
    client.flush();
    

    Open the gate’s Diagnostics tab. The Exposure Stream table lists each check, including the failing desktop check, the mobile pass, and the employee pass.

    Exposure Stream table with a failing desktop check, a mobile pass, and an employee pass

Use the gate in production

Wrap feature logic in a gate check so only targeted users see the experience:

js
if (client.checkGate('mobile_registration')) {
  show(mobileRegistrationPage);
} else {
  show(oldRegistrationPage);
}

Your feature gate is ready for production.

Was this helpful?

For AI agents: a documentation index is available at /llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

On this page