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 which are defined later on 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.

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:

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 Include part of the Site Area

into the Exclude part of the Site Area we would add a URL targeting rule like below: URL does not contain /test_page.html. Doing so, we'd include into 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)

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 below: