Integrating Interstitials

Go through the process of requesting and displaying an Interstitial to users in your app here. Before following this, you should have already completed Getting started with the Fyber Unity Plugin.

[Requesting an Interstitial](#requesting-an-interstitial)
[Handling Errors](#handling-errors)
[Displaying an Interstitial](#displaying-an-interstitial)
[Interstitial Result](#interstitial-result)
[Mediation](#mediation)
## Requesting an Interstitial Before displaying an `Interstitial` you'll have to request it. To do so: 1. Make sure you've initialized the SDK before this, as described in [Getting started with Fyber SDK](/content/current/unity/integration/getting-started/). 2. Set up a delegate to listen to `Interstitial` requests using this snippet: ```csharp void OnEnable() { // Ad availability FyberCallback.AdAvailable += OnAdAvailable; } void OnDisable() { // Ad availability FyberCallback.AdAvailable -= OnAdAvailable; } //[...] Ad interstitialAd; //[...] private void OnAdAvailable(Ad ad) { switch(ad.AdFormat) { case AdFormat.INTERSTITIAL: interstitialAd = ad; break; //handle other ad formats if needed } } ``` 3. Use the following code snippet to request an `Interstitial`: ```csharp InterstitialRequester.Create() // optional method chaining //.AddParameter("key", "value") //.AddParameters(dictionary) //.WithPlacementId(placementId) // you don't need to add a callback if you are using delegates //.WithCallback(requestCallback) // request the ad .Request(); ``` ### Handling Errors You won't always get a successful response. To handle these scenarios you'll have to set up these delegate methods: ```csharp void OnEnable() { FyberCallback.AdNotAvailable += OnAdNotAvailable; FyberCallback.RequestFail += OnRequestFail; } void Disable() { FyberCallback.AdNotAvailable -= OnAdNotAvailable; FyberCallback.RequestFail -= OnRequestFail; } private void OnAdNotAvailable(AdFormat adFormat) { switch(adFormat) { case AdFormat.INTERSTITIAL: interstitialAd = null; break; //handle other ad formats if needed } } private void OnRequestFail(RequestError error) { // process error UnityEngine.Debug.Log("OnRequestError: " + error.Description); } ``` `error` can have one of these values: | RequestError | Description | --- | ---- |DEVICE_NOT_SUPPORTED | Only devices running Android API level 10 and above are supported. |CONNECTION_ERROR | Internet connection error. |UNKNOWN_ERROR | An unknown error occurred. |SDK_NOT_STARTED | You need to start the SDK. |NULL_CONTEXT_REFERENCE | Context reference cannot be null. |SECURITY_TOKEN_NOT_PROVIDED | The security token was not provided when starting the SDK. |ERROR_REQUESTING_ADS | An error happened while trying to retrieve ads |UNABLE_TO_REQUEST_ADS | The SDK is unable to request right now because it is either already performing a request or showing an ad. ## Displaying an Interstitial Once you've received an `Interstitial` `Ad` you can display it like so: ```csharp Ad interstitialAd; //[...] public void ShowInterstitial() { if (interstitialAd != null) { interstitialAd.Start(); interstitialAd = null; } } ``` ### Interstitial Result You may want to be notified when the `Interstitial` is started and closed. To do so you'll have to add these extra delegates: ```csharp void OnEnable() { FyberCallback.AdStarted += OnAdStarted; FyberCallback.AdFinished += OnAdFinished; } void OnDisable() { FyberCallback.AdStarted -= OnAdStarted; FyberCallback.AdFinished -= OnAdFinished; } private void OnAdStarted(Ad ad) { // this is where you mute the sound and toggle buttons if necessary switch (ad.AdFormat) { case AdFormat.INTERSTITIAL: interstitialAd = null; break; //handle other ad formats if needed } } private void OnAdFinished(AdResult result) { switch (result.AdFormat) { case AdFormat.INTERSTITIAL: UnityEngine.Debug.Log("nterstitial closed with result: " + result.Status + "and message: " + result.Message); break; //handle other ad formats if needed } } ``` `result.Status` can have one of these values: | `AdStatus` | Description | | -------- | ----------- | | `AdStatus.OK` | The `Interstitial` was closed successfully | | `AdStatus.Error` | An error occurred while displaying the ad | if `result.Status` is equal to `AdStatus.OK` then `result.Message` can have one of these values: | value | Description | |-------------------------- |------------- | | `"ReasonUserClickedOnAd"` | The `Interstitial` was closed because the user clicked on the ad. | | `"ReasonUserClosedAd"` | The `Interstitial` was explicitly closed by the user. | | `"ReasonUnknown"` | The `Interstitial` was dismissed for an unknown reason. | Otherwise, `result.Message` will contain a message with the error description. Note that the reason why the `Interstitial` was dismissed may not be known, in particular if the underlying mediated SDK does not notify its client of whether an `Interstitial` ad was clicked or just dismissed by the user. ## Mediation Fyber `Interstitial` product supports mediation for all of our partner networks. To learn more about integrating mediated partners please read [Ad Network Mediation section](/content/current/unity/mediation).