The web application provides you with a custom JavaScript targeting option that lets you target the experiment you create for a particular set of audience with a specific JavaScript condition.

The Custom JavaScript targeting option lets you write a custom JavaScript code, which will be evaluated for all the site’s visitors at each page load.

An important thing to note here is that the code/condition you write must return a boolean value. If the condition returns true, the experiment will run for that visitor. If the condition returns false, the experiment will not run.


Importance of Custom JavaScript targeting

In the web application, targeting options let you define who and where your experiment should be targeting. However, sometimes you need to go beyond the standard targeting options to reach a more specific and complex set of audiences. 

For instance, let us assume that an e-commerce site selling all electronic items wants to target the audience who clicks explicitly on the iPod product page. This is where Custom JavaScript targeting option comes into the picture. A JavaScript condition should be written so that it returns true as the boolean value when the visitor lands on the iPod product page.

Here, the web application intelligently evaluates the custom JavaScript code whenever a page loads and targets the experiment, specifically when the condition returns true to those audiences.

Setting up Custom JavaScript targeting

To add a custom javascript targeting,

  1. Go to MARKETING AUTOMATION > A/B Testing.

  2. Choose an experiment and click Pause.

  3. Click on the paused experiment to open it.

  4. Click the Edit Experiment button.

  5. Under the TARGETING tab > click ADD CUSTOM AUDIENCE

  6. In the Source option, choose Custom Javascript

  1. Type your javascript code in the text area with the placeholder ‘Type your code here’

  2. Click SAVE

Some cases in which Custom JavaScript targeting can be used.

Case 1 - Running the experiment for a certain time period

Scenario: An e-commerce site wants to display a page containing exclusive offers to all the site’s visitors who enter the afternoon after 12 till midnight.

Solution: To achieve this, all you need to do is write a JavaScript condition given in the below screenshot.

Case 2 - Running the experiment for logged-in visitors exclusively

Scenario: An online book store wants to target the experiment specifically to the logged-in visitors to display a page containing access to all their books’ premium collections.

Solution: This can be achieved by writing a JavaScript condition with a variable that has the information if a visitor is logged in or not. Refer the screenshot below for an example code to achieve this targeting.

Case 3 - Running the experiment for visitors with certain screen sizes

Scenario: An online ticket booking site has a responsive website and wants to target the audience who enter the site with specific screen sizes, like the one on a tab.

Solution: You can achieve this targeting by a running a JavaScript condition with the screen height and width variable set to the screen size. Now, only the visitors from tabs are targeted. Refer the below screenshot for an example code.