Remove content for paying visitors
This guide describes one way you can remove content for paying visitors after the monetization
event fires. The event only fires after an outgoing payment request is successfully created. If the event doesn’t fire, then the content will remain visible on your page.
Before you begin
- You must have a wallet address or payment pointer assigned to you by your wallet provider.
- Each page you want to monetize must served over HTTPS.
User experience
For visitors without Web Monetization, your content appears as soon as your page loads.
For visitors with Web Monetization, there’s a three-second grace period before your content appears. This gives Web Monetization a chance to initialize and prevents the content from briefly appearing to paying visitors.
If Web Monetization:
- Initializes within the grace period, the content is hidden
- Fails to initialize within the grace period, the content is shown
- Initializes any time after the grace period, the content is hidden
Example use case: Remove ads
In this example, you’ll give your paying visitors an ad-free experience by preventing the ad from loading on the page once Web Monetization initializes.
Assuming Web Monetization initializes within the grace period, neither the ad nor any related images and/or trackers (which aren’t part of this example) are loaded onto the page at all.
The code
How it works
Let’s start with the code for showing your ad.
We’ll bind the monetization
event to its respective event handler if the visitor is web monetized. This prevents the ad from loading on the page once Web Monetization initializes. Assuming it initializes within the grace period, the ad isn’t added to the page at all. This means any related images and trackers aren’t loaded either.
The hasPaid
variable in the timer is for when/if Web Monetization starts after the grace period.
As soon as the page loads, the ad will immediately appear to any visitor who isn’t web monetized. This is handled via !window.MonetizationEvent
, shown below.
If the visitor has Web Monetization in their browser, then the monetization
event must fire within 3 seconds (3000ms) to indicate that Web Monetization has initialized. If it doesn’t initialize by the timeout, the ad is shown to the visitor.
Interactive example
This example simulates showing and hiding an ad based on a visitor’s Web Monetization state.
The example doesn’t require you to have Web Monetization enabled in your browser and no real payments are occurring.
Click View as web monetized/non-web monetized visitor to toggle your monetization state. If source files appear instead of the example, click View App in the bottom-right corner.