1 つのインタースティシャル広告を読み込む

インタースティシャル広告は、画面の切り替え時やゲームレベルの合間など、アプリの画面が切り替わるタイミングで表示されるフルスクリーン広告です。ユーザーは広告をタップしてリンク先に移動するか、広告を閉じてアプリに戻ることができます。詳しくは、インタースティシャル広告に関するガイドラインと事例紹介をご覧ください。

このガイドでは、React Native アプリにインタースティシャル広告を組み込む方法について説明します。

前提条件

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

広告を読み込む

インタースティシャル広告を読み込むには、InterstitialAd クラスの load メソッドを呼び出します。

次の例では、成功した場合はインタースティシャル広告を読み込み、失敗した場合はエラーをログに記録します。

export async function loadInterstitialAd(
  adUnitId: string,
): Promise<InterstitialAd | null> {
  try {
    const ad = await InterstitialAd.load({adUnitId});
    return ad;
  } catch (error) {
    const loadError = error as LoadAdError;
    console.error('Failed to load interstitial ad:', loadError.message);
    return null;
  }
}

広告イベントをリッスンする

広告を表示する前に、読み込まれた InterstitialAd インスタンスに adEventCallback リスナーを設定します。このリスナーは、広告が画面に表示されたときや、ユーザーが広告をタップしたときなど、広告のライフサイクル イベントのコールバックを受け取ります。使用可能なコールバックについては、AdEventCallback をご覧ください。

次の例では、広告イベントをリッスンするコールバックを設定しています。

export function registerAdEvents(ad: InterstitialAd) {
  ad.adEventCallback = {
    // Called when the ad is presented on screen.
    onAdShowedFullScreenContent: () => {
      console.log('Interstitial ad showed full screen content.');
    },
    // Called when the ad is closed by the user.
    onAdDismissedFullScreenContent: () => {
      console.log('Interstitial ad dismissed full screen content.');
      ad.destroy();
    },
    // Called when presentation fails.
    onAdFailedToShowFullScreenContent: (error) => {
      console.error('Failed to show interstitial ad:', error);
      ad.destroy();
    },
    // Called when an impression is recorded.
    onAdImpression: () => {
      console.log('Interstitial ad recorded an impression.');
    },
    // Called when the user taps the ad.
    onAdClicked: () => {
      console.log('Interstitial ad was clicked.');
    },
    // Called when the ad is estimated to have earned money.
    onAdPaid: (adValue) => {
      console.log(
        `Interstitial ad paid: ${adValue.amountMicros}` +
          ` ${adValue.currencyCode}`,
      );
    },
  };
}

この例では、ユーザーが広告を閉じたときや、広告の表示に失敗したときにも、destroy メソッドを呼び出しています。このプロセスにより、広告で使用されていたリソースが解放されます。

広告を表示する

インタースティシャル広告を表示するには、読み込まれた InterstitialAd インスタンスで show メソッドを呼び出します。

次の例は、インタースティシャル広告を表示しています。

export function showInterstitialAd(ad: InterstitialAd) {
  ad.show();
}

サンプル全体を表示する

このセクションでは、インタースティシャル広告を React Native アプリに統合する完全な例について説明します。

次の例では、広告を React Native コンポーネントに統合し、広告インスタンスを状態管理して、広告を読み込んで表示するコントロールを提供しています。

export function InterstitialSingleLoadScreen() {
  const [ad, setAd] = useState<InterstitialAd | null>(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [status, setStatus] = useState<string>('Idle');

  useEffect(() => {
    return () => {
      ad?.destroy();
    };
  }, [ad]);

  const handleLoad = async () => {
    setStatus('Loading Interstitial ad...');
    const loadedAd = await loadInterstitialAd(INTERSTITIAL_AD_UNIT_ID);
    if (loadedAd) {
      registerAdEvents(loadedAd);
      const onDismiss =
        loadedAd.adEventCallback?.onAdDismissedFullScreenContent;
      loadedAd.adEventCallback = {
        ...loadedAd.adEventCallback,
        onAdDismissedFullScreenContent: () => {
          onDismiss?.();
          setStatus('Ad dismissed.');
        },
      };
      setAd((prevAd) => {
        prevAd?.destroy();
        return loadedAd;
      });
      setIsLoaded(true);
      setStatus('Interstitial ad loaded!');
    } else {
      setStatus('Failed to load Interstitial ad.');
    }
  };

  const handleShow = () => {
    if (ad) {
      showInterstitialAd(ad);
      setIsLoaded(false);
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Interstitial Ad</Text>
      <Text style={styles.status}>Status: {status}</Text>
      <View style={styles.buttonGroup}>
        <Button title="Load Ad" onPress={handleLoad} />
        <View style={styles.spacerSmall} />
        <Button title="Show Ad" onPress={handleShow} disabled={!isLoaded} />
      </View>
    </View>
  );
}

次のステップ

インタースティシャル広告を統合したら、次のトピックをご覧ください。