Skip to main content
Users
CloudPortalLogin
  • Powered byPowered by
Introduction to Sitecore Personalize
Managing accounts and system settings
Identifying guests
Introduction to experiments
Introduction to experiences
Decisioning
View dashboards
Developer Center
Connecting to an external system
Using client-side JavaScript
Using server-side JavaScript
AI in Personalize
Glossary
  • Sitecore Personalize
  • Developer Center
  • Conditions
  • Best practices for creating custom conditions

Best practices for creating custom conditions

We recommend that you follow these best practices when creating custom conditions and using them in multiple conditions:

  • Use an Immediately Invoked Function Expression (IIFE) - encapsulate condition logic within an IIFE. Using an IIFE to create a local scope for your condition logic ensures that variables and functions are not inadvertently exposed globally, reducing the risk of conflicts across conditions. To enforce best practices, we validate that all condition logic is written within an IIFE. If the logic is not encapsulated in an IIFE, you will not be able to save it. For example:

    RequestResponse
    (function() {
        const conditionValue = getConditionValue();
        return conditionValue > 10;
    })();

    This ensures consistency and modularity across all condition implementations.

    The advantages of using IIFEs include preventing polluting the global namespace, ensuring immediate execution of the condition logic in a safe, isolated scope, and making the code more modular and easier to debug.

  • Use local variables whenever possible - declare variables locally within functions rather than globally to prevent variable clashes across conditions. This reduces the risk of unintentional overwriting or interference between conditions. For example:

    RequestResponse
    (function() {
        let conditionA_value = getValue();
        // Logic using conditionA_value
        return conditionA_value > 0;
    })();
  • Avoid using global variables within conditions - avoid relying on global variables, especially when combining multiple conditions. Use function parameters or locally scoped variables to maintain isolation and clarity.

  • Prefix variable names - add unique prefixes to variable and function names related to a specific condition. For example, use conditionA_ or conditionB_ to differentiate similar variables across conditions. This makes the code more readable and avoids naming conflicts.

  • Avoid modifying global objects - do not modify or extend global objects like window, document, or Object.prototype. Changes to these objects can affect other JavaScript running in the same environment and reduce compatibility and stability across scripts.

  • Namespace shared functions - encapsulate reusable functions within a namespace object, such as myConditionUtils. This prevents naming collisions and keeps shared logic organized and accessible. For example:

    RequestResponse
    const myConditionUtils = {
        isValid: function(value) {
            return value !== null && value !== undefined;
        },
        parseValue: function(value) {
            return parseInt(value, 10);
        }
    };    
  • Minimize dependencies - limit the use of external libraries or resources in conditions. Dependencies can introduce conflicts, especially if they are loaded multiple times or in incompatible versions. Aim to keep the code self-contained whenever possible.

  • Explicitly define return statements - ensure each condition's JavaScript code returns an object with distinct keys and values. Avoid using identical keys unless explicitly intended to be shared across conditions.

  • Ensure consistency in return types - when combining multiple conditions, make sure all conditions consistently return a true or false result. Returning mixed data types can lead to unpredictable behavior and make debugging more difficult. Similarly, do not combine multiple conditions that return values, as this can lead to unpredictable results.

  • Test each condition in isolation - verify the functionality of individual conditions before integrating them into the broader logic. This helps isolate issues and ensures that each condition behaves as expected.

  • Document condition logic - document the purpose and functionality of each each condition. Comments or documentation are especially helpful in projects involving multiple developers. For example:

    RequestResponse
    (function() {
        // This condition checks if the user's age is above 18.
        var userAge = getUserAge();
        return userAge > 18;
    })();
Note

The amount of customer data available in custom conditions is subject to data retention limits. If you use Personalize without Sitecore CDP, you can access up to the last 20 orders and last 40 sessions (within 90 days). CDP extends access to a maximum of 100 orders and 1,000 sessions for identified guests. For more information, see Understanding data limits and Data availability in Sitecore CDP.

Do you have some feedback for us?

If you have suggestions for improving this article,

Privacy policySitecore Trust CenterCopyright © 1999-2026 Sitecore