How to Use OptinMonster’s JavaScript Variables Rule

OptinMonster makes it easy to hide or show campaigns based on a JavaScript variable in the page the campaign is loading on.

In this article, you’ll learn how to use OptinMonster’s Display Rules to hide or show a campaign based on a JavaScript variable.

Before you start, JavaScript Variables Targeting is available with a Pro or higher subscription.

The JavaScript variable needs to be declared on a global scope (ie. window.customJSVariable), or it will not be detected.

Configure JavaScript Variables Rule

When you create a campaign in OptinMonster you can configure it to be shown or hidden based on a JavaScript variable in the page.

To configure the JavaScript variables rule, follow these steps:

  1. Navigate to the Display Rules view in the campaign builder.
    Navigate to the Display Rules in the OptinMonster campaign builder.
  2. Next, when adding or editing a rule select Who (Personalization) from the left-hand list and select the JavaScript Variable Targeting option.
    Select the JavaScript Variable Targeting rule.
  3. Configure the rule as desired using the operator dropdowns, and key and value fields.
    Configure the JavaScript Variable Targeting rule.
    The following options are available for JavaScript Variable Targeting:

    • exactly matches
    • does not exactly match
    • contains
    • does not contain
    • starts with
    • does not start with
    • ends with
    • does not end with
    • is empty
    • is anything
    • matches the pattern

    If either the JavaScript variable’s key or value is irrelevant, choose the is anything option.

  4. When you’re finished, Save the campaign.


The following are some examples to help you get started:

Simple Variables

If I entered myJavaScriptVariable as my key, and testValue as my value, the rule will only pass when it is able to find a global JavaScript variable named myJavaScriptVariable that has a value of testValue.

JavaScript Variables rule example.

Complex Variables

Users can chain together more complex variables, such as an object.

For instance, if my key was globalObj.test1.test2.test3.testVar, the rule will look for window.globalObj, then loop through each section of the JavaScript object to find testVar.

If the string is improperly formed, has a syntax error, or the variable path is incorrect, the rule will fail.

If you’re serious about jumpstarting your website business growth, then get started with OptinMonster today!

Get Started With OptinMonster Today!

BONUS: Done-For-You Campaign Setup ($297 value)
Our conversion experts will design 1 free campaign for you to get maximum results – absolutely FREE! Click here to get started →JavaScript Variables rule example.