Repository navigation
feat(ads): admob adapter #2023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat(ads): admob adapter #2023
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3bf92e1
feat(ads): admob adapter
peterporfy 555f4e3
chore: fastlane config
peterporfy f90f5e2
chore: cleanup versions and unnecessary changes
peterporfy 4e93e42
fix(ads): handle banner state differently than other units
peterporfy d72475e
fix: comment wording
peterporfy 5ff4935
chore: move the adsTester adapter changes to a stacked PR
peterporfy 850785c
fix: core package version
peterporfy 8428957
feat: lower minimum version
peterporfy 7c8b706
refactor: 1-2-3 switch order
peterporfy 9e4b5d0
fix: yarn lock update
peterporfy ee72efc
feat: add public api comments and usage examples
peterporfy e097df6
refactor: remove unnecessary public keywords
peterporfy e9452ad
refactor: better impressionid
peterporfy ed74ab0
fix: bump adapter to core 10.14.0.
peterporfy 8fed1ea
feat: add unverified reward earn tracking
peterporfy cb3a0dc
fix(ads): do not reuse a verification token when a rewarded ad is rel…
peterporfy 8e77ed2
refactor: internal capturemethod
peterporfy 1d2639c
fix(ads): per-platform error codes and single-use verification tokens
peterporfy 567b4fb
fix: type safe crypto
peterporfy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # OSX | ||
| # | ||
| .DS_Store | ||
|
|
||
| # XDE | ||
| .expo/ | ||
|
|
||
| # VSCode | ||
| .vscode/ | ||
| jsconfig.json | ||
|
|
||
| # Xcode | ||
| # | ||
| build/ | ||
| *.pbxuser | ||
| !default.pbxuser | ||
| *.mode1v3 | ||
| !default.mode1v3 | ||
| *.mode2v3 | ||
| !default.mode2v3 | ||
| *.perspectivev3 | ||
| !default.perspectivev3 | ||
| xcuserdata | ||
| *.xccheckout | ||
| *.moved-aside | ||
| DerivedData | ||
| *.hmap | ||
| *.ipa | ||
| *.xcuserstate | ||
| project.xcworkspace | ||
|
|
||
| # Android/IJ | ||
| # | ||
| .classpath | ||
| .cxx | ||
| .gradle | ||
| .idea | ||
| .project | ||
| .settings | ||
| local.properties | ||
| android.iml | ||
|
|
||
| # Cocoapods | ||
| # | ||
| example/ios/Pods | ||
|
|
||
| # Ruby | ||
| example/vendor/ | ||
|
|
||
| # node.js | ||
| # | ||
| node_modules/ | ||
| npm-debug.log | ||
| yarn-debug.log | ||
| yarn-error.log | ||
|
|
||
| # BUCK | ||
| buck-out/ | ||
| \.buckd/ | ||
| android/app/libs | ||
| android/keystores/debug.keystore | ||
|
|
||
| # Yarn | ||
| .yarn/* | ||
| !.yarn/patches | ||
| !.yarn/plugins | ||
| !.yarn/releases | ||
| !.yarn/sdks | ||
| !.yarn/versions | ||
|
|
||
| # Expo | ||
| .expo/ | ||
|
|
||
| # Turborepo | ||
| .turbo/ | ||
|
|
||
| # generated by bob | ||
| lib/ | ||
|
|
||
| dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 10.14.0 | ||
| ### ✨ New Features | ||
| * Initial release of `react-native-purchases-admob`: automatic ad tracking and rewarded-ad reward verification for `react-native-google-mobile-ads` 17+. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2023 RevenueCat | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <p align="center"> | ||
| <img src="https://uploads-ssl.webflow.com/5e2613cf294dc30503dcefb7/5e752025f8c3a31d56a51408_logo_red%20(1).svg" width="350" alt="RevenueCat"/> | ||
| <br> | ||
|
|
||
| [](https://www.npmjs.com/package/react-native-purchases-admob) | ||
|
|
||
| ## react-native-purchases-admob | ||
|
|
||
| _react-native-purchases-admob_ connects [react-native-google-mobile-ads](https://github.com/invertase/react-native-google-mobile-ads) to [RevenueCat](https://www.revenuecat.com/)'s ad tracking and reward verification. Create your ads through the adapter and it reports loaded, failed-to-load, displayed, opened, revenue and earned-reward events for you; rewarded ads can additionally have their rewards verified server-side. See the [AdMob integration docs](https://www.revenuecat.com/docs/ad-monetization/admob) for the full picture. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| yarn add react-native-purchases react-native-google-mobile-ads react-native-purchases-admob | ||
| ``` | ||
|
|
||
| Requires `react-native-google-mobile-ads` 17 or newer (the first version that exposes `responseInfo` and `valueMicros` on every ad format). Configure `Purchases` and initialize `mobileAds()` as you normally would; the adapter only tracks while `Purchases` is configured. | ||
|
|
||
| ## Tracking full-screen ads | ||
|
|
||
| Replace `InterstitialAd.createForAdRequest(...)` with the matching `RevenueCatAdMob.create...` call. The returned object is the same `InterstitialAd`, so `load()`, listeners and `destroy()` work as before; `placement` is a free-form label for where the ad is shown. | ||
|
|
||
| ```ts | ||
| import { RevenueCatAdMob } from "react-native-purchases-admob"; | ||
|
|
||
| const interstitial = RevenueCatAdMob.createInterstitialAd(adUnitId, { | ||
| placement: "level_complete", | ||
| requestOptions: { keywords: ["game"] }, | ||
| }); | ||
| interstitial.addAdEventListener(AdEventType.LOADED, () => setReady(true)); | ||
| interstitial.load(); | ||
|
|
||
| // Later. The placement given here overrides the load-time one for this impression. | ||
| await RevenueCatAdMob.showAndTrack(interstitial, { | ||
| placement: "level_complete_retry", | ||
| }); | ||
| ``` | ||
|
|
||
| `createAppOpenAd` works the same way. For an ad you created yourself, call `RevenueCatAdMob.trackAd(ad, { placement })` before `load()`. | ||
|
|
||
| ## Banner and native ads | ||
|
|
||
| ```tsx | ||
| import { useTrackingBannerAdProps } from "react-native-purchases-admob"; | ||
|
|
||
| function HomeBanner() { | ||
| const bannerProps = useTrackingBannerAdProps({ | ||
| unitId, | ||
| size: BannerAdSize.BANNER, | ||
| placement: "home_bottom", | ||
| onPaid: (paid) => console.log(paid.value), | ||
| }); | ||
| return <BannerAd {...bannerProps} />; | ||
| } | ||
|
|
||
| const nativeAd = await NativeAd.createForAdRequest(adUnitId); | ||
| RevenueCatAdMob.trackNativeAd(nativeAd, { placement: "feed" }); | ||
| ``` | ||
|
|
||
| Banners are a component, not an ad object, and their events arrive through props that React re-evaluates on every render, which is why they get a hook while every other format does not. Your own banner callbacks (`onAdLoaded`, `onPaid`, ...) are forwarded. Class components can use `RevenueCatAdMob.trackingBannerAdProps(...)` instead; it returns the same props but starts a new tracking state on every call, so call it once per banner instance (for example in the constructor), not inside `render`. | ||
|
|
||
| ## Reward verification | ||
|
|
||
| `react-native-google-mobile-ads` only accepts server-side verification options when the ad request is created, before AdMob's response id exists. The adapter therefore generates the impression id itself when you opt in, attaches the RevenueCat token to the request, and uses that id for every tracking event of the ad. | ||
|
|
||
| ```ts | ||
| const rewarded = await RevenueCatAdMob.createRewardedAd(adUnitId, { | ||
| placement: "extra_life", | ||
| enableRewardVerification: true, | ||
| }); | ||
| rewarded.load(); | ||
|
|
||
| await RevenueCatAdMob.showWithRewardVerification(rewarded, { | ||
| onRewardVerificationStarted: () => showSpinner(), | ||
| onRewardVerificationCompleted: (result) => { | ||
| if (result.failed) return showError(); | ||
| grant(result.reward); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Verification tokens are single-use, so create a new ad for every verified show rather than calling `load()` again on the same instance; after it has loaded, a second `load()` on a verified instance logs a warning and does nothing (a retry after a load error still works). `onRewardVerificationCompleted` is called at most once per show, after the user earns the reward; it is not called when the ad is dismissed or fails before a reward. The ad unit's server-side verification URL must point at RevenueCat and a reward must be configured for it in the RevenueCat dashboard; see the [rewards docs](https://www.revenuecat.com/docs/ad-monetization/rewards). | ||
|
|
||
| ## Not covered | ||
|
|
||
| The `useInterstitialAd` / `useRewardedAd` hooks create their ads internally and cannot be routed through the adapter; use the class API above. Ad pools and multi-format requests are not tracked. | ||
|
|
||
| ## Requirements | ||
|
|
||
| - iOS 15.0+ (ad tracking is unavailable below that; calls are ignored with a warning) | ||
| - `react-native-google-mobile-ads` 17.x (17.0.0 needs React Native 0.76+, 17.1.0+ needs 0.86+) and the New Architecture | ||
| - `react-native-purchases` 10.14.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.