Easily add Google’s DoubleClick ads in your HTML5 games with a little Phaser plugin

When you create an HTML5 game, sooner or later you will want to monetize it. If you plan to add ads, it can be quite a pain since there isn’t that much information around the web about HTML5 content and ads. DoubleClick network seems to be a good choice and there are good news because Orange Games developed a plugin for providing nice ads integration in your Phaser games, have a look at this example:
You may have to reload the iframe to see it back in action. You can find all the required docs in the official GitHub page, anyway here is the core of the integration:
<!DOCTYPE html&gt;
<html&gt;
<head&gt;
    <meta charset="UTF-8"&gt;
    <meta http-equiv="x-ua-compatible" content="ie=edge"&gt;
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no" /&gt;

    <title&gt;Phaser Advertisement example</title&gt;

    <script type="text/javascript" src="//imasdk.googleapis.com/js/sdkloader/ima3.js"&gt;</script&gt;

    <!-- Include Phaser Crisp Text JS --&gt;
    <script type="text/javascript" src="../build/phaser.min.js"&gt;</script&gt;
    <script type="text/javascript" src="../build/phaser-ads.js"&gt;</script&gt;

    <style type="text/css"&gt;
        body {
            margin: 0 auto;
            padding: 0;
        }

        #game-container, #ad-container {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 800px;
            height: 600px;
        }
    </style&gt;

    <!-- Game we want to track --&gt;
    <script type="text/javascript"&gt;
        var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game-container', { create: create, preload: preload, resize: resize});
        Phaser.Device.whenReady(function () {
            game.plugins.add(Fabrique.Plugins.AdManager);
        });

        function preload() {

        }

        function create(){
            //Set the ad provider, we use google AdSense (ima 3 sdk)
            game.ads.setAdProvider(new Fabrique.AdProvider.AdSense(
                    game,
                    'game-container',
                    'ad-container',
                    'https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&amp;iu=/124319096/external/single_ad_samples&amp;ciu_szs=300x250&amp;impl=s&amp;gdfp_req=1&amp;env=vp&amp;output=vast&amp;unviewed_position_start=1&amp;cust_params=deployment%3Ddevsite%26sample_ct%3Dskippablelinear&amp;correlator='
            ));

            //Content paused event is fired when the content (game) should be paused, and the ad will be played
            game.ads.onContentPaused.add(function () {
                console.log('Started playing add');
            });

            //This is fired when the ad is finished playing and the content (game) should be resumed
            game.ads.onContentResumed.add(function () {
                console.log('Finished playing add');
            });

            //This is how we request an add
            game.ads.requestAd();

            //the actual game content, behind the ad
            var text = game.add.text(game.width / 2, game.height / 2, 'If you see this, there is no Ad beeing displayed', {
                font: '30px Arial',
                fill: '#ffffff'
            });
            text.anchor.set(0.5);
        }

        function resize() {
            console.log('resized');
        }
    </script&gt;
</head&gt;
<body&gt;
<div id="content-wrapper"&gt;
    <div id="game-container"&gt;</div&gt;
    <div id="ad-container"&gt;</div&gt;
</div&gt;
</body&gt;
</html&gt;
Let me explain highlighted lines: Line 45: the game itself. Line 46: the id of game container. Line 47: the id of ad container. Line 48: the tag of your video ad. It’s not that easy to generate a working tag for video ads, so let me know if you need a tutorial about it. I think I’ll try this service in a game I am about to publish.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers