Skip to content
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

feat: gather consent method to make user consent easier #654

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions docs/european-user-consent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,30 +198,24 @@ import mobileAds, { AdsConsent, AdsConsentStatus } from 'react-native-google-mob

let isMobileAdsStartCalled = false;

// Request an update for the consent information.
AdsConsent.requestInfoUpdate().then(() => {

AdsConsent.loadAndShowConsentFormIfRequired().then(adsConsentInfo => {

// Consent has been gathered.
if (adsConsentInfo.canRequestAds) {
// Request consent information and load/present a consent form if necessary.
AdsConsent.gatherConsent()
.then(({canRequestAds}) => {
if (canRequestAds) {
startGoogleMobileAdsSDK()
}
})
})
.catch((error) => console.error('Consent gathering failed:', error))

// Check if you can initialize the Google Mobile Ads SDK in parallel
// while checking for new consent information. Consent obtained in
// the previous session can be used to request ads.
// So you can start loading ads as soon as possible after your app launches.
// Check if you can initialize the Google Mobile Ads SDK in parallel
// using consent obtained in the previous session.
const {canRequestAds} = await AdsConsent.getConsentInfo()
if (canRequestAds) {
startGoogleMobileAdsSDK()
}

async function startGoogleMobileAdsSDK() {
if (isMobileAdsStartCalled) return;

isMobileAdsStartCalled = true;

// (Optional, iOS) Handle Apple's App Tracking Transparency manually.
Expand Down
5 changes: 5 additions & 0 deletions src/AdsConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export const AdsConsent: AdsConsentInterface = {
return native.getConsentInfo();
},

async gatherConsent(options: AdsConsentInfoOptions = {}): Promise<AdsConsentInfo> {
await this.requestInfoUpdate(options);
return this.loadAndShowConsentFormIfRequired();
},

reset(): void {
return native.reset();
},
Expand Down
6 changes: 6 additions & 0 deletions src/types/AdsConsent.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export interface AdsConsentInterface {
*/
getConsentInfo(): Promise<AdsConsentInfo>;

/**
* Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary.
*/
gatherConsent(): Promise<AdsConsentInfo>;

/**
* Returns the value stored under the `IABTCF_TCString` key
* in NSUserDefaults (iOS) / SharedPreferences (Android) as
Expand Down
Loading