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

Here are some things to know before you begin:

  • JavaScript Variable targeting is available with a Pro or higher 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 view in the campaign builder.
    Display Rules view in the OptinMonster campaign builder
  2. When adding a new rule, select JavaScript Variable Targeting from the Targeting menu.
    Add the JavaScript Variable Targeting Display Rule to your OptinMonster campaign.
  3. Configure the rule as desired using the operator dropdowns, and key and value fields.
    JavaScript Variable Targeting configuration options.
    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, 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 →

Examples

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.