Skip to content

Commit

Permalink
docs: add native ads documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wjaykim committed Dec 27, 2024
1 parent aea10f0 commit 6b47b10
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[
["Getting Started", "/"],
["Displaying Ads", "/displaying-ads"],
["Native Ads", "/native-ads"],
["Common Reasons For Ads Not Showing", "/common-reasons-for-ads-not-showing"]
]
],
Expand Down
4 changes: 4 additions & 0 deletions docs/displaying-ads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,7 @@ function App() {
```
The `sizes` prop takes an array of [`BannerAdSize`](https://github.com/invertase/react-native-google-mobile-ads/blob/main/src/BannerAdSize.ts) types.
## Native Ads
Head over to the [Native Ads](/native-ads) documentation.
254 changes: 254 additions & 0 deletions docs/native-ads.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
# Native Ads

Native ads are ad assets that are presented to users through UI components that are native to the platform.
They're shown using the same types of views with which you're already building your layouts, and can be formatted to match your app's visual design.

When a native ad loads, your app receives an ad object that contains its assets, and the app—rather than the Google Mobile Ads SDK—is then responsible for displaying them.

Broadly speaking, there are two parts to successfully implementing native ads: Loading an ad using the SDK and then displaying the ad content in your app.

## Load ads

Native ads are loaded with `NativeAd.createForAdRequest` static method, which returns a promise that resolves with a `NativeAd` object.
The first argument of the method is the "Ad Unit ID".
For testing, we can use a Test ID, however for production the ID from the Google AdMob dashboard under "Ad units" should be used:

```tsx
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
```

### Native ad Options

Native ads have many advanced features that allow you to make additional customizations and make the best possible ad experience.

You can pass an optional `NativeAdRequestOptions` object as the second argument to `NativeAd.createForAdRequest` to customize the ad request:

```tsx
NativeAd.createForAdRequest(TestIds.NATIVE, {
aspectRatio: NativeMediaAspectRatio.LANDSCAPE,
});
```

#### Preferred media aspect ratio controls

Media Aspect Ratio Controls let you specify a preference for the aspect ratio of ad creatives.

<Info>
Use of this API does not guarantee that all ad creatives will meet the preference specified.
</Info>

Specify `requestOptions.aspectRatio` with a `NativeMediaAspectRatio` enum value when calling `NativeAd.createForAdRequest`.

- When unset, the returned ad can have any media aspect ratio.
- When set, you will be able to improve the user experience by specifying the preferred type of aspect ratio.

The following example instructs the SDK to prefer a return image or video with a specific aspect ratio.

```tsx
NativeAd.createForAdRequest(TestIds.NATIVE, {
aspectRatio: NativeMediaAspectRatio.LANDSCAPE,
});
```

#### AdChoices placements

The AdChoices position controls lets you choose which corner to render the AdChoices icon.

Specify `requestOptions.adChoicesPlacement` with a `NativeAdChoicesPlacement` enum value when calling `NativeAd.createForAdRequest`.

- If unset, the AdChoices icon position is set to the top right.
- If set, AdChoices is placed at the custom position as requested.

The following example demonstrates how to set a custom AdChoices image position.

```tsx
NativeAd.createForAdRequest(TestIds.NATIVE, {
adChoicesPlacement: NativeAdChoicesPlacement.TOP_LEFT,
});
```

#### Start mute behavior

The start muted behavior lets you disable or enable a video's starting audio.

Specify `requestOptions.startVideoMuted` with a boolean value when calling `NativeAd.createForAdRequest`.

- The start muted behavior is enabled by default.
- When disabled, your app requests the video should begin with audio.
- When enabled, your app requests that the video should begin with audio muted.

The following example shows how to start the video with un-muted audio.

```tsx
NativeAd.createForAdRequest(TestIds.NATIVE, {
startVideoMuted: false,
});
```

#### Other Ad Request Options
You can specify other request options to be sent whilst loading an advert, such as keywords & location.
Setting additional request options helps AdMob choose better tailored ads from the network.
View the [RequestOptions](https://github.com/invertase/react-native-google-mobile-ads/blob/main/src/types/RequestOptions.ts) source code to see the full range of options available.

## Display ads

When a native ad loads, your app is then responsible for displaying the ad(though it doesn't necessarily have to do so immediately).
To make displaying system-defined ad formats easier, the SDK offers some useful resources, as described below.

### NativeAdView component

For the NativeAd format, there is the corresponding `NativeAdView` component.
This component is a wrapper component that publishers should use as the root for the NativeAd.
A single `NativeAdView` corresponds to a single native ad.
Each view used to display that ad's assets should be placed within the view hierarchy of the `NativeAdView` component.
Pass the `NativeAd` object to the `nativeAd` prop of the `NativeAdView` component to register the ad view:

```tsx
const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
}, []);

