Created
February 22, 2020 07:42
-
-
Save dinhkhanh/08f246b321502e1db8ab8d8cf856522c to your computer and use it in GitHub Desktop.
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
class AdsManager { | |
constructor() { | |
let supportedAPIs = FBInstant.getSupportedAPIs(); | |
this.canShowInterstitialAds = supportedAPIs.includes('getInterstitialAdAsync'); | |
this.canShowRewardedVideoAds = supportedAPIs.includes('getRewardedVideoAsync'); | |
if (!this.canShowInterstitialAds) { | |
Sentry.captureMessage(window.platform + ' can\'t show interstitital ads'); | |
} | |
if (!this.canShowRewardedVideoAds) { | |
Sentry.captureMessage(window.platform + ' can\'t show rewarded video ads'); | |
} | |
// for testing, ad disabled | |
// this.canShowInterstitialAds = false; | |
// this.canShowRewardedVideoAds = false; | |
this.preloadedInterstitial = null; | |
this.preloadedRewardedVideo = null; | |
} | |
preloadInterstitial(success, fail, final, retry = 0) { | |
if (!this.canShowInterstitialAds) { | |
return; | |
} | |
let self = this; | |
FBInstant.getInterstitialAdAsync(window.interstitialId) | |
.then(function (interstitial) { | |
// Load the Ad asynchronously | |
self.preloadedInterstitial = interstitial; | |
return self.preloadedInterstitial.loadAsync(); | |
}) | |
.then(function () { | |
FBInstant.logEvent('Interstitial Preloaded', 1); | |
}) | |
.catch(function (error) { | |
FBInstant.logEvent('Interstitial Ad Load Error', 1); | |
Sentry.captureException(error); | |
// You can try to reload after 30 seconds (2nd attempt) | |
// setTimeout(function () { self.handleAdsNoFill(self.preloadedInterstitial, 2); }, 5 * 1000); | |
}); | |
} | |
preloadRewarded(success, fail, final, retry = 0) { | |
if (!this.canShowRewardedVideoAds) { | |
return; | |
} | |
let self = this; | |
FBInstant.getRewardedVideoAsync(window.rewardedVideoId) | |
.then(function (rewarded) { | |
// Load the Ad asynchronously | |
self.preloadedRewardedVideo = rewarded; | |
return self.preloadedRewardedVideo.loadAsync(); | |
}) | |
.then(function () { | |
FBInstant.logEvent('Rewarded Video Preloaded', 1); | |
}) | |
.catch(function (error) { | |
FBInstant.logEvent('Rewarded Video Ad Load Error', 1); | |
Sentry.captureException(error); | |
// You can try to reload after 30 seconds (2nd attempt) | |
// setTimeout(function () { self.handleAdsNoFill(self.preloadedRewardedVideo, 2); }, 5 * 1000); | |
}); | |
} | |
handleAdsNoFill(adInstance, attemptNumber) { | |
let self = this; | |
if (attemptNumber > 3) { | |
// You can assume we will not have to serve in the current session, no need to try | |
// to load another ad. | |
FBInstant.logEvent('Too Many Retry Ad Preloaded', 1); | |
return; | |
} else { | |
adInstance.loadAsync().then(function () { | |
// This should get called if we finally have ads to serve. | |
FBInstant.logEvent('Ad No Fill Preloaded', 1); | |
}).catch(function (err) { | |
FBInstant.logEvent('Ad No Fill Load Error', 1); | |
// You can try to reload after 30 seconds | |
setTimeout(function () { | |
self.handleAdsNoFill(adInstance, attemptNumber + 1); | |
}, 5 * 1000); | |
}); | |
} | |
} | |
showInterstitial(timeout = 0, success, fail, final) { | |
if (!this.canShowInterstitialAds) { | |
typeof final === 'function' && final(); | |
return; | |
} | |
let self = this; | |
document.querySelector('.interstitial-notice').style.display = 'block'; | |
setTimeout(function () { | |
self.preloadedInterstitial.showAsync() | |
.then(function () { | |
// User has watched the ad. | |
// Perform post-ad success operation | |
//.. | |
FBInstant.logEvent('Interstitial Ad Watched', 1); | |
typeof success === 'function' && success(); | |
}) | |
.catch(function (error) { | |
FBInstant.logEvent('Interstitial Ad Show Error', 1); | |
Sentry.captureException(error); | |
typeof fail === 'function' && fail(); | |
}).finally(function () { | |
document.querySelector('.interstitial-notice').style.display = 'none'; | |
self.preloadInterstitial(); | |
typeof final === 'function' && final(); | |
}); | |
}, timeout); | |
} | |
showRewarded(success, fail, final) { | |
if (!this.canShowRewardedVideoAds) { | |
typeof final === 'function' && final(); | |
return; | |
} | |
let self = this; | |
self.preloadedRewardedVideo.showAsync() | |
.then(function () { | |
// User has watched the ad. | |
// Perform post-ad success operation | |
//.. | |
FBInstant.logEvent('Rewarded Video Ad Watched', 1); | |
typeof success === 'function' && success(); | |
}) | |
.catch(function (error) { | |
FBInstant.logEvent('Rewarded Video Ad Show Error', 1); | |
Sentry.captureException(error); | |
typeof fail === 'function' && fail(); | |
}).finally(function () { | |
self.preloadRewarded(); | |
typeof final === 'function' && final(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment