A/B Testing on Menu item

Created by Donald Ng, Modified on Wed, 6 Nov at 3:59 AM by Donald Ng

When running A/B tests on your website, it's important to ensure that the experiment is triggered based on meaningful user interactions. For example, if you're testing changes to a hamburger menu, you want to make sure that only users who engage with the menu are included in the test. Otherwise, you risk polluting your test results with data from visitors who never interacted with the menu.


In this guide, we'll show you how to trigger an A/B test using Mida.so only when users click or tap on a hamburger menu. This ensures that your test data reflects actual user behavior, giving you more accurate and actionable insights. 


By using JavaScript to listen for click and touchend events, the test will run only for users who engage with the menu, leaving out passive visitors who do not interact.


Steps to Set Up the Test Campaign

  1. Create a Test Campaign in Mida
  2. Under the Configuration tab, find the setting "When to load test?".
  3. Select "Do Not Start (Execute by Javascript)" to make sure the test does not automatically start and waits for a trigger via JavaScript.


Trigger the Test on Menu Interaction

To trigger the experiment when the user clicks on a menu item (e.g., hamburger menu), use the following JavaScript code:


// 1. Change .hamburger-menu to your actual menu button
waitForElement('.hamburger-menu', (menu) => {
    // Add click event listener for desktop
    menu.addEventListener('click', function() {
        mida.execute(1234);  // 2. Replace '1234' with your actual test ID
    });

    // Add touch event listener for mobile
    menu.addEventListener('touchend', function() {
        mida.execute(1234);  // 2. Replace '1234' with your actual test ID
    });
});

// Do not change anything below
function waitForElement(selector, callback, maxRetries = 50, interval = 100) {
    let retries = 0;
    
    const checkExist = setInterval(() => {
        const element = document.querySelector(selector);
        
        if (element) {
            clearInterval(checkExist);
            callback(element);
        } else if (retries >= maxRetries) {
            clearInterval(checkExist);
            console.warn(`Element ${selector} not found after ${maxRetries * interval / 1000} seconds.`);
        }
        
        retries++;
    }, interval);
}


Replace .hamburger-menu with the appropriate selector for your menu item if it differs. 


This will execute the experiment when the code is triggered, where 1234 should be replaced with the test_id for your specific experiment.


This code listens for a click event on the hamburger menu and then executes the test once the menu is clicked.


Apply the Code Globally

To apply the experiment trigger across all pages on your site (as long as the Mida script is loaded), follow these steps:

  1. Go to the Experimentation section in your Mida dashboard.
  2. Find "Global Custom Code".
  3. Click "Edit JS".
  4. Paste the JavaScript code snippet you just created into the editor.
  5. Click Save.


This will ensure that the test is triggered across your website whenever the hamburger menu is clicked, as long as the Mida script is loaded.


Conclusion

This method allows for more granular control over when and where your experiments run, ensuring that the test is executed based on user interactions.






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article