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 variables.

Before You Start

Here are some things to know before you begin:

  • JavaScript Variable targeting requires a Growth subscription.
  • JavaScript Variable targeting can be used with any campaign type.
  • You will need to declare the JavaScript variable 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 tab in the campaign builder.
    Display Rules Tab
  2. While editing the Display Rules, add a rule and from the dropdown select the JavaScript variable.
  3. Next, you’ll see two options appear to set the JavaScript variable’s key and value.
    Configure the Javascript variable rule in the Display Rules of the campaign builder.
    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
  4. If either the JavaScript variable’s key or value is irrelevant, choose the is anything option.
  5. When you’re finished, click Save.

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 →


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.

Configure the Javascript variable rule in the Display Rules of the campaign builder.

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.