Triggering Experiments with Javascript Conditions

Created by LiRou C, Modified on Tue, 5 Mar at 5:57 AM by LiRou C

Mida's tracking script typically initiates tests as soon as the page loads. However, certain scenarios require tests to be activated based on information that becomes available later on the page. For such situations, Mida's 'Do Not Start (Execute by Javascript)' feature can help. 


Take, for example, a scenario where you are testing various arrangements of your dropdown menu items. Rather than initiating the test as soon as the page loads, you would prefer to specifically target those visitors who interact with the menu. In this situation, the test is not activated until a visitor clicks on the menu. With this feature, you can ensure a more accurate and relevant test data collection.



Example: Trigger an experiment when someone clicks or hovers over a menu


On the CONFIGURATION, ‘When to load test’, select ‘Do Not Start (Execute by Javascript)





Then, go to Google Tag Manager, add the specific JS conditions as a new Custom HTML tag in Google Tag Manager and run it at pageload. Note, you need to change the test_id value to your actual test id from the script.


<script>
(function(){
  // Find the menu element
  var hamburg = document.querySelector('body > header > div.header-mobile > div > div> i > svg')

  // If menu element is found
  if (hamburg && hamburg.parentNode.parentNode) {
    var hamburgParentContainer = hamburg.parentNode.parentNode

    // When menu element was clicked
    hamburgParentContainer.addEventListener('touchstart',function(e){

      // You can find the ID by going to Experiment > Configuration > When to load test? > Do Not Start (Execute by Javascript)

      var test_id = 1234; // Change this to your test id

      mida.execute(test_id); // Execute Mida experiment

    }, false);
  }
})();
</script>

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