Randomly Rotate Popups on a Page

Show just one popup from a group at random—ideal for testing variations or keeping your page clean without overwhelming visitors.

Want to display only one of several popups on a page, selected at random? Whether you’re experimenting with different content or simply trying to avoid showing multiple popups at once, there’s a simple way to control this behavior. While EngageBox doesn’t offer built-in A/B testing, you can replicate this logic using a short script.

Steps

First, publish all the popups you want to include in the rotation. Ensure they’re assigned to the same page so they’re all loaded and available for selection. 

Next, add the following JavaScript to your site: 

EngageBox.onReady(() => {
    // Array of popup IDs we want to manage
    const popups = [3, 4, 5];
    
    // Randomly select one popup ID from the array
    const randomId = popups[Math.floor(Math.random() * popups.length)];

    // Iterate through all popup IDs
    popups.forEach(popupId => {

        // Skip the randomly selected popup ID
        // This popup will be allowed to display normally
        if (popupId === randomId) {
            return;
        }

        // Get the EngageBox instance for the current popup ID
        const popup = EngageBox.getInstance(popupId);

        // If the popup instance doesn't exist, skip to the next one
        if (popup === undefined) {
            return;
        }

        // Add an event listener for the 'beforeOpen' event
        // and prevent the popup from opening
        popup.on('beforeOpen', () => {
            return false;
        });
    });
});

Notes

  • All popups must be published on the same page for this method to work. If a popup is not loaded on the page, it won’t be available for EngageBox to control.
  • You can use any of the supported trigger points—On Page Load, Exit Intent, Scroll, Click, etc. It does not matter. The script will still prevent non-selected popups from opening.
  • There is no limit to the number of popups you can include in the random rotation. Simply add all the popup IDs to the popupIds array.
  • This approach does not offer built-in tracking or conversion statistics but provides a simple way to rotate content without showing everything at once.
Last updated on May 15th 2025 09:05