Skip to content

Adblock Detection

The Adblock Detection component provides functionality to detect if a user has an ad blocker enabled in their browser. It can be used to display alternative content or messages when ad blockers are detected.

The Adblock Detection component is automatically initialized when the Metro UI framework is loaded. No additional markup is required.

// Listen for adblock detection events
$(window).on("adblock-alert", function() {
console.log("Ad blocker detected!");
// Show alternative content or message
});
// Configure adblock detection globally
Metro.adblockSetup({
checkInterval: 2000,
fireOnce: true,
localhost: false
});
ParameterTypeDefaultDescription
adblockDeferrednumber0Deferred initialization time in milliseconds
checkIntervalnumber1000Interval in milliseconds to check for ad blockers
fireOnceboolean/numbertrueWhether to fire the alert event only once (true), or a specific number of times
checkStopnumber10Number of checks before stopping even if no ad blocker is detected
localhostbooleanfalseWhether to check for ad blockers on localhost
onAlertfunctionMetro.noopCallback function triggered when an ad blocker is detected
onFishingStartfunctionMetro.noopCallback function triggered when the detection starts
onFishingDonefunctionMetro.noopCallback function triggered when the detection is done
EventDescription
adblock-alertFired when an ad blocker is detected
fishing-startFired when the detection process starts
fishing-doneFired when the detection process is complete

The Adblock component provides the following methods through the Metro.Adblock object:

  • bite() - Initiates the adblock detection process by creating a hidden element with ad-related classes.
  • fishing() - Starts checking for ad blockers by monitoring the visibility of the created element.
// Manually start the adblock detection
Metro.Adblock.bite();

The Adblock Detection component works by:

  1. Creating a hidden element with class names commonly targeted by ad blockers
  2. Periodically checking if the element is hidden or removed by an ad blocker
  3. Triggering events and callbacks when an ad blocker is detected

You can configure the Adblock Detection component globally before the Metro UI framework is initialized:

window.metroAdblockSetup = {
checkInterval: 2000,
fireOnce: false,
checkStop: 5,
onAlert: function() {
console.log("Ad blocker detected!");
}
};
  • The detection is not 100% reliable as ad blockers constantly evolve their detection methods
  • Some browsers with built-in ad blocking may not be detected
  • The component has minimal performance impact as it only creates a small hidden element
  • Detection stops automatically after the specified number of checks or when an ad blocker is detected