return (
<NativeAdView nativeAd={nativeAd}>
// Components to display assets must be placed here
</NativeAdView>
);
};
```

### NativeAsset component

`NativeAsset` component, which should be placed within the view hierarchy of a `NativeAdView`, is used to register the view used for each individual asset.
Registering the views in this way allows the SDK to automatically handle tasks such as:

- Recording clicks.
- Recording impressions (when the first pixel is visible on the screen).
- Displaying the AdChoices overlay.

`NativeAsset` component is a headless component, which means the component does not have UI implementation.
Customize your UI components as needed and place them inside the `NativeAsset` component, setting the `assetType` prop to the asset type you are displaying:

```tsx
const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
}, []);

return (
<NativeAdView nativeAd={nativeAd}>
<NativeAsset assetType={NativeAssetType.HEADLINE}>
<Text>{nativeAd.headline}</Text>
</NativeAsset>
</NativeAdView>
);
};
```

### NativeMediaView component

Main image and video assets are displayed to users via `NativeMediaView`. It should be placed within the view hierarchy of a `NativeAdView`, as with any other asset view.

Unlike other asset views, the `NativeMediaView` automatically populates its content.

```tsx
const NativeComponent = () => {
return (
<NativeAdView nativeAd={nativeAd}>
<NativeMediaView />
// Other assets..
</NativeAdView>
);
};
```

#### Resize Mode

The `NativeMediaView` component respects `resizeMode` property when displaying images. Defaults to `cover`.

Supports the following values:
- `cover`: Scale the image uniformly (maintain the image's aspect ratio) so that:
- Both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding)
- At least one dimension of the scaled image will be equal to the corresponding dimension of the view (minus padding)
- `contain`: Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
- `stretch`: Scale width and height independently, This may change the aspect ratio of the src.

<Info>NativeMediaView's `aspectRatio` style property is set to `nativeAd.mediaContent.aspectRatio` by default.</Info>

```tsx
const NativeComponent = () => {
return (
<NativeAdView nativeAd={nativeAd}>
<NativeMediaView resizeMode={'contain'} style={{ aspectRatio: 1 }} />
// Other assets..
</NativeAdView>
);
};
```

### AdChoices overlay

An AdChoices overlay is added as an ad view by the SDK when a backfill ad is returned.
If your app uses native ads backfill, leave space in your [preferred corner](#adchoices-placements) of your native ad view for the automatically inserted AdChoices logo.
Also, it's important that the AdChoices overlay is seen, so choose background colors and images appropriately.
For more information on the overlay's appearance and function, refer to the [programmatic native ads implementation guidelines](https://support.google.com/admob/answer/6329638#adchoices).

### Ad attribution for programmatic native ads

When displaying programmatic native ads, you must display an ad attribution to denote that the view is an advertisement.
Learn more in [policy guidelines](https://support.google.com/admob/answer/6329638#ad-attribution).

### Code example

These are the steps for displaying a native ad:

1. Place the NativeAdView, register it by setting the nativeAd prop to the NativeAd object.
2. For each ad asset to be displayed:
a. Populate the asset view with the asset in the NativeAd object.
b. Register the asset view by wrapping it with the NativeAsset component.
3. Place the NativeMediaView inside your native ad layout to display media asset.

```tsx
const NativeComponent = () => {
const [nativeAd, setNativeAd] = useState<NativeAd>();

useEffect(() => {
NativeAd.createForAdRequest(TestIds.NATIVE)
.then(setNativeAd)
.catch(console.error);
}, []);

return (
// Wrap all the ad assets in the NativeAdView component, and register the view with the nativeAd prop
<NativeAdView nativeAd={nativeAd}>
// Display the icon asset with Image component, and use NativeAsset to register the view
{nativeAd.icon && (
<NativeAsset assetType={NativeAssetType.ICON}>
<Image source={{uri: nativeAd.icon.url}} width={24} height={24} />
</NativeAsset>
)}
// Display the headline asset with Text component, and use NativeAsset to register the view
<NativeAsset assetType={NativeAssetType.HEADLINE}>
<Text style={{fontSize: 18, fontWeight: 'bold'}}>
{nativeAd.headline}
</Text>
</NativeAsset>
// Always display an ad attribution to denote that the view is an advertisement
<Text>Sponsored</Text>
// Display the media asset
<NativeMediaView />
// Repeat the process for the other assets in the NativeAd.
</NativeAdView>
);
};
```

0 comments on commit 6b47b10

Please sign in to comment.