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!

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

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

OptinMonster has transformed our business. Before integrating their optins, our clients saw a 2% conversion rate from web traffic, which is slightly above the standard for their industry. With the integration of OptinMonster’s lightbox popup, exit-intent technology, split testing, and other tools, our client’s conversion rates soared by an unprecedented 500%. They attracted substantially more customers while spending less on digital advertising. Their success propelled them to the top of their markets, outperforming national chains and other medical spas with much bigger marketing budgets. Our client’s success in their market has distinguished us in our own. We are now one of the most sought-after marketing firms in the industry of aesthetic medicine. We know that the integration of OptinMonster, more than any other tool or marketing strategy, has supported this growth and achievement.

chris zelig medstar media

Chris Zelig

Partner, Medstar Media

OptinMonster has been a game-changer for us. From driving donations to collecting new leads, there is nothing that this simple tool has failed to accomplish. Our lead collection rate has jumped by over 1,000%.

joe lowe american bird conservancy

Joe Lowe

Director of Digital Communications, American Bird Conservancy

Through OptinMonster’s exit-intent popups, we have more than doubled our sales conversion rate. Best of all is the compound effect of using OptinMonster over a long period of time—more conversions mean more customers, which means more word of mouth.

mitch smith, managing director, shockbyte

Mitch Smith

Managing Director, Shockbyte.com