{"id":150400,"date":"2023-07-10T11:43:56","date_gmt":"2023-07-10T15:43:56","guid":{"rendered":"https:\/\/optinmonster.com\/?post_type=optinmonster_docs&#038;p=150400"},"modified":"2025-08-26T09:21:53","modified_gmt":"2025-08-26T13:21:53","slug":"how-to-use-optinmonsters-javascript-variables-rule","status":"publish","type":"optinmonster_docs","link":"https:\/\/optinmonster.com\/docs\/how-to-use-optinmonsters-javascript-variables-rule\/","title":{"rendered":"How to Use OptinMonster&#8217;s JavaScript Variables Rule"},"content":{"rendered":"\n<p>OptinMonster makes it easy to hide or show campaigns based on a JavaScript variable in the page the campaign is loading on.<\/p>\n\n\n\n<p>In this article, you&#8217;ll learn how to use OptinMonster&#8217;s Display Rules to hide or show a campaign based on a JavaScript variable.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column alert alert-info is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong><em>Before you start<\/em><\/strong>, this feature is available with a <a href=\"https:\/\/optinmonster.com\/pricing\" target=\"_blank\" rel=\"noopener\" title=\"Pro and higher subscription\">Pro and higher subscription<\/a>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p><em><strong>The JavaScript variable needs to be declared on a global scope<\/strong><\/em> (ie. <code>window.customJSVariable<\/code>), or it will not be detected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure JavaScript Variables Rule<\/h2>\n\n\n\n<p>When you&nbsp;<a href=\"https:\/\/optinmonster.com\/docs\/creating-your-first-optin\/\" target=\"_blank\" rel=\"noopener\" title=\"\">create a campaign<\/a> in OptinMonster you can configure it to be shown or hidden based on a JavaScript variable in the page.<\/p>\n\n\n\n<p>To configure the JavaScript variables rule, follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to the <strong>Display Rules<\/strong> view in the campaign builder.<br><img decoding=\"async\" width=\"800\" height=\"560\" class=\"alignnone size-full wp-image-180093\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view.jpg\" alt=\"Navigate to the Display Rules in the OptinMonster campaign builder.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view-300x210.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/06\/display-rules-view-768x538.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/li>\n\n\n\n<li>Next, when adding or editing a rule select <strong>Who (Personalization)<\/strong> from the left-hand list and select the <strong>JavaScript Variable Targeting<\/strong> option.<br><img decoding=\"async\" width=\"800\" height=\"543\" class=\"alignnone size-full wp-image-180358\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-rule.jpg\" alt=\"Select the JavaScript Variable Targeting rule.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-rule.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-rule-300x204.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-rule-768x521.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/li>\n\n\n\n<li>Configure the rule as desired using the operator dropdowns, and <strong>key<\/strong> and&nbsp;<strong>value<\/strong> fields.<br><img decoding=\"async\" width=\"800\" height=\"157\" class=\"alignnone size-full wp-image-180359\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variable-options.jpg\" alt=\"Configure the JavaScript Variable Targeting rule.\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variable-options.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variable-options-300x59.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variable-options-768x151.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><br>The following options are available for JavaScript Variable Targeting:\n<ul class=\"wp-block-list\">\n<li>exactly matches<\/li>\n\n\n\n<li>does not exactly match<\/li>\n\n\n\n<li>contains<\/li>\n\n\n\n<li>does not contain<\/li>\n\n\n\n<li>starts with<\/li>\n\n\n\n<li>does not start with<\/li>\n\n\n\n<li>ends with<\/li>\n\n\n\n<li>does not end with<\/li>\n\n\n\n<li>is anything (if the key or value is irrelevant, choose this option)<\/li>\n\n\n\n<li>matches the pattern<\/li>\n\n\n\n<li>does not match the pattern<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>When you&#8217;re finished, <strong>Save<\/strong> the campaign.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>The following are some examples to help you get started:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Simple Variables<\/h5>\n\n\n\n<p>If I entered <code>myJavaScriptVariable<\/code> as my key, and <code>testValue<\/code> as my value, the rule will only pass when it is able to find a global JavaScript variable named <code>myJavaScriptVariable<\/code> that has a value of <code>testValue<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"800\" height=\"148\" src=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-example.jpg\" alt=\"JavaScript Variables rule example.\" class=\"wp-image-180360\" srcset=\"https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-example.jpg 800w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-example-300x56.jpg 300w, https:\/\/optinmonster.com\/wp-content\/uploads\/2023\/04\/js-variables-example-768x142.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Complex Variables<\/h5>\n\n\n\n<p>Users can chain together more complex variables, such as an object.<\/p>\n\n\n\n<p>For instance, if my key was <code>globalObj.test1.test2.test3.testVar<\/code>, the rule will look for <code>window.globalObj<\/code>, then loop through each section of the JavaScript object to find <code>testVar<\/code>.<\/p>\n\n\n\n<p>If the string is improperly formed, has a syntax error, or the variable path is incorrect, the rule will fail.<\/p>\n\n\n\n\n\n<p><style>code {background:#eee;padding:3px 5px;}<\/style><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;ll learn how to use OptinMonster&#8217;s Display Rules to hide or show a campaign based on a JavaScript variable. The JavaScript variable needs to be declared on a global scope&nbsp;&hellip;<\/p>\n","protected":false},"author":4657,"comment_status":"open","ping_status":"closed","template":"","documentation\/categories":[28201],"documentation\/tags":[31399,31498,31415,31459,31411],"class_list":["post-150400","optinmonster_docs","type-optinmonster_docs","status-publish","hentry","categories-display-rules"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation"}],"about":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/types\/optinmonster_docs"}],"author":[{"embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/users\/4657"}],"replies":[{"embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/comments?post=150400"}],"version-history":[{"count":22,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150400\/revisions"}],"predecessor-version":[{"id":199213,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/150400\/revisions\/199213"}],"wp:attachment":[{"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/media?parent=150400"}],"wp:term":[{"taxonomy":"categories","embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/categories?post=150400"},{"taxonomy":"internal_tags","embeddable":true,"href":"https:\/\/optinmonster.com\/wp-json\/wp\/v2\/documentation\/tags?post=150400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}