How to Exclude Campaigns from a Page using the JavaScript Events API

Want to bypass the OptinMonster display rules and use your own code to hide your campaigns in certain situations? It’s easy with OptinMonster’s JavaScript Events API. Check out some examples below to get started.

NOTE: If you are on the WordPress platform, please see our Page-Level Targeting Guide referencing WordPress. However, if you want to exclude a campaign based on a referral link, the best option is to use the JavaScript below.

Important: Remember to place your code in <script> tags.

&lt;script type="text/javascript"&gt;
 // Your JavaScript here..
&lt;/script&gt;

You can then take the code (script tags and all) and place it in either the header or footer template files for your site. The following are some examples of removing your campaign to help get you started.


Remove All Campaigns From Specific URL

jQuery(document).ready(function($){
   $(document).on('OptinMonsterBeforeShow', function(event, data, object){

      // If url matches the following address, remove the campaign.
      if(window.location.href == "http://www.example.com/page-with-no-campaign"){
         $('#om-' + data.optin).remove();
      }

   });
});

Remove Specific Campaign From Specific URL

Note: be sure to replace slkdfa0922kf in the example below with your campaign’s unique slug.

jQuery(document).ready(function($){
   $(document).on('OptinMonsterBeforeShow', function(event, data, object){

      // If url matches the following address, remove the campaign.
      if(window.location.href == "http://www.example.com/page-with-no-campaign"){
         $('#om-slkdfa0922kf').remove();
      }

   });
});

Remove Specific Campaign From Any URLs Containing a Specific Slug

Note: be sure to replace slkdfa0922kf in the example below with your campaign’s unique slug.

jQuery(document).ready(function($){
   $(document).on('OptinMonsterBeforeShow', function(event, data, object){

      // If url contains the "shopping-cart" slug, remove the campaign.
      if(window.location.search.indexOf("shopping-cart") &gt; -1){
         $('#om-slkdfa0922kf').remove();
      }

   });
});

Remove Specific Campaign Based on the Referral URL

Note: be sure to replace slkdfa0922kf in the example below with your campaign’s unique slug, and add the referral url you wish to target.

$(document).ready(function() {
​   $(document).on('OptinMonsterBeforeShow', function(event, data, object){
      if(document.referrer.search.indexOf("google.com") &lt; 0){
         $('#om-slkdfa0922kf').remove();
      }
​   }
});

Note: in this example the cookie name is HIDE_OM, replace with the name of the specific cookie you’re detecting.

$(document).ready(function() {
​   $(document).on('OptinMonsterBeforeShow', function(event, data, object){
      if( $.cookie('HIDE_OM') ){
         $('#om-' + data.optin).remove();
      }
​   }
});

Note: be sure to replace slkdfa0922kf in the example below with your campaign’s unique slug. In this example the cookie name is HIDE_OM, replace with the name of the specific cookie you’re detecting.

$(document).ready(function() {
​   $(document).on('OptinMonsterBeforeShow', function(event, data, object){
      if( $.cookie('HIDE_OM') ){
         $('#om-slkdfa0922kf').remove();
      }
​   }
});

WordPress — Remove Specific Campaign From Mobile Device View Only

Note: be sure to replace slkdfa0922kf in the example below with your campaign’s unique slug. Also, this snippet is only for WordPress-powered sites.

Add to your active theme’s functions.php file.

//* Filter wp_footer
  add_action( 'wp_footer', 'ekf_om_remove_campaigns_mobile' );
  
  //* Remove specific campaign on mobile
  function ekf_om_remove_campaigns_mobile() {
   if( wp_is_mobile() ) {
      echo "&lt;script type='text/javascript'&gt;
        jQuery(document).ready(function($){ 
          jQuery(document).on('OptinMonsterBeforeShow', function(event, data, object){
    
            // Replace YOUR_OPTIN_SLUG with the slug of the optin you want to remove on mobile
            jQuery('#om-YOUR_OPTIN_SLUG').remove();
          });
        });
      &lt;/script&gt;";
    } 
  } 

Interested in more ways to extend the functionality of OptinMonster? You can also check out our documentation on the OptinMonster JavaScript Events API for additional event triggers and useful examples!