Sub topic 9
Frequency Caps and Targeting
This chapter assumes you can write basic HTML and JavaScript. You should also know how a pop-up is triggered, as covered in the previous chapter. Here you will limit how often a pop-up appears and decide who should see it.
Why caps and targeting matter
A pop-up that appears on every page view annoys visitors. It also teaches them to close it without reading. A frequency cap is a rule that limits how often one visitor sees a prompt. Targeting is a rule that decides which visitors are eligible at all. Together they keep the pop-up useful and keep complaints low.
Frequency caps
The scroll-triggered example from the previous chapter already contains a cap. It stores a timestamp in localStorage when the dialog closes, and it skips the pop-up for seven days. You can reuse that pattern for other rules:
- Per-visitor cooldown: after a dismissal, wait a set number of days.
- Per-session limit: show the prompt at most once per visit.
- Permanent suppression: never show it again once the visitor subscribes.
A subscriber should never see the pop-up again. Set a flag when the form is submitted successfully, and check it before every trigger.
const DISMISSED_KEY = 'signupDismissedAt';
const SUBSCRIBED_KEY = 'signupSubscribed';
const COOLDOWN_MS = 7 * 24 * 60 * 60 * 1000;
function storageGet(key) {
try {
return localStorage.getItem(key);
} catch (e) {
return null; // storage can be blocked
}
}
function storageSet(key, value) {
try {
localStorage.setItem(key, value);
} catch (e) {
// ignore: the pop-up may reappear, which is acceptable
}
}
function canShowPopup() {
if (storageGet(SUBSCRIBED_KEY) === 'yes') return false;
if (sessionStorage.getItem('signupShownThisSession')) return false;
const t = Number(storageGet(DISMISSED_KEY));
return !(t > 0 && Date.now() - t < COOLDOWN_MS);
}
function markShown() {
sessionStorage.setItem('signupShownThisSession', '1');
}
function markDismissed() {
storageSet(DISMISSED_KEY, String(Date.now()));
}Call canShowPopup() before opening the dialog, markShown() when it opens, and markDismissed() in the dialog's close event.
Targeting
Targeting uses what you know about the visit to decide whether to show a prompt. Common signals are:
| Signal | Example rule | Reason |
|---|---|---|
| Page type | Show only on blog posts | The visitor is reading, so a related offer fits. |
| Traffic source | Show to search visitors, not to returning subscribers | New visitors are the main audience. |
| Visit count | Wait until the second visit | The visitor has already shown interest. |
| Subscriber status | Hide for anyone already on the list | Repeat prompts add nothing. |
Match the offer to the page. A visitor reading a guide on bread baking is more likely to want a baking checklist than a general newsletter. Start with one or two simple rules. Every extra rule shrinks your audience and makes results harder to read.
Common errors
- Storage is not shared across devices. A visitor on a phone and a laptop is counted as two people. Accept this, or store the status on the server for logged-in users.
- The cap is cleared with the cookies. Treat the cap as a courtesy, not a guarantee.
- The flag is set on dismissal only. If you forget to suppress after a successful sign-up, new subscribers keep getting asked.
- Storage throws an error. Private modes and strict browser settings can block
localStorage. Wrap access intry/catch, as above.
Exercise
A visitor closes your pop-up on Monday and your cooldown is seven days. They also subscribe through the inline form on Wednesday. Which rule should stop the pop-up from appearing on Thursday, and why?
Show answer
The permanent suppression rule for subscribers. The cooldown would already block the pop-up until the next Monday, but it ends. A flag set on successful sign-up keeps the pop-up hidden for good, so a subscriber is never asked again once the cooldown expires.
Exercise
Why should access to localStorage be wrapped in try/catch?
Show answer
Some browsers and privacy modes block storage and throw an error. Without try/catch, the script would stop and the pop-up logic would fail. With it, the page keeps working and the pop-up may simply show more often.