Skip to content

API Reference

Adsgram methods

init

Create AdController which is used to show, load and destroy ad.
It's enough to initialize only once for one blockId.
If you call init function with same blockId more than once, you will get the same AdController for this blockId.

Syntax

js
const AdController = window.Adsgram.init({
  blockId: "your-block-id",
  debug: true,
  debugBannerType: "FullscreenMedia"
});

Parameters

blockId

String with digits, for example "123".
The unique identifier of the ad block that you receive in the publisher's account.
Detailed instruction on how to get blockId you can find in Get blockId section

debug Optional

Boolean value if set to true you will get response from server with test ad.

DANGER

Don't forget to remove or set to false, when release to production.
We do not add test ad impressions to the statistics, so you will not see the statistics of its impressions, shows, clicks, etc. on the platform and block page.

debugBannerType Optional

Possible values are "FullscreenMedia" and "RewardedVideo".
Use this value when you want to get exact test banner type. Works only if debug parameter equals true.

Return value

AdController

AdController methods

show

Loads and shows ad.

Syntax

js
AdController.show();
ts
const showPromise: Promise<ShowPromiseResult> = AdController.show();

Return value

Promise which becomes resolved if the user watches the ad till the end, otherwise rejected.
Result of the promise has the following types:

ts
interface ShowPromiseResult {
  done: boolean; // true if user watch till the end, otherwise false
  description: string; // event description
  state: 'load' | 'render' | 'playing' | 'destroy'; // banner state
  error: boolean; // true if event was emitted due to error, otherwise false
}

Examples

Using promises API:

js
AdController.show().then((result) => {
  // user watch ad till the end
  // your code to reward user
}).catch((result) => {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
})
ts
AdController.show().then((result: ShowPromiseResult) => {
  // user watch ad till the end
  // your code to reward user
}).catch((result: ShowPromiseResult) => {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
})

You can also achieve the same behavior using await

js
try {
  const showPromiseResult = await AdController.show();
  // user watch ad till the end
  // your code to reward user
} catch (showPromiseResult) {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
}
ts
try {
  const showPromiseResult: ShowPromiseResult = await AdController.show();
  // user watch ad till the end
  // your code to reward user
} catch (showPromiseResult: ShowPromiseResult) {
  // user get error during playing ad or skip ad
  // do nothing or whatever you want
}

TIP

Chain promise with finally if you need to add any extra action after the ad ends playing, is skipped or gets an error.

destroy

To stop showing ad and remove loaded banner data.

Syntax

js
AdController.destroy();

Return value

None(undefined)

INFO

Usually you don't need to call it manually, in case of an error, skip or ad fully watched the ad will be automatically destroyed.

addEventListener

Subscribes to event and invokes callback function if event was fired.
It is used if you need more control over the display of ads.

Syntax

js
AdController.addEventListener('onReward', () => {
    // your code to reward user
});

Parameters

event

Types of events:
EventOccurs
onStartwhen the first video frame is shown
onSkipwhen user closed the ad
onRewardwhen user watched the video till the end
onErrorwhen user gets an error during render or playing the video
onBannerNotFoundwhen there is no banner to display

WARNING

If you subscribe to onBannerNotFound we won't show the default alert.
The alert is displayed in the user's language or in English
and looks like this 👇 alert

callback

Function that will be invoked when an event of the specified type occurs.

Return value

None(undefined)

removeEventListener

Unsubscribes to event with determined callback

Syntax

ts
AdController.removeEventListener('onReward', rewardFuction);

Parameters

The same as in addEventListener

Return value

None(undefined)

Deprecated methods

load deprecated

Uses for load ad.

Syntax

js
const loadPromise = await AdController.load();
ts
const loadPromise: Promise<true> = await AdController.load();

Return value

promise always resolves with true

DANGER

Now deprecated and always resolves with true.
In the following release will be completely removed.

preload deprecated

To preload a video for an ad.

Syntax

js
AdController.preload();

Return value

None(undefined)

DANGER

Now deprecated and does nothing.
In the following release will be completely removed.