# Native (Google)

The native ads are a kind of ad that can be shown using the same visual style like the rest of the app, which allows a natural and non-intrusive integration with the user interface.

Currently, Wortise provides the possibility of use the native ads from Google's ad platform ("*Native Advanced*"), in a simple and direct way.

## Integration

To request a native ad, it is needed to make an integration by code just as shown in the example below:

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

```kotlin
class MainActivity : Activity() {

    private var googleNativeAd: GoogleNativeAd? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        googleNativeAd = GoogleNativeAd(
                         this, "Wortise Ad Unit ID", nativeListener).also {
            it.load()
        }
    }
    
    override fun onDestroy() {
        ...
        googleNativeAd?.destroy();
    }
    
    private val nativeListener = object : GoogleNativeAd.Listener() {
            
        override fun onNativeClicked(ad: GoogleNativeAd) {
            // Invoked when the ad has been clicked
        }
        
        override fun onNativeFailedToLoad(ad: GoogleNativeAd, error: AdError) {
            // Invoked when the ad could not be loaded
            // (because of an error or no fill)
        }
        
        override fun onNativeImpression(ad: GoogleNativeAd) {
            // Invoked when the ad has generated an impression
        }
        
        override fun onNativeLoaded(ad: GoogleNativeAd, nativeAd: NativeAd) {
            // Invoked when the ad has been loaded
        }
        
        override fun onNativeRevenuePaid(ad: GoogleNativeAd,
                                         data: RevenueData) {
            // Invoked when the ad has generated revenue
        }
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
public class MainActivity extends Activity {

    private GoogleNativeAd mGoogleNativeAd;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        mGoogleNativeAd = new GoogleNativeAd(
                this, "Wortise Ad Unit ID", mNativeListener);
        mGoogleNativeAd.load();
    }
    
    @Override
    public void onDestroy() {
        ...
        mGoogleNativeAd.destroy();
    }
    
    private GoogleNativeAd.Listener mNativeListener 
            = new GoogleNativeAd.Listener() {
            
        @Override
        public void onNativeClicked(@NonNull GoogleNativeAd ad) {
            // Invoked when ad has been clicked
        }
        
        @Override
        public void onNativeFailedToLoad(@NonNull GoogleNativeAd ad,
                                         @NonNull AdError error) {
            // Invoked when the ad could not be loaded
            // (because of an error or no fill)
        }
        
        @Override
        public void onNativeImpression(@NonNull GoogleNativeAd ad) {
            // Invoked when the ad has generated an impression
        }
        
        @Override
        public void onNativeLoaded(@NonNull GoogleNativeAd ad,
                                   @NonNull NativeAd nativeAd) {
            // Invoked when the ad has been loaded
        }
        
        @Override
        public void onNativeRevenuePaid(@NonNull GoogleNativeAd ad,
                                        @NonNull RevenueData data) {
            // Invoked when the ad has generated revenue
        }
    }
}
```

{% endtab %}
{% endtabs %}

Once the native ad has been successfully loaded, obtaining as result a `NativeAd` instance, it is needed to complete the integration by following Google's documentation on this matter: <https://developers.google.com/ad-manager/mobile-ads-sdk/android/native/advanced?hl=en#display_a_nativead>

### Specify options

The `GoogleNativeAd` class includes a method called `withNativeAdOptions` which allows to specify custom options by using the `NativeAdOptions` class from Google. This method must be called before loading the ad.

Below you can check a brief usage example:

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

```kotlin
var adOptions = NativeAdOptions.Builder()
    // Options are set here
    .build();

googleNativeAd.withNativeAdOptions(adOptions)
```

{% endtab %}

{% tab title="Java" %}

```java
NativeAdOptions adOptions = new NativeAdOptions.Builder()
    // Options are set here
    .build();

mGoogleNativeAd.withNativeAdOptions(adOptions);
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wortise.com/en/android-sdk/sdk-integration/native-google.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
