npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ima-wrapper

v1.0.8

Published

Wrapper for IMA SDK. IMA Wrapper is a convenience layer around Google Interactive Media Ads (IMA SDK for HTML5) which tries to make using IMA less cumbersome for common monetization use cases.

Downloads

283

Readme

npm version downloads per week license

ima-wrapper

Wrapper for IMA SDK. IMA Wrapper is a convenience layer around Google Interactive Media Ads (IMA SDK for HTML5) which tries to make using IMA less cumbersome for common monetization use cases.

This SDK can be used to create a simple in-stream/out-stream video ad player, or to implement more complex monetization scenarios such as:

  • Creating multiple instances of the IMA SDK to request multiple ad requests at the same time (in parallel)
  • Integration with VPAID

NOTE: If you have already included the ima3.js script, the wrapper will check the IMA client side SDK version if it was included and will use it, if not, the wrapper will try to load ima3.js from //imasdk.googleapis.com/js/sdkloader/ima3.js.

Table of Contents

Usage

import IMAWrapper from 'ima-wrapper';

// Get your HTML element for ad container
const adContainer = document.getElementById('ad-container');
// Get your video element
const videoElement = document.getElementById('video-element');
// Define IMA wrapper, pass ad container and video element
const adsManager = new IMAWrapper(adContainer, videoElement, (error) => {
    if(error) {
        console.log(error);
        return;
    }
});
// Subscribe for events
// AdsManagerLoaded
adsManager.addEventListener('AdsManagerLoaded', function() {
    // Get height and width of your video element
    let width = videoElement.clientWidth;
    let height = videoElement.clientHeight;
    let viewMode = 'normal'; // fullscreen
    // Init
    try {
        adsManager.init(width, height, viewMode);
    } catch (adError) {
        // Play your context without ads, if an error occurs
    }
});
// AdError
adsManager.addEventListener('AdError', function(adError) {
    if(adsManager) {
        // Removes ad assets loaded at runtime that need to be properly removed at the time of ad completion
        // and stops the ad and all tracking.
        adsManager.abort();
    }
    // ... 
});
// AdLoaded
adsManager.addEventListener('AdLoaded', function(adEvent) {
    // Ad loaded, awaiting start
    // Check if ad type is linear
    if(adEvent.isLinear()) {
        try {
            // Start ad
            adsManager.start();
        } catch (adError) {
            // Play video content without ads in case of error
        }
    } else {
        // Ad is not linear
    }
});
// AdStarted
adsManager.addEventListener('AdStarted', function() {
    // Pause your video content
    videoElement.pause();
});
// ...
// AdDurationChange
// AdSizeChange
// AdImpression
// AdVideoStart
// AdVideoFirstQuartile
// AdVideoMidpoint
// AdVideoThirdQuartile
// AdVideoComplete
// AdPaused
// AdPlaying
// AdStopped
// AdSkipped
// AdClickThru
// ...
// AllAdsCompleted
adsManager.addEventListener('AllAdsCompleted', function() {
    // Play your video content
    videoElement.play();
});


// VAST tag url
let vastUrl = 'your VAST tag url';

// Request Ads
adsManager.requestAds(vastUrl);

/*
// VAST XML
let vastXML = `<?xml version="1.0" encoding="UTF-8"?>
    <VAST version="2.0">
      <Error><![CDATA[http://example.com/empty-no-ad]]></Error>
    </VAST>`;
adsManager.requestAds(vastXML);
 */

Documentation

Pre-bundled versions

Browser script

A pre-bundled version of ima-wrapper is available: ima-wrapper.js [minified].

You can add the script directly to your page and access the library's components through the adserve.tv object.

<script src="ima-wrapper.js"></script>
// Get your HTML elements for ad container and video element
const adContainer = document.getElementById('ad-container');
const videoElement = document.getElementById('video-element');
// Define IMA wrapper, pass ad container and video element
const adsManager = new adserve.tv.IMAWrapper(adContainer, videoElement, (error) => {
   if(error) {
       console.log(error);
       return;
   } 
});

Install

Get Started

ima-wrapper is available as an NPM package and can be easily installed with:

$ npm i ima-wrapper

Using Git

$ git clone https://github.com/basil79/ima-wrapper.git
$ cd ima-wrapper
$ npm ci

Build

To build the project for development:

$ npm run build:dev

To build the project for production:

$ npm run build:prod

This will generate the following file:

  • ./dist/ima-wrapper.js - Minified browser production code

Run

$ npm start

Then navigate to: http://localhost:8087 in your browser

Supported Browsers

ima-wrapper is supported all modern browsers.

Contribute

See CONTRIBUTING