Using JavaScript targeting to qualify visitors

This article is intended for developers with JavaScript knowledge.

In this article, you learn how to add custom JavaScript to a campaign in order to limit which visitors are eligible for the campaign or determine when the campaign should be activated.

In the example below, we will add a JavaScript targeting rule so that visitors will only be eligible for this campaign when window.page_type === 'product_pages'.

  1. In the Targeting tab, drag JavaScript Code into the Campaign Rules section.
  2. An empty function will appear which you can modify and save.
  3. Once created, this function, named Qualifying, can be modified in the Campaign Scripts section of your campaign.
  4. This function runs whenever your test pages are loaded. If the function returns a truthy value or a promise which resolves, the campaign is activated and a request for content is made.

Scenario 1 - targeting based on a user's cookies

In this scenario, we want to target our campaign to only visitors with a cookie named logged_in. To do this, we can use the following code snippet, which makes use of the CDAPI's cookies module.

Scenario 2 - targeting based on the page's data layer

In this scenario, we want our campaign to activate once our data layer has been loaded (asynchronously) and only if there is a property of page_type with a value of product_pages. To do this, we can use the following code snippet, which makes use of "when" method from CDAPI's deferred module.

Scenario 3 - activating a campaign if and when an HTML element has loaded

In this scenario, we want our campaign to activate if and when <div id="logged-in-user"> has loaded. To do this, we can use the following code snippet, which makes use of CDAPI's "waitForElement" method, part of the Renderer module.