Javascript ADS
Add mid-roll and rewarded ads to a JavaScript web game on VIVERSE with the VIVERSE Ad SDK. Covers setup, cooldowns, pausing gameplay, and granting rewards.
Before you start
The VIVERSE Ad SDK runs only on the VIVERSE platform. Ads don't display when you run your game locally, so build your game to keep working when the SDK fails to initialize.
You need:
A VIVERSE App ID for your game.
A game you can publish to VIVERSE for testing.
Ad formats on this page
The SDK uses its own names for each format. Mid-roll ads are the Overlay and Video formats, and rewarded ads are the Reward Overlay and Reward Video formats.
Overlay
Mid-roll, full-screen interstitial
Your game, between levels
showOverlay()
Video
Mid-roll, video interstitial
Your game, between levels
showVideo()
Reward Overlay
Rewarded, full-screen, 15s video, image, or image
The player, from a button
showRewardOverlay()
Reward Video
Rewarded, 60s video
The player, from a button
showRewardVideo()
The SDK also supports banner ads, which this page doesn't cover.
Set up the SDK
Load the SDK script, then initialize it once before you call any show method.
Pause and resume your game during an ad
Every format on this page covers the screen, so your game must stop while the ad plays. The SDK tells you when to pause and when to resume through callbacks.
onBreakStart
Pause gameplay, animations, input, background music, and sound effects.
onBreakEnd
Resume everything you paused.
onAdError
Resume everything you paused, and let the game continue.
Resume in both onBreakEnd and onAdError. When no ad is available, the SDK calls onAdError instead of showing an ad, and onAdError can fire before onBreakStart. Make your pause and resume functions safe to call more than once.
Show mid-roll ads between levels
Show Overlay or Video ads at natural breaks, such as after a level ends and before the next one loads. Don't show one when the game first starts, before the player has played anything.
Overlay and Video share one cooldown. Wait at least 30 seconds after either one before you call either one again. Track the cooldown in your game; the example below uses a single shared timer.
showVideo() passes a skipped boolean to onBreakEnd if you want to log whether the player skipped the video.
Show rewarded ads
A rewarded ad gives the player something in exchange for watching, such as an extra life or in-game currency. The player must start it. Show it only from a button or similar control the player chooses, such as Watch an ad to continue, and never autoplay it.
Grant the reward only inside onRewardGranted. Don't grant it in onBreakEnd.
Test your integration
Ads appear only when your game runs on VIVERSE. Publish a build to VIVERSE, then check each item below.
Mid-roll ads appear between levels and never at game start.
A second mid-roll ad within 30 seconds of the first one is skipped.
Gameplay, input, music, and sound effects stop during every ad and resume afterward.
The game keeps running when an ad fails. To test this, turn on an ad blocker.
Rewarded ads start only from a player action, and each ad grants its reward once.
API reference
These are the fullscreen methods and callbacks this page uses. Call every method on window.ViverseAdSDK after init() resolves. Only one ad can show at a time.
init({ appId })
None. Returns a promise.
Promise
showOverlay(options)
onAdReady, onBreakStart, onBreakEnd, onAdError(error)
OverlayAd
showVideo(options)
onBreakStart, onBreakEnd(skipped), onAdError(error)
VideoAd
showRewardOverlay(options)
onAdReady, onBreakStart, onRewardGranted, onBreakEnd, onAdError(error)
RewardOverlayAd
showRewardVideo(options)
onBreakStart, onRewardGranted, onBreakEnd(skipped), onAdError(error)
RewardVideoAd
All callbacks are optional. onAdReady fires when the ad has loaded, just before it displays.
Last updated
Was this helpful?