Skip to content

Commit 062de7a

Browse files
committed
docs: clarify use of then and make gathering consent easier
1 parent e618741 commit 062de7a

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

docs/european-user-consent.mdx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,26 @@ The UMP SDK uses the consent status from the previous session.
198198
```js
199199
import mobileAds, { AdsConsent, AdsConsentStatus } from 'react-native-google-mobile-ads';
200200

201-
let isMobileAdsStartCalled = false;
202-
203-
// Request consent information and load/present a consent form if necessary.
204-
AdsConsent.gatherConsent()
205-
.then(({canRequestAds}) => {
206-
if (canRequestAds) {
207-
startGoogleMobileAdsSDK()
208-
}
209-
})
210-
.catch((error) => console.error('Consent gathering failed:', error))
211-
212-
// Check if you can initialize the Google Mobile Ads SDK in parallel
213-
// using consent obtained in the previous session.
214-
const {canRequestAds} = await AdsConsent.getConsentInfo()
215-
if (canRequestAds) {
216-
startGoogleMobileAdsSDK()
217-
}
201+
const isMobileAdsStartCalledRef = useRef(false);
202+
203+
useEffect(() => {
204+
// Request consent information and load/present a consent form if necessary.
205+
AdsConsent.gatherConsent()
206+
.then(startGoogleMobileAdsSDK)
207+
.catch((error) => console.error('Consent gathering failed:', error));
208+
209+
// This sample attempts to load ads using consent obtained in the previous session.
210+
// We intentionally use .then() chaining (instead of await) to ensure parallel execution.
211+
startGoogleMobileAdsSDK();
212+
}, []);
218213

219214
async function startGoogleMobileAdsSDK() {
220-
if (isMobileAdsStartCalled) return;
221-
isMobileAdsStartCalled = true;
215+
const {canRequestAds} = await AdsConsent.getConsentInfo();
216+
if (!canRequestAds || isMobileAdsStartCalledRef.current) {
217+
return;
218+
}
219+
220+
isMobileAdsStartCalledRef.current = true;
222221

223222
// (Optional, iOS) Handle Apple's App Tracking Transparency manually.
224223
const gdprApplies = await AdsConsent.getGdprApplies();

0 commit comments

Comments
 (0)