1. Help Center
  2. Target Visitors

Target experiment based on a custom Javascript condition that evaluates true at a later stage


Experiments are evaluated as soon as the Convert Experiences tracking script loads. Sometimes it's needed to fire an experiment based on values of variables that are defined later on the page (below the main tracking code inclusion point). This DMP (Data Management Platform) feature allows it to target, for example, lead score/stage from your Marketing Automation tool (like Marketo, Pardot, Elequou, Hubspot, or Salesforce) without much effort.

In that case, we can use a custom javascript condition targeting rule and an API call to re-check the experiment later.

Inside the JS condition, the following JS API function can be used: 

convert_recheck_experiment()

When that code executes, a re-check of the experiment's conditions is scheduled for 50 ms later, for approximately the next two minutes, or until the experiment JS condition check is final (whichever comes first).  Here is an example:

(function() {
if(typeof(window.my_variable)=="undefined") {
convert_recheck_experiment(); return false;}
else return (window.my_variable == "test_value");
})()

In the above example, we check if variable window.my_variable is defined at the run time; if not we call the API function to recheck in 50ms; if it's defined, we check its value against test_value and return true or false, depending on whether they match or not. The window.my_variable can be defined later on the page, after the main Convert tracking script, and the experiment will be checked and fired when that variable is defined. 

Important

It is important to make sure that you take into account the possibility that a variable may not be defined at the time the code is first run; if that happens and an error is thrown, the rest of the code will not run.

 

An example is shown in the code above where we check to see if the variable is undefined, so that an error will not be returned and convert_recheck_experiment() will run.

The above type of targeting can be combined with the URL targeting type. If we wanted to fire the experiment like above but only for a page that has path /test_page.html:

  •  we would set the above condition into the Locations JS condition
  • and we would add a URL targeting rule like below: URL does not contain /test_page.html. In doing so, we'd include the experiment pages where the JS condition is true but exclude all the pages that do not contain /test_page.html  into the URL (therefore just the ones that contain /test_page.html  will be included)mceclip1-Apr-02-2024-06-54-08-5343-AM

There are multiple use cases of this functionality limited only by the imagination of whoever uses it. It's worth mentioning one other common use case: fire the experiment when an element was added to the page (maybe via Ajax); the JS condition would look something like the below:

(function() {
if (document.querySelectorAll("element_selector").length === 0) {
convert_recheck_experiment();
return false;
} else {
return true;
}
})();

keywords: "DMP", "Data Management Platform"