Tauri Plugin Google AdMob
A comprehensive Tauri plugin for integrating Google AdMob advertisements into your mobile applications. This plugin supports all major ad formats including banner, interstitial, rewarded, rewarded interstitial, and app open ads.
Features
- 🎯 Banner Ads: Display banner advertisements with customizable positioning
- 📱 Interstitial Ads: Full-screen ads that appear at natural app transition points
- 🎁 Rewarded Ads: Users earn rewards for viewing video advertisements
- 🏆 Rewarded Interstitial Ads: Full-screen ads with reward mechanics
- 🚀 App Open Ads: Monetize app launch and resume events
- 🔄 Event System: Listen to ad lifecycle events (loaded, displayed, clicked, etc.)
- 🎨 Customizable: Configure ad sizes, positions, and behavior
- 🛡️ Type Safe: Full TypeScript support with comprehensive type definitions
Installation
Add the plugin to your Tauri application:
Rust (Cargo.toml)
[]
= "1.0.0"
JavaScript/TypeScript
# or
# or
Setup
1. Register the Plugin
In your Tauri application's src-tauri/src/main.rs:
use GoogleAdmobPlugin;
2. Add Permissions
Add the required permissions to your src-tauri/capabilities/default.json:
3. Android Configuration
Add the Google Mobile Ads SDK to your Android configuration:
android/build.gradle.kts:
composeOptions {
kotlinCompilerExtensionVersion = "2.1.0"
}
kotlinOptions {
jvmTarget = "1.8"
}
dependencies {
implementation("com.google.android.gms:play-services-ads:24.9.0")
}
android/src/main/AndroidManifest.xml:
<!-- Add these permissions -->
<!-- Add your AdMob App ID -->
Usage
Initialize AdMob
import { initialize } from 'tauri-plugin-google-admob';
// Initialize with your AdMob configuration
await initialize({
app_id: 'ca-app-pub-3940256099942544~3347511713', // Your AdMob App ID
test_device_ids: ['DEVICE_ID_1', 'DEVICE_ID_2'], // Optional test devices
is_debug: true, // Enable debug mode for development
tag_for_child_directed_treatment: null,
tag_for_under_age_of_consent: null
});
Banner Ads
import { showBanner, hideBanner } from 'tauri-plugin-google-admob';
// Show a banner ad
await showBanner({
ad_id: 'ca-app-pub-3940256099942544/6300978111', // Test banner ad unit ID
position: 'BOTTOM', // 'TOP' | 'BOTTOM'
size: 'BANNER' // 'BANNER' | 'LARGE_BANNER' | 'MEDIUM_RECTANGLE' | 'FULL_BANNER' | 'LEADERBOARD' | 'SMART_BANNER'
});
// Hide the banner
await hideBanner();
Interstitial Ads
import { prepareInterstitial, showInterstitial } from 'tauri-plugin-google-admob';
// Prepare the interstitial ad
await prepareInterstitial({
ad_id: 'ca-app-pub-3940256099942544/1033173712' // Test interstitial ad unit ID
});
// Show when ready
await showInterstitial();
Rewarded Ads
import { prepareRewarded, showRewarded } from 'tauri-plugin-google-admob';
// Prepare the rewarded ad
await prepareRewarded({
ad_id: 'ca-app-pub-3940256099942544/5224354917' // Test rewarded ad unit ID
});
// Show and handle rewards
await showRewarded();
Rewarded Interstitial Ads
import { prepareRewardedInterstitial, showRewardedInterstitial } from 'tauri-plugin-google-admob';
// Prepare the rewarded interstitial ad
await prepareRewardedInterstitial({
ad_id: 'ca-app-pub-3940256099942544/5354046379' // Test rewarded interstitial ad unit ID
});
// Show when ready
await showRewardedInterstitial();
App Open Ads
import { prepareAppOpen, showAppOpen } from 'tauri-plugin-google-admob';
// Prepare the app open ad
await prepareAppOpen({
ad_id: 'ca-app-pub-3940256099942544/9257395921', // Test app open ad unit ID
orientation: 'PORTRAIT' // 'PORTRAIT' | 'LANDSCAPE'
});
// Show when app becomes active
await showAppOpen();
Listening to Ad Events
import { listen } from '@tauri-apps/api/event';
// Listen to ad events
await listen('admob://banner_loaded', (event) => {
console.log('Banner ad loaded:', event.payload);
});
await listen('admob://banner_failed_to_load', (event) => {
console.log('Banner ad failed to load:', event.payload);
});
await listen('admob://interstitial_loaded', (event) => {
console.log('Interstitial ad loaded');
});
await listen('admob://rewarded_earned_reward', (event) => {
console.log('User earned reward:', event.payload);
});
API Reference
Types
// Configuration
export interface InitializeRequest {
app_id: string;
test_device_ids?: string[];
is_debug?: boolean;
tag_for_child_directed_treatment?: boolean | null;
tag_for_under_age_of_consent?: boolean | null;
}
// Banner Ad Options
export interface BannerAdOptions {
ad_id: string;
position: AdPosition;
size: BannerAdSize;
}
export type AdPosition = 'TOP' | 'BOTTOM';
export type BannerAdSize = 'BANNER' | 'LARGE_BANNER' | 'MEDIUM_RECTANGLE' | 'FULL_BANNER' | 'LEADERBOARD' | 'SMART_BANNER';
// Other Ad Options
export interface InterstitialAdOptions {
ad_id: string;
}
export interface RewardedAdOptions {
ad_id: string;
}
export interface RewardedInterstitialAdOptions {
ad_id: string;
}
export interface AppOpenAdOptions {
ad_id: string;
orientation: AdOrientation;
}
export type AdOrientation = 'PORTRAIT' | 'LANDSCAPE';
Commands
| Command | Parameters | Description |
|---|---|---|
initialize |
InitializeRequest |
Initialize AdMob with your app configuration |
showBanner |
BannerAdOptions |
Display a banner advertisement |
hideBanner |
- | Hide the currently displayed banner |
prepareInterstitial |
InterstitialAdOptions |
Load an interstitial ad for later display |
showInterstitial |
- | Show the prepared interstitial ad |
prepareRewarded |
RewardedAdOptions |
Load a rewarded ad for later display |
showRewarded |
- | Show the prepared rewarded ad |
prepareRewardedInterstitial |
RewardedInterstitialAdOptions |
Load a rewarded interstitial ad |
showRewardedInterstitial |
- | Show the prepared rewarded interstitial ad |
prepareAppOpen |
AppOpenAdOptions |
Load an app open ad for later display |
showAppOpen |
- | Show the prepared app open ad |
Events
The plugin emits various events during the ad lifecycle:
| Event | Description |
|---|---|
admob://banner_loaded |
Banner ad successfully loaded |
admob://banner_failed_to_load |
Banner ad failed to load |
admob://banner_opened |
Banner ad was clicked/opened |
admob://banner_closed |
Banner ad was closed |
admob://interstitial_loaded |
Interstitial ad successfully loaded |
admob://interstitial_failed_to_load |
Interstitial ad failed to load |
admob://interstitial_showed |
Interstitial ad was displayed |
admob://interstitial_failed_to_show |
Interstitial ad failed to show |
admob://interstitial_dismissed |
Interstitial ad was dismissed |
admob://rewarded_loaded |
Rewarded ad successfully loaded |
admob://rewarded_failed_to_load |
Rewarded ad failed to load |
admob://rewarded_showed |
Rewarded ad was displayed |
admob://rewarded_failed_to_show |
Rewarded ad failed to show |
admob://rewarded_dismissed |
Rewarded ad was dismissed |
admob://rewarded_earned_reward |
User earned a reward |
Test Ad Unit IDs
For testing purposes, use these Google-provided test ad unit IDs:
| Ad Format | Test Ad Unit ID |
|---|---|
| Banner | ca-app-pub-3940256099942544/6300978111 |
| Interstitial | ca-app-pub-3940256099942544/1033173712 |
| Rewarded | ca-app-pub-3940256099942544/5224354917 |
| Rewarded Interstitial | ca-app-pub-3940256099942544/5354046379 |
| App Open | ca-app-pub-3940256099942544/9257395921 |
Example Application
Check out the complete example in the examples/tauri-app directory for a full implementation showing all ad types and event handling.
Troubleshooting
Common Issues
1. Plugin commands not found
- Ensure you've added all required permissions to your capabilities file
- Verify the plugin is registered in your Rust main function
2. Ads not loading
- Check your internet connection
- Verify your AdMob App ID and ad unit IDs are correct
- Make sure you've added the required permissions to AndroidManifest.xml
- For production, ensure your app is approved by AdMob
3. Android build issues
- Ensure you have the correct Google Mobile Ads SDK version
- Check that your Android API level is compatible (minimum API 19)
4. TypeScript errors
- Make sure you've installed the plugin's npm package
- Import types from 'tauri-plugin-google-admob'
Debug Mode
Enable debug mode during development to get detailed logging:
await initialize({
app_id: 'your-app-id',
is_debug: true // Enable debug logging
});
Contributing
Contributions are welcome! Please read our contributing guidelines before submitting pull requests.
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
- Google Mobile Ads SDK
- Tauri - Build smaller, faster, and more secure desktop applications