# Working with advertising

## Identifiers from personal account

### Publisher identifier (publisher\_id)

To initialize the SDK 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%2F6ulGW1qReRgLVOSBUVa3%2Fimage.png?alt=media&#x26;token=25f0328b-c105-4904-88b8-7c6376dc932f" alt=""><figcaption></figcaption></figure>

### Ad unit identifier (placement\_name)

When using SDK 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%2FhqeyANcjLvwKT7OXuVz0%2Fimage.png?alt=media&#x26;token=9d83b988-6c62-412f-a0e0-6a634d4c7056" alt=""><figcaption></figcaption></figure>

## Test identifiers

During SDK 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.

```swift
Madex.showAd(adType: Madex.INTERSTITIAL, placementName: "placement_name", rootViewController: self)
```

### 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.

```swift
Madex.showAd(adType: Madex.REWARDED, placementName: "placement_name", rootViewController: self)
```

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.

```swift
Madex.showAd(adType: Madex.BANNER, placementName: "placement_name", rootViewController: self)
```

#### 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.

```swift
Madex.setBannerCustomSettings { settings in
    let _ = settings
        .updateRefreshIntervalSeconds(10)
}
```

**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 change this behavior in several ways.

{% tabs %}
{% tab title="With BannerSettings" %}
Define the banner’s position using `BannerSettings`.

```swift
Madex.setBannerCustomSettings { settings in
    let _ = settings
        .updateBannerPosition(BannerPosition.BOTTOM_CENTER)
}
```

{% endtab %}

{% tab title="In runtime" %}
After the banner loads, add the following code:

```swift
if let bannerView = Madex.getBannerView(placementName) {
    let position = BannerPosition.BOTTOM_CENTER
    
    if let view = self.view {
        bannerView.setAnchor(BannerPosition.toAnchor(position))
        bannerView.attach(to: view)
    }
}
```

{% endtab %}
{% endtabs %}

#### Show close button

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

```swift
Madex.setBannerCustomSettings { settings in
    let _ = settings
        .updateShowCloseButton(true)
}
```

## SDK Methods

Working with ads involves working with Madex SDK methods.

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

```swift
Madex.initialize(publisherID: "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.

```swift
// Load Interstitial ad
Madex.loadAd(Madex.INTERSTITIAL, "placement_name")
```

```swift
// Load Rewarded video ad
Madex.loadAd(Madex.REWARDED, "placement_name")
```

```swift
// Load banner ad
Madex.loadAd(Madex.BANNER, "placement_name")
```

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

```swift
Madex.isAdLoaded(Madex.INTERSTITIAL, "placement_name")
```

```swift
Madex.isAdLoaded(Madex.REWARDED, "placement_name")
```

```swift
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.

```swift
// Check the loading status and subsequently show the Interstitial ad
if (Madex.isAdLoaded(Madex.INTERSTITIAL, "placement_name")) {    
    Madex.showAd(adType: Madex.INTERSTITIAL, placementName: "placement_name", rootViewController: self)
}
```

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

```swift
Madex.showAd(adType: Madex.INTERSTITIAL, placementName: "placement_name", rootViewController: self)
```

```swift
Madex.showAd(adType: Madex.REWARDED, placementName: "placement_name", rootViewController: self)
```

```swift
Madex.showAd(adType: Madex.BANNER, placementName: "placement_name", rootViewController: self)
```

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.

```swift
// 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](https://madex.gitbook.io/madex-documentation/ios-sdk/user-consent-screen).

## 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 SDK 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. Create an extension for `ViewController` and inherit it from the interface for event tracking.

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

```swift
extension YourViewController: InterstitialDelegate {
    
    func onInterstitialLoaded(_ adPayload:AdPayload) {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onInterstitialLoadFailed(_ adPayload:AdPayload, _ error: AdException) {
        // 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
    }

    func onInterstitialShown(_ adPayload:AdPayload) {
        //  Called when the ad is displayed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onInterstitialShowFailed(_ adPayload:AdPayload, _ error: AdException) {
         // 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
    }

    func onInterstitialClosed(_ adPayload:AdPayload) {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}

{% tab title="Reworded Video" %}

```swift
extension YourViewController: RewardedDelegate {
    
    func onRewardedLoaded(_ adPayload:AdPayload) {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onRewardedLoadFailed(_ adPayload:AdPayload, _ error: AdException) {
        // 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
    }

    func onRewardedShown(_ adPayload:AdPayload) {
        // Called when the ad is displayed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onRewardedShowFailed(_ adPayload:AdPayload, _ error: AdException) {
         // 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
    }

    func onRewardedClosed(_ adPayload:AdPayload) {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onRewardedFinished(_ adPayload:AdPayload) {
        // Called when the ad video is finished
        // Here you can reward the user
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    func onRewardedVideoStarted(_ adPayload:AdPayload) {
        // Called when a video ad starts playing
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    func onRewardedVideoCompleted(_ adPayload:AdPayload) {
        // Called when the advertising video has completed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    func onUserRewarded(_ adPayload:AdPayload) {
        // Here you can reward the user
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}

{% tab title="Banner" %}

```swift
extension YourViewController: RewardedDelegate {
    
    func onBannerLoaded(_ adPayload:AdPayload) {
        // Called when the ad is loaded
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onBannerLoadFailed(_ adPayload:AdPayload, _ error: AdException) {
        // 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
    }

    func onBannerShown(_ adPayload:AdPayload) {
        // Called when the ad is displayed
        // Using AdPayload adPayload, you can get detailed information about the request
    }

    func onBannerShowFailed(_ adPayload:AdPayload, _ error: AdException) {
         // 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
    }

    func onBannerClosed(_ adPayload:AdPayload) {
        // Called when the ad is closed
        // Using AdPayload adPayload, you can get detailed information about the request
    }
    
    func onBannerImpression(_ adPayload:AdPayload) {
        // Recording the banner impression
        // Using AdPayload adPayload, you can get detailed information about the request
    }
}
```

{% endtab %}
{% endtabs %}

2. Associate your event tracking `ViewController` with the Madex SDK.\
   \
   Usually, the `ViewController` that works with ads is also the class for event tracking, so you can specify `self` as the delegate property.

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

```swift
Madex.setInterstitialDelegate(self)
```

{% endtab %}

{% tab title="Reworded Video" %}

```swift
Madex.setRewardedDelegate(self)
```

{% endtab %}

{% tab title="Banner" %}

```swift
Madex.setBannerDelegate(self)
```

{% endtab %}
{% endtabs %}

<br>
