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:
- 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 empty
- is anything
- matches the pattern
If either the JavaScript variable’s key or value is irrelevant, choose the is anything option.
- 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!
Our conversion experts will design 1 free campaign for you to get maximum results – absolutely FREE! Click here to get started →