Activating an Experiment when someone clicks or hovers over a menu item

Issue

There may be times that bucketing a visitor in an experiment by landing on a page may not be sufficient. This might be, because we may need to change a menu that is only seen when they hover or click on an element like a menu item. We might only want to sample the visitors that see the menu and perform this action, so the experiment is free of the noise of people that actually did not see the change.

Solution

To solve this we need to activate the experiment when they perform this action. To do this, we need to add some code in the Project Settings that will trigger the code when the visitor hovers or clicks on an element. 

1) Add the following code to the Project > Project Settings > Project Global Javascript area. This code will trigger the experiment anywhere where the element is hovered over on any page. Here targeted by the menu_item_selector, which you should replace by your own element selector.

convert.$(function(){
convert.$( "menu_item_selector" ).hover(function() {
    window.runExperiment = 1;
    window._conv_q = window._conv_q || [];
    window._conv_q.push(["executeExperiment","123456789"])
  });
});

Following is an example of activating an experiment by clicking on an element on a specific case. The previous example targeted any page on the site. The following example monitors for the clicks on the menu_item_selector and the https://www.website.com/index.html page. 

if (location.href == 'https://www.website.com/index.html') {
convert.$(function(){
convert.$( "menu_item_selector" ).hover(function() {
window.runExperiment = 1;
window._conv_q = window._conv_q || [];
window._conv_q.push(["executeExperiment","123456789"])
});
});
}

Make sure that you replace the URL, menu_item_selector, and the experiment id. 

2) Add the following JS Condition to the Site Area of your experiment:

(window.runExperiment == 1)

With this code, the experiment should only bucket the right visitors and avoid adding any noise in the visitor sample.

Need assistance? Start a conversation with our Support Team.
CONVERT MORE

Reliably Convert More Site Visitors, Even If Everything Else is Changing in Your Business.

START 15-DAY FREE TRIAL
✓ No Credit Card  ✓ Easy Set-Up  ✓ Access All Features

Comments