Skip to main content

Hide popups when a side cart or mobile menu is open

Use a custom code targeting rule to keep popups closed while a side cart or menu is open.

Written by Rob D

If your store has a side cart, drawer menu, or mobile navigation that slides over the page, you can keep your popups from opening while it's open. This stops the popup and the menu from overlapping.

You'll do this with a targeting rule that runs a short piece of code. In this guide you'll:

  1. Find your menu or cart's selector in your browser's developer tools.

  2. Create a targeting rule that checks whether it's open.

  3. Add the rule to your campaign.

💡 Note: To hide popups on certain pages instead, see Hide popups on certain pages.

Step 1: Find your menu or cart's selector

  1. Open your store in a desktop browser.

  2. Open the side cart or menu you want to check for.

  3. Right-click it and select Inspect to open the developer tools.

  4. Close and reopen the menu, and watch which attribute on the element changes.

You're looking for two things:

  • A unique ID on the menu or cart element.

  • A class that's only there while it's open.

For example, you might see:

<div id="site-mobile-nav" class="visible">

Here, the ID is #site-mobile-nav and the open-state class is visible. Every theme is different, so your names will vary.

Step 2: Create a targeting rule

  1. In Alia, go to Campaigns > Targeting rules.

  2. Click Create rule.

  3. Click Select a filter and choose Custom code.

    [Screenshot: Select a filter > Custom code, from the Notion draft]

  4. Paste this code:]

    return !document.querySelector('#site-mobile-nav')?.classList.contains('visible');

  5. Replace #site-mobile-nav with your element's ID (keep the #), and visible with your open-state class.

  6. Give the rule a clear name, such as "Side cart closed". Changes on the Targeting rules page save automatically.

How the rule works

  • While the menu is open, the code returns false, so the visitor doesn't match and the popup doesn't open.

  • While it's closed, the code returns true, so the visitor matches.

  • If Alia can't find the element at all, the code also returns true, so popups still show.

Alia checks targeting about once a second while the visitor is on the page, so the popup can open as soon as the menu closes.

Step 3: Add the rule to your campaign

  1. Go to Campaigns and click Targeting on the campaign's card.

  2. Click Select a filter and choose In targeting rule or campaign.

  3. Choose the rule you created.

  4. Click Save.

The campaign's popup won't open while the side cart or menu is open.

💡 Tip: If only one campaign needs this, you can skip the saved rule and add the Custom code condition straight to that campaign's targeting. A saved rule is easier when several campaigns need it, because you only update it once.

Tips

  • Test on both desktop and mobile. Some themes use different elements for the mobile and desktop menus, so you may need a rule for each.

  • Make separate rules for carts, menus, and search overlays, or combine them in one rule.

  • If the popup still appears, check that the ID and class match exactly. They're case sensitive.

  • Your code must return exactly true to match, within one second.

Still have questions?

Start a chat from inside the Alia app, or use the chat in our Help Center.

Did this answer your question?