DEV Community

Cover image for Exit intent that works on mobile, not just desktop
Arafat Islam
Arafat Islam

Posted on • Originally published at arafatcro.dev

Exit intent that works on mobile, not just desktop

Originally published on my site, arafatcro.dev/guides.

You copy the exit-intent snippet, it fires nicely when the cursor heads for the close button, and you ship. Then you check the data and half your traffic never triggered it, because they were on phones and a phone has no cursor to chase. Exit intent is not one trick. It is a different signal on each device, fired once, and built so it does not become the thing people leave over.

The trick everyone copies

The standard snippet listens for the mouse leaving the top of the page. When the cursor crosses the top edge, heading for the tab bar or the close control, you call it intent to leave and show your overlay. On desktop it works.

The problem is that this signal only exists on a device with a pointer. On a phone or tablet there is no cursor, nothing ever leaves the top of the viewport, and that listener never fires. So the most common exit-intent implementation does nothing for anyone on a phone, which on a lot of sites is more than half the traffic. Nobody notices, because it looks like it is working on the desktop where you tested it.

Detect the device, use the signal that exists on it

On desktop it is the cursor arcing toward the browser chrome. On touch you read it from behaviour instead: a fast flick upward toward the address bar, or a stretch of inactivity. None is as crisp as the desktop signal, so you tune the thresholds rather than firing on the first twitch. Put both behind one helper so your variation code does not care which fired.

function exitIntent(callback, {
  sensitivity = 20,        // px from the top edge that counts as "leaving"
  mobileScrollDelta = 60,  // px of fast upward scroll that counts as a flick
  idle = 0,                // ms of inactivity before firing (0 = off)
  once = true,
} = {}) {
  let fired = false;
  let lastY = window.scrollY;
  let idleTimer;

  function teardown() {
    document.removeEventListener("mouseout", onMouseOut);
    window.removeEventListener("scroll", onScroll);
    clearTimeout(idleTimer);
  }

  const trigger = () => {
    if (fired) return;
    if (once) fired = true;
    teardown();
    callback();
  };

  const onMouseOut = (e) => {
    if (!e.relatedTarget && e.clientY <= sensitivity) trigger();
  };

  const resetIdle = () => {
    clearTimeout(idleTimer);
    idleTimer = setTimeout(trigger, idle);
  };

  const onScroll = () => {
    const y = window.scrollY;
    if (lastY - y > mobileScrollDelta) trigger(); // fast flick upward
    lastY = y;
    if (idle) resetIdle();
  };

  document.addEventListener("mouseout", onMouseOut);
  window.addEventListener("scroll", onScroll, { passive: true });
  if (idle) resetIdle();

  return teardown;
}
Enter fullscreen mode Exit fullscreen mode

This helper lives in ab-test-helpers, a small set of dependency-free helpers for client side tests.

Fire once, and do not nag

An overlay that reappears on every page stops being a save and becomes the reason someone leaves. Fire the detector once per session, and remember a dismissal so a visitor who said no is not asked again.

function allowOncePerDays(key, days = 7) {
  const name = `fc_${key}`;
  try {
    const until = Number(localStorage.getItem(name) || 0);
    if (Date.now() < until) return false;
    localStorage.setItem(name, String(Date.now() + days * 864e5));
    return true;
  } catch (e) {
    return true; // storage blocked: do not suppress
  }
}

exitIntent(() => {
  if (sessionStorage.getItem("exit_dismissed") === "1") return;
  if (!allowOncePerDays("exit_offer", 14)) return;
  showOverlay();
}, { idle: 20000 });
Enter fullscreen mode Exit fullscreen mode

It is a modal, so make it accessible

The overlay is a dialog. Trap focus inside it, return focus on close, let Escape dismiss it, and give it the ARIA roles a screen reader needs. Skip these and a keyboard user is trapped behind it or lost on the page underneath.

Measure the intent, not just the impression

Fire an analytics event when leave intent is detected, separate from whether the overlay rendered. Then the control group logs exit intent too, and you can measure how often it happens and how the variation changed behaviour, not just count the people who saw the modal.

The short version

Exit intent is a desktop signal plus a couple of touch signals, behind one detector that fires once. Cap it so it does not nag, build it as a real dialog with a focus trap, and log the detection separately from the popup. Do that and it works for everyone, not just the half on a laptop.


I write up the hard parts of client side A/B testing at arafatcro.dev/guides. The full version of this post, with a comparison table of every signal, is here.

Top comments (0)