Skip to content

Commit

Permalink
feat: gather consent method to make user consent easier (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancom authored Nov 15, 2024
1 parent 1d181f1 commit 9a9e5e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
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

0 comments on commit 9a9e5e5

Please sign in to comment.