Skip to content

कोड उदाहरण ​

  • Rewarded प्रारूप में, जिसका अर्थ है कि उपयोगकर्ता को विज्ञापन अंत तक देखने पर पुरस्कार देना चाहिए और विज्ञापन को एक बटन पर क्लिक करने के बाद उम्मीद की जानी चाहिए।
    अधिक जानकारी के लिए विज्ञापन आवश्यकताएँ देखें。
  • Interstitial प्रारूप में, जिसका अर्थ है कि उपयोगकर्ता अंत से पहले विज्ञापन छोड़ सकता है और विज्ञापन को ऐप में प्राकृतिक विराम के दौरान दिखाना चाहिए।
    अधिक जानकारी के लिए विज्ञापन आवश्यकताएँ देखें。

जानकारी

नीचे Rewarded प्रारूप के लिए कोड उदाहरण दिए गए हैं। ये उदाहरण Interstitial प्रारूप के लिए भी प्रासंगिक हैं। एकमात्र अंतर यह है कि AdController.show() प्रॉमिस उन विज्ञापनों के लिए भी resolve होता है जो पूरी तरह देखे गए हों और उन विज्ञापनों के लिए भी जो बंद किए गए हों। Interstitial का सरल कोड उदाहरण देखें।

React ​

स्क्रिप्ट जोड़ें ​

अपने HTML के <head> में यह स्क्रिप्ट जोड़ें।

useAdsgram हुक बनाएं ​

अपने प्रोजेक्ट में useAdsgram कस्टम हुक बनाएँ।
कस्टम हुक के बारे में अधिक जानकारी के लिए React documentation देखें।

javascript
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(() => {
          // उपयोगकर्ता विज्ञापन को अंत तक देखता है या interstitial प्रारूप में बंद करता है
          onReward();
        })
        .catch((result) => {
          // विज्ञापन चलाते समय error आया
          onError?.(result);
        });
    } else {
      onError?.({
        error: true,
        done: false,
        state: 'load',
        description: 'Adsgram script not loaded',
      });
    }
  }, [onError, onReward]);
}
typescript
import { useCallback, useEffect, useRef } from 'react';
/**
 * Typescript अनुभाग देखें
 * और 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(() => {
          // उपयोगकर्ता विज्ञापन को अंत तक देखता है या interstitial प्रारूप में बंद करता है
          onReward?.();
        })
        .catch((result: ShowPromiseResult) => {
          // विज्ञापन चलाते समय error आया
          onError?.(result);
        });
    } else {
      onError?.({
        error: true,
        done: false,
        state: 'load',
        description: 'Adsgram script not loaded',
      });
    }
  }, [onError, onReward]);
}

सुझाव

इंटीग्रेशन का परीक्षण करने के लिए init में debug: true पास करें — असली विज्ञापन के बजाय टेस्ट विज्ञापन मिलेगा। production में रिलीज़ करने से पहले इसे हटा दें या false पर सेट करें। देखें init पैरामीटर।

उपयोग का उदाहरण ​

सरल बटन कॉम्पोनेंट का उदाहरण, जब बटन पर क्लिक करें तो विज्ञापन दिखेगा।

jsx
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));
  }, []);

  /**
   * अपना blockId डालें
   */
  const showAd = useAdsgram({ blockId: "your-block-id", onReward, onError });

  return (
    <button onClick={showAd}>Show Ad</button>
  )
}
tsx
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));
  }, []);

  /**
   * अपना blockId डालें
   */
  const showAd = useAdsgram({ blockId: "your-block-id", onReward, onError });

  return (
    <button onClick={showAd}>Show Ad</button>
  )
}

Unity ​

हमारे पास Unity के लिए प्लगइन नहीं हैं।

.jslib फ़ाइल बनाएं: Unity मैनुअल में अपना JavaScript प्लग-इन सेट करें देखें।

Unity से JS फ़ंक्शंस कॉल करने के लिए Unity C# स्क्रिप्ट से JavaScript फ़ंक्शंस कॉल करना देखें।
JS से Unity फ़ंक्शंस कॉल करने के लिए JavaScript से Unity C# स्क्रिप्ट फ़ंक्शंस कॉल करना देखें।

Vanilla js ​

इनलाइन स्क्रिप्ट और विज्ञापन दिखाने के बटन के साथ उदाहरण।

html
<!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>
            // अपना blockId डालें
            const AdController = window.Adsgram.init({ blockId: "your-block-id" });
            const button = document.getElementById('ad');
            button.addEventListener('click', () => {
                AdController.show().then((result) => {
                    // उपयोगकर्ता विज्ञापन को अंत तक देखता है या interstitial प्रारूप में बंद करता है
                    // rewarded प्रारूप के लिए उपयोगकर्ता को reward देने का आपका कोड
                    alert('Reward');
                }).catch((result) => {
                    // विज्ञापन चलाते समय error आया
                    // कुछ न करें या जो आप चाहें
                    alert(JSON.stringify(result, null, 4));
                })
            })
        </script>
    </body>
</html>