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

@adlad/adlad

v0.14.0

Published

Ad sdks management for web games made easy.

Downloads

49

Readme

AdLad allows you to easily integrate ads for your games without the need for different implementations from every publisher. Instead, you can configure which plugins you would like to use, and whenever you want to show an ad, AdLad will forward your request to the plugin that is currently active.

List of available plugins

Usage

To quickly get started, you can use jsdelivr.com to import AdLad:

<script type="module">
	import {AdLad} from "https://cdn.jsdelivr.net/npm/@adlad/adlad/mod.min.js";

	// You need at least one plugin in order for AdLad calls to function.
	import {crazyGamesPlugin} from "https://cdn.jsdelivr.net/npm/@adlad/plugin-crazygames/mod.min.js";

	const adLad = new AdLad({
		plugins: [
			crazyGamesPlugin(),
		]
	});
</script>

You can immediately start making calls, but ads won't show until the plugin has initialized.

await adLad.showFullScreenAd();

Both showFullScreenAd() and showRewardedAd() return an object that allows you to determine if an ad was shown.

const result = await adLad.showRewardedAd();
console.log(result); // { didShowAd: true, errorReason: null }

Some portals require you to report loading and gameplay state, which can be done with the methods below. You can call these as often as you like, if you call gameplayStart() twice in a row for example, AdLad will make sure it is passed to the plugin only once.

adLad.gameplayStart();
adLad.gameplayStop();
adLad.loadStart();
adLad.loadStop();

It's important that you mute audio and pause gameplay during ads. This can be done by observing the following values:

adLad.needsPause; // true | false
adLad.needsMute; // true | false

Usually these both become true once you make a call for showing ads, but plugins can manually override this value. That way your game always behaves according to the policies of the platform it's embedded on.

You can also listen for changes to these values:

adLad.onNeedsPauseChange((needsPause) => {
	console.log(needsPause);
});
adLad.onNeedsMuteChange((needsMute) => {
	console.log(needsMute);
});

You can use adLad.removeOnNeedsPauseChange and adLad.removeOnNeedsMuteChange To remove your callbacks.

Once you have experimented enough, you can bundle AdLad using a build tool of choice. AdLad is available as an npm package and on deno.land/x.

For the full API reference, see this page.

Plugins

By default the first available plugin from your plugins option is used. You can get the id of the active plugin using adLad.activePlugin.

To override which plugin is active, you can pass the plugin id via the plugin option like so:

const adLad = new AdLad({
	plugins: [notSoCoolPlugin(), myCoolPlugin()],
	plugin: "my-cool-plugin",
});

Alternatively, you can use the ?adlad=my-cool-plugin query string. This is useful when you want to host your own page, while still allowing gaming portals to use a specific plugin. You can simply provide each gaming portal with a different url.

Tree shaking unused plugins

If you make separate builds for each publisher, you can conditionally include plugins based on build flags. That way, unnecessary code is excluded from your build.

Ultimately, the best way to do this depends on your build pipeline. But let's assume you use something like Rollup and @rollup/plugin-replace.

In your build script you can configure the rollup plugin like so:

replace({
	values: {
		DEBUG_BUILD: String(debug),
		PUBLISHER_BUILD: `'${publisher}'`,
	},
	preventAssignment: true,
});

Where debug and publisher are variables in your script set by a commandline flag for instance.

Then in your client code you can do something like this:

// Use a local import path which points to your node_modules,
// or use an import map if necessary.
import { AdLad } from "https://cdn.jsdelivr.net/npm/@adlad/adlad/mod.min.js";
import { dummyPlugin } from "https://cdn.jsdelivr.net/npm/@adlad/plugin-dummy/mod.js";
import { somePublisherPlugin } from "./path/to/publisher/plugin.js";

// Declare the same global variables.
// This is only required if you want to be able to run this script without a build step.
globalThis.DEBUG_BUILD = true;
globalThis.PUBLISHER_BUILD = "none";

let plugins = [];
let plugin = "none";

if (DEBUG_BUILD) {
	plugins.push(dummyPlugin());
	plugin = "dummy";
}

if (PUBLISHER_BUILD == "somepublisher") {
	plugins.push(somePublisherPlugin());
	plugin = "some-publisher";
}

const adLad = new AdLad({
	plugins,
	plugin,
});

console.log(adLad.activePlugin); // Should print "dummy" or "some-publisher" based on your build flags.

Now if your build pipeline is configured correctly, it will tree shake these plugins depending on the configured DEBUG_BUILD and PUBLISHER_BUILD values.