playgen.io

Documentation

Ads

Introduction

Learn how to integrate ads with playgen.io games using event listeners and the built-in SDK.

playgen.io games embedded via iframe can trigger events to allow seamless ad integration within your client page. There are two main ways to integrate ads:

  1. Listen to game events emitted from the iframe, such as PLAYGEN_GAME_STARTED or PLAYGEN_REWARDED, and use them to trigger ads using your preferred ad provider or logic.
  2. You can use the playgen.io Ad SDK, if you want which provides a built-in solution to handle ad playback or integrate your own ad playback.

Example Game Events

  • PLAYGEN_GAME_STARTED — emitted when the game session begins (ideal for pre-roll ads).
  • PLAYGEN_REWARDED — emitted when the user earns a reward (ideal for rewarded ads).

You can combine both methods depending on your specific use case and user experience goals.

Preroll Ads Integration

To show preroll ads when the game starts, listen for the PLAYGEN_GAME_STARTED event from the iframe and trigger an ad accordingly.

Example

<iframe src="https://game.playgen.io/<GAME_ID>" id="game-frame"></iframe>
<script src="https://cdn.playgen.io/playgen-ad-sdk.min.js"></script>
<script>
  Playgen.ad({
    iframeId: "game-frame",
    adTag: "YOUR_AD_TAG_URL",
  });
 
  window.addEventListener("message", function (event) {
    if (event.data?.event_name === "PLAYGEN_GAME_STARTED") {
      Playgen.showAd();
    }
  });
</script>

Rewarded Ads Integration

When a user completes a task or reaches a rewardable moment in a game, the game will emit the PLAYGEN_REWARDED event. You can then show a rewarded ad.

Example

<iframe src="https://game.playgen.io/<GAME_ID>" id="game-frame"></iframe>
<script src="https://cdn.playgen.io/playgen-ad-sdk.min.js"></script>
<script>
  Playgen.ad({
    iframeId: "game-frame",
    adTag: "YOUR_AD_TAG_URL",
  });
 
  window.addEventListener("message", function (event) {
    if (event.data?.event_name === "PLAYGEN_REWARDED") {
      Playgen.showAd();
    }
  });
</script>

Playgen Ad SDK API

The Playgen Ad SDK allows easy integration of ad playback within your client pages using events from embedded game iframes.

SDK URL

<script src="https://cdn.playgen.io/playgen-ad-sdk.min.js"></script>

Initialization

Playgen.ad({
  iframeId: "game-frame", // Required — The ID of the iframe containing the game
  adTag: "YOUR_AD_TAG_URL", // Required — The VAST ad tag to be used
});

Methods

Playgen.showAd()

Manually trigger an ad playback based on the initialized configuration.

Playgen.ad(config)

Initializes the ad system. Accepts the following parameters:

  • iframeId (string): ID of the iframe that will post events.
  • adTag (string): VAST tag URL to use for ad playback.

Notes

  • Ensure the iframe is from a trusted origin and allows postMessage communication.
  • Ads will be displayed over the iframe and paused during playback.
  • Recommended to always wait for specific events like PLAYGEN_GAME_STARTED or PLAYGEN_REWARDED before triggering ads.

On this page