> For the complete documentation index, see [llms.txt](https://madex.gitbook.io/madex-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://madex.gitbook.io/madex-documentation/unity-plugin/working-with-advertising.md).

# Working with advertising

General principle of interaction with ad units.

## Identifiers from personal account

### Publisher identifier (publisher\_id)

To initialize the plugin you will need the publisher identifier (`publisher_id`)

Your `publisher_id` is located in the "Settings" section of your Madex personal account.

<figure><img src="https://626582791-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwiiOaJhSveT4OR9bjTvw%2Fuploads%2F9k1N0ZkEKxgXapZpWxQB%2Fimage.png?alt=media&amp;token=81e6eb10-6830-4e30-966f-4c118e680729" alt=""><figcaption></figcaption></figure>

### Ad unit identifier (placement\_name)

When using plugin methods, you will need the ad unit identifier (`placement_name`) of the ad unit.&#x20;

`placement_name` is the ID of the ad unit. Ad units are created in Madex personal account.

<figure><img src="https://626582791-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwiiOaJhSveT4OR9bjTvw%2Fuploads%2FDPAznuQjuyygzJolDeUy%2Fimage.png?alt=media&amp;token=4415fa8a-9719-43d1-b627-936a08f0e516" alt=""><figcaption></figcaption></figure>

## Test identifiers

During plugin integration, we recommend using special `publisher_id` and `placement_name` to get stable test ads.

`Publisher ID` - `d42994c6-2145-4269-9c2f-2adcf9d9703f`

#### International format

`Madex Interstitial ID` - `50f37030-162d-4e8c-a9c0-e078d8fbf2f7`

`IronSource Interstitial ID` - `82e3bba6-6add-4594-8bb7-f96fef5b252d`

#### Rewarded Video Format

`Madex Rewarded ID` - `dacd566d-8487-4bc6-8afc-486f625be870`

`IronSource Rewarded ID` - `42e716e1-91c0-47e0-9832-c36ada6fa836`

#### Banner Format&#x20;

`Madex Banner ID` - `901a609e-b9b2-4f82-b8b8-c899c5524867`

## Advertising Formats

The Madex advertising network supports 2 ad formats.

### Fullscreen Banner (Interstitial)

The ad is displayed on the entire screen and contains a banner, interactive banner, or video. Tapping on it leads to a transition to the link specified by the advertiser.

Users can close the banner after 3 seconds.

This format is typically used when launching the application or transitioning between application screens.

When working with this format, use the `Madex.Interstitial`parameter.

Here's an example method call to display this format of advertising.

```csharp
Madex.ShowAd(Madex.Interstitial, "placement_name");
```

### Rewarded Video

The ad is displayed on the entire screen and contains a video or interactive banner. Tapping on it leads to a transition to the link specified by the advertiser.

Users can stop viewing the ad, but in this case, they will see a dialog warning about the lack of a reward when closing the video before its completion.

When working with this format, use the `Madex.Rewarded` parameter.

Here's an example method call to display this format of advertising.

```csharp
Madex.ShowAd(Madex.Rewarded, "placement_name");
```

To determine whether to reward the user, use the `onRewardedFinished` event.&#x20;

### Banner 320x50

The ad is displayed at the bottom of the screen and contains an interactive banner. Tapping on it leads to a transition to the link specified by the advertiser.

When working with this format, use the `Madex.Banner` parameter.

Here's an example method call to display this format of advertising.

```csharp
Madex.showAd(Madex.Banner, "placement_name");
```

#### Auto-Refresh of Banner Ads

To increase your profits, we have developed a function for automatic content change in the banner.

To configure, specify the interval in seconds (minimum 10 seconds).

If the parameter is not filled in or equals zero, the content will not be updated.

```csharp
var settings = new BannerSettings()
    .SetRefreshIntervalSeconds(10);
Madex.SetBannerCustomSettings(settings);
```

**Banner Placement**

The SDK supports custom banner positioning on the screen.

* Bottom center — `BannerPosition.BOTTOM_CENTER`
* Bottom left — `BannerPosition.BOTTOM_LEFT`
* Bottom right — `BannerPosition.BOTTOM_RIGHT`
* Top center — `BannerPosition.TOP_CENTER`
* Top left — `BannerPosition.TOP_LEFT`
* Top right — `BannerPosition.TOP_RIGHT`

By default, the banner is positioned at the bottom center, but you can override this behavior.

```csharp
var settings = new BannerSettings()
    .SetBannerPosition(BannerPosition.BOTTOM_CENTER);
Madex.SetBannerCustomSettings(settings);
```

#### Show close button

By default, the close banner button is not shown, but you can enable it:

```csharp
var settings = new BannerSettings()
    .SetShowCloseButton(true);
Madex.SetBannerCustomSettings(settings);
```

## Plugin Methods

Working with ads involves working with Madex Plugin methods.

1. `initialize` (required): Initializes the plugin . It's sufficient to call this method once at the application launch or before the first call to the `loadAd` method.

```csharp
Madex.Initialize("publisher_id");
```

2. `loadAd` (required): Loads ads from the server. Usually, this method is called in advance so that the content is loaded by the time of the ad display. In this case, the user's waiting time for ad display will be minimal.

```csharp
// Load Interstitial ad
Madex.LoadAd(Madex.Interstitial, "placement_name");
```

```csharp
// Load Rewarded video ad
Madex.LoadAd(Madex.Rewarded, "placement_name");
```

```csharp
// Load Banner ad
Madex.LoadAd(Madex.Banner, "placement_name");
```

3. `isAdLoaded` (optional): Checks the ad unit loading status.

```csharp
Madex.IsAdLoaded(Madex.Interstitial, "placement_name");
```

```csharp
Madex.IsAdLoaded(Madex.Rewarded, "placement_name");
```

```csharp
Madex.IsAdLoaded(Madex.Banner, "placement_name");
```

The method returns `true` or `false`. If the answer is negative, attempting to show the ad will lead to nothing.

Although the use of this method is optional, we strongly recommend checking the ad loading status before attempting to display it.

```csharp
// Check the loading status and subsequently show the Interstitial ad
if (Madex.IsAdLoaded(Madex.Interstitial, "placement_name")) {    
    Madex.ShowAd(Madex.Interstitial, "placement_name");
}
```

4. `showAd` (required): Calls ad display using the player built into the plugin.

```csharp
Madex.ShowAd(Madex.Interstitial, "placement_name");
```

```csharp
Madex.ShowAd(Madex.Rewarded, "placement_name");
```

```csharp
Madex.ShowAd(Madex.Banner, "placement_name");
```

5. `destroyAd` (optional): Destroys the ad container. You can work with multiple ad units simultaneously, but we strongly recommend destroying unnecessary ones to avoid memory issues and other problems.

```csharp
// Destroy the Rewarded Video ad container
Madex.DestroyAd(Madex.Rewarded, "placement_name");
```

6. `setUserConsent` (optional): Intended for conveying the user's consent status for the collection, processing, and use of personal data. By default, it is set to `false`, meaning the user has not given consent to the use of their data for advertising and other purposes.\
   \
   We recommend using this method to improve ad monetization quality. You can obtain consent on your own or use our [Consent Manager](/madex-documentation/unity-plugin/user-consent-screen.md).

## Using Multiple Ad Units

You can work with multiple ad units asynchronously, meaning you can call methods with different `placement_names` and formats independently of each other. For example, you can call the `loadAd` method for several blocks at once and load them concurrently.

## Tracking Ad Events

Madex Plugin allows you to track events occurring with ads to use them for integrating ads into your application's logic.

To track events, you need to provide a class to work with.

Note that ad events work on any ad unit.

That is, if you call the loading of 2 ad units simultaneously, the loading event will work 2 times, once for each unit.

#### To track events, do the following:

1. Inherit the class that will handle ad events from the interface to track events.

{% tabs %}
{% tab title="Interstital" %}

```csharp
class YourClassName : IInterstitialAdListener {
    public void OnInterstitialLoaded(AdPayload adPayload)
    {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnInterstitialLoadFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad loading
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnInterstitialShown(AdPayload adPayload)
    {
       // Called when the ad is displayed
       // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnInterstitialShowFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad display
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnInterstitialClosed(AdPayload adPayload)
    {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}

{% tab title="Rewarded Video" %}

```csharp
class YourClassName : IRewaredAdListener {
    public void OnRewaredLoaded(AdPayload adPayload)
    {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnRewardedLoadFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad loading
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnRewaredShown(AdPayload adPayload)
    {
        // Called when the ad is displayed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnRewardedShowFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad display
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnRewaredClosed(AdPayload adPayload)
    {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnRewardedVideoStarted(AdPayload adPayload)
    {
        // Called when a video ad starts playing
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    public void OnRewardedVideoCompleted(AdPayload adPayload)
    {
        // Called when the advertising video has completed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    public void OnUserRewarded(AdPayload adPayload)
    {
        // Here you can reward the user
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}

{% tab title="Banner" %}

```csharp
class YourClassName : IBannerAdListener {
    public void OnBannerLoaded(AdPayload adPayload)
    {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnBannerLoadFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad loading
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnBannerShown(AdPayload adPayload)
    {
        // Called when the ad is displayed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    public void OnBannerShowFailed(AdPayload adPayload, AdException error)
    {
        // Called if there is an error during ad display
        // Using AdPayload adPayload, you can get detailed information about the request
        // You can obtain detailed error information using AdException error
    }
    
    public void OnBannerClosed(AdPayload adPayload)
    {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    public void OnBannerImpression(AdPayload adPayload)
    {
        // Recording the banner impression
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}
{% endtabs %}

2. Link your event tracking class to the Madex Plugin.\
   \
   Usually, the class that works with ads is also the class for tracking events, so you can specify `this` as the delegate property.

{% tabs %}
{% tab title="Interstital" %}

```csharp
Madex.SetInterstitialCallbacks(this);
```

{% endtab %}

{% tab title="Rewarded" %}

```csharp
Madex.SetRewardedCallbacks(this);
```

{% endtab %}

{% tab title="Banner" %}

```csharp
Madex.SetBannerCallbacks(this);
```

{% endtab %}
{% endtabs %}
