Skip to content

Only include in the test a user if it meets a condition

Osvaldo edited this page Dec 27, 2025 · 1 revision

It is possible to include in a test a user if it meets a certain criteria, by adding a function as a parameter.

// Assign a variant (Variant name, optional fraction of the audience to include in the experiment: from 0 to 1, optional segment function)
MyTest.assignVariant("MyTest", 1, () => {
    // Example segment: only include users with Portuguese as browser language
    return navigator.language === "pt";
});

Possible usage:

  • Do the experiment only if the user has a certain browser language
  • Do the experiment only if the user has a small (mobile) screen: return window.innerWidth < 768;

Clone this wiki locally