OptinMonster Logo

OptinMonster Documentation

Documentation, Reference Materials, and Tutorials for OptinMonster

Home

Docs

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

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);
    }
});


Still stuck?

How can we help?

Last updated on


Start Getting More
Leads & Sales Today
with OptinMonster!

Popups work, and you can get started for a few bucks a month. What are you waiting for?

Create and launch smart capture forms
today in minutes. What are you waiting for?