web-monetization-video-ads v0.1.60
Web Monetization Video Ads
Web Monetization Video Ads is a utility that allows you to monetize videos.
It uses web monetization when available and loads ads as a fallback.
Installing
npm install web-monetization-video-ads --saveSet Up and Usage
The package exposes 3 functions:
initVideoMonetizerfor monetize the video element using web monetization API.initVideoAdvertizera wrapper around IMA SDK, used for advertising.initVideoAdsMonetizerincludes both of the sub modules to provide web monetization and advertising as a fallback.
Simple
For the bare minimum set up, importinitVideoAdsMonetizer and pass some configurations as follow:
import { initVideoAdsMonetizer } from "web-monetization-video-ads";
const paymentPointer = "$your.paymentpointer.com";
const tagUrl ="your-custom-tag-url";
const videoElement = document.querySelector("#video-to-monetize");
const config = {
videoElement,
monetizationConfig: {
paymentPointer,
},
adsConfig: {
tagUrl,
},
startAdsTime:4000
};
const videoAdvertizer = initVideoAdsMonetizer(config);Config
videoElementThe video element you want to monetize. At this moment the library supports just one videoElement per page. You must wrap the video with a container with no other child elements in it. The container is crucial to make your video responsive, please see an example here .monetizationConfigUsed byinitVideoMonetizerfor monetizing.adsConfigUsed byinitVideoAdvertizerfor initializing and loading ads.startAdsTime (default = 4000)Waiting time before attempting to initialize the advertising.
What it does
Launches initVideoAdvertizer when document.monetization is not present in the page or if any monetization errors occur. In all other cases it runs initVideoMonetizer.
The videoElement is paused on every monetizationstop event.
Sub Modules
VideoMonetizer
It's main purpose is to start and stop web monetization whenever a video is playing or has been paused.
import { initVideoMonetizer } from "web-monetization-video-ads";
const monetizationConfig = {
paymentPointer,
receiptVerify:{
enabled: false,
apiUrl: "xxxxxx",
verifyEndPoint: "xxxx",
createCustomPaymentPointer: true,
bodyParsed: true,
},
vanillaCredentials = {
enabled: false,
clientId: "xxxxxx",
clientSecret: "xxxxx",
};
fakeMonetization: {
enabled: false,
triggerFail: {
onStart: false,
onProgress: false,
timeout: 6000,
},
},
};
const videoMonetizer = initVideoMonetizer(monetizationConfig);This modules includes some functionalities and helper functions for the web monetization.
Config
paymentPointeryour custom payment pointerreceiptVerifyit follows the receipt verifier api standards to provide verification. -apiUrl (default=$webmonetization.org/api/receipts) your custom verification url -verifyEndPoint (default='verify')your custom verification endpoint - IfcreateCustomPaymentPointer (default=false)is set to true, it will format thepaymentPointeras showed in the web monetization api documentation. - IfbodyParsed (default=true)is set to true will parse thereceiptas an object property of the body call, conversely will pass it as a string.- Alternatively to verify receipts you can also use vanilla.so , by enabling
vanillaCredentials. -clientIdyour vanilla id -clientSecretyour vanilla secret key fakeMonetizationThis can be used to mock web monetization events, when developing or testing. - WithintriggerFailyou can force web monetization error events -onStart (default=false)set to true will trigger amonetizationstart-error
-onProgress (default=false)set to true will triggermonetizationprogress-error-timeout (default=6000)for specifying in millisecond when to triggermonetizationprogress-error.
Further functionalities
webMonetizationChecker.jsextends the web monetization with the following events listeners: -monetizationstart-erroruses the same technique in https://testwebmonetization.com/ for detecting when monetization doesn't start after a specific period of time. This is defined bystartErrorWaitingTime (default = 8000). -monetizationprogress-errordispatched whenmonetizationprogressis not occurring after a specifying period of time. This is defined byprogressErrorWatitingTime (default = 6000). -receiptVerificationresponse: - Upon success dispatchesmonetizationreceipt. - Upon fail dispatchesmonetizationreceipt-error. -vanillaVerificationresponse returns : - Upon success dispatchesmonetizationproof. - Upon fail dispatchesmonetizationproof-error.
Example usage
document.monetization.addEventListener("monetizationstart-error", function(){
console.log("Monetization start event not occurred");
});webMonetizationFaker.jsthis is the module that mocks web monetization api.webMonetizationHelper.jshelper functions to start/stop monetization, retrieve the current payment pointer in the metatag and observe its mutations.
VideoAdvertizer
It initializes and plays the video ads and also exposes stopAds method for stopping them when needed.
import { initVideoAdvertizer } from "web-monetization-video-ads";
const adsConfig = {
tagUrl,
live:false,
interval:30
},
const videoAdvertizer = initVideoAdvertizer(adsConfig);Config
tagUrlyour custom VAST tag-url.live (default=false)optional settings to repeat an advert after a certain amount of time defined byinterval (default=30 sec).
Issues
- MultiVideos are not yet supported or tested.
- Ad-blockers are not yet supported or tested.
Not Supported
- Not supported for Safari < 10 or IE11
- On iPhone the video must have a
playinslineandmutedattributes
License
This project uses the following license: MIT License.