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
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.
Request toReward Url
won't be sent.
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 equalstrue
.
Return value
AdController
AdController methods
show
Loads and shows ad.
Syntax
AdController.show();
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:tsinterface 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:
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
})
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
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
}
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
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
AdController.addEventListener('onReward', () => {
// your code to reward user
});
Parameters
event
Types of events:
Event Occurs onStart
when the first banner frame is shown onSkip
when user closed the ad onReward
when user watched rewarded banner till the end onComplete
when user watched interstitial banner till the end or close it onError
when user gets an error during render or playing the banner onBannerNotFound
when there is no banner to display onNonStopShow
when user is trying to watch several ads in a row TIP
If you want to show your custom alert, you can subscribe to
onBannerNotFound
oronNonStopShow
.
Otherwise we'll show the default alert.
The alert is displayed in the user's language or in English
and looks like this 👇
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
AdController.removeEventListener('onReward', rewardFuction);
Parameters
The same as in addEventListener
Return value
None(
undefined
)