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, this feature is available with a Pro and 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:
- Navigate to the Display Rules view in the campaign builder.
- Next, when adding or editing a rule select Who (Personalization) from the left-hand list and select the JavaScript Variable Targeting option.
- Configure the rule as desired using the operator dropdowns, and key and value fields.
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 anything (if the key or value is irrelevant, choose this option)
- matches the pattern
- does not match the pattern
- When you’re finished, Save the campaign.
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
.
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 →