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.

Before You Start

Here are some things to know before you begin:

  • In most cases, you should hide / show campaigns using the Display Rules.
  • The examples provided in this guide make use of our Javascript Events API.
  • You should add the code directly to the footer of your website, before the closing </body> tag.
  • Code should be wrapped in valid <script> tags.

Remove All Campaigns From Specific URL

document.addEventListener('om.Campaign.startShow', function(event) {
    if(window.location.href == 'https://example.com/specific-page') {
        var optinCampaign = document.querySelector('#om-' + event.detail.Campaign.id);
        optinCampaign.parentNode.removeChild(optinCampaign);
    }
});

Remove Specific Campaign From Specific URL

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

document.addEventListener('om.Campaign.startShow', function(event) {
    if(window.location.href == 'https://example.com/specific-page') {
        var optinCampaign = document.querySelector('#om-CAMPAIGN_ID');
        optinCampaign.parentNode.removeChild(optinCampaign);
    }
});

Remove Specific Campaign From Any URLs Containing a Specific Slug

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

document.addEventListener('om.Campaign.startShow', function(event) {
    if(window.location.search.indexOf('shopping-cart') > -1) {
        var optinCampaign = document.querySelector('#om-CAMPAIGN_ID');
        optinCampaign.parentNode.removeChild(optinCampaign);
    }
});

Remove Specific Campaign Based on the Referral URL

Note: be sure to replace CAMPAIGN_ID in the example below with your campaign’s unique ID, and add the referral URL you want to target.

document.addEventListener('om.Campaign.startShow', function(event) {
    if(document.referrer.search.indexOf('google.com') > 0) {
        var optinCampaign = document.querySelector('#om-CAMPAIGN_ID');
        optinCampaign.parentNode.removeChild(optinCampaign);
    }
});