Code examples
- Rewarded format, which mean you should give user reward when he watched ad till the end and ad should be expected like after click on element.
More info about show ad requirements. - Interstitial format, which mean user can skip an ad before the end and ad should be expected during natural pauses in the flow of an app
More info about show ad requirements.
INFO
Below are code examples for the Rewarded format. These examples are also relevant for the Interstitial format. The only difference is that the AdController.show() promise resolves for ads that have been viewed till the end, as well as those that have been closed. See the simple Interstitial code example.
React
Insert script
Insert this script inside <head> tag of your HTML.
Create useAdsgram hook
Create file inside your project with custom hook useAdsgram.
More info about custom hooks in React documentation.
import { useCallback, useEffect, useRef } from 'react';
export function useAdsgram({ blockId, onReward, onError }) {
const AdControllerRef = useRef(undefined);
useEffect(() => {
AdControllerRef.current = window.Adsgram?.init({ blockId });
}, [blockId]);
return useCallback(async () => {
if (AdControllerRef.current) {
return AdControllerRef.current
.show()
.then(() => {
// user watch ad till the end or close it in interstitial format
onReward();
})
.catch((result) => {
// user get error during playing ad
onError?.(result);
});
} else {
onError?.({
error: true,
done: false,
state: 'load',
description: 'Adsgram script not loaded',
});
}
}, [onError, onReward]);
}import { useCallback, useEffect, useRef } from 'react';
/**
* Check Typescript section
* and use your path to adsgram types
*/
import type { AdController, ShowPromiseResult } from 'path-to-adsgram-types';
export interface useAdsgramParams {
blockId: string;
onReward?: () => void;
onError?: (result: ShowPromiseResult) => void;
}
export function useAdsgram({ blockId, onReward, onError }: useAdsgramParams): () => Promise<void> {
const AdControllerRef = useRef<AdController | undefined>(undefined);
useEffect(() => {
AdControllerRef.current = window.Adsgram?.init({ blockId });
}, [blockId]);
return useCallback(async () => {
if (AdControllerRef.current) {
return AdControllerRef.current
.show()
.then(() => {
// user watch ad till the end or close it in interstitial format
onReward?.();
})
.catch((result: ShowPromiseResult) => {
// user get error during playing ad
onError?.(result);
});
} else {
onError?.({
error: true,
done: false,
state: 'load',
description: 'Adsgram script not loaded',
});
}
}, [onError, onReward]);
}TIP
To test the integration, pass debug: true to init — you will get a test ad instead of a real one. Remove it or set it to false before releasing to production. See init parameters.
Usage example
Example of simple button component, when click on the button ad will be showed.
import { useCallback, ReactElement } from 'react'
import { useAdsgram } from "./hooks/useAdsgram.ts";
export function ShowAdButton() {
const onReward = useCallback(() => {
alert('Reward');
}, []);
const onError = useCallback((result) => {
alert(JSON.stringify(result, null, 4));
}, []);
/**
* insert your-block-id
*/
const showAd = useAdsgram({ blockId: "your-block-id", onReward, onError });
return (
<button onClick={showAd}>Show Ad</button>
)
}import { useCallback, ReactElement } from 'react'
import { useAdsgram } from "./hooks/useAdsgram.ts";
import { ShowPromiseResult } from "./types/adsgram";
export function ShowAdButton(): ReactElement {
const onReward = useCallback(() => {
alert('Reward');
}, []);
const onError = useCallback((result: ShowPromiseResult) => {
alert(JSON.stringify(result, null, 4));
}, []);
/**
* insert your-block-id
*/
const showAd = useAdsgram({ blockId: "your-block-id", onReward, onError });
return (
<button onClick={showAd}>Show Ad</button>
)
}Unity
We don't have plugins for unity.
Create a .jslib file: see Set up your JavaScript plug-in in the Unity Manual.
To call JS functions from Unity, see Call JavaScript functions from Unity C# scripts.
To call Unity functions from JS, see Call Unity C# script functions from JavaScript.
Vanilla js
Example with inline script and button to show ad.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test app</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<script src="https://sad.adsgram.ai/js/sad.min.js"></script>
</head>
<body>
<button id="ad">Show ad</button>
<script>
// insert your block id
const AdController = window.Adsgram.init({ blockId: "your-block-id" });
const button = document.getElementById('ad');
button.addEventListener('click', () => {
AdController.show().then((result) => {
// user watch ad till the end or close it in interstitial format
// your code to reward user for rewarded format
alert('Reward');
}).catch((result) => {
// user get error during playing ad
// do nothing or whatever you want
alert(JSON.stringify(result, null, 4));
})
})
</script>
</body>
</html>