-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvertsWrapper.as
More file actions
432 lines (380 loc) · 17.6 KB
/
AdvertsWrapper.as
File metadata and controls
432 lines (380 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package
{
import com.distriqt.extension.adverts.AdapterStatus;
import com.distriqt.extension.adverts.InterstitialAd;
import com.distriqt.extension.adverts.builders.AdRequestBuilder;
import com.distriqt.extension.adverts.rewarded.RewardedVideoAd;
import com.distriqt.extension.adverts.events.AdvertsEvent;
import com.distriqt.extension.adverts.events.InterstitialAdEvent;
import com.distriqt.extension.adverts.events.RewardedVideoAdEvent;
import com.distriqt.extension.adverts.events.FullScreenContentEvent;
import com.distriqt.extension.adverts.Adverts;
import com.distriqt.extension.adverts.AdvertPlatform;
import com.distriqt.extension.adverts.ump.ConsentInformation;
import com.distriqt.extension.adverts.ump.ConsentStatus;
import com.distriqt.extension.adverts.ump.ConsentRequestParameters;
import com.distriqt.extension.adverts.ump.events.ConsentInformationEvent;
import com.distriqt.extension.adverts.ump.events.UserMessagingPlatformEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import com.junkbyte.console.Cc;// we recommend to use add junkbyte console for logging
/**
* This is a wrapper for the basic functions of distiqt's Adverts
* So that you can just add AdvertsWrapper.as into your project
*
* You can initialize in 2 ways: after your own "GDPR gate" or with Google's UMP form
* Show Interstitial ads or Rewarded Videos
* Automatically preload them
* Define if the ads are not available due to the network connection or because of the player's selection in the Google's UMP form
*
* @author Olexiy Izvalov
*/
public class AdvertsWrapper
{
static public var self:AdvertsWrapper;
//These are the codes of Admob test ads. Don't forget to change the ones you're using to the real ad codes from your account when you finish testing
private var adIdBanner:String = "ca-app-pub-3940256099942544/2934735716";
private var adIdInterstitial:String = "ca-app-pub-7541048939453624/7379575655";//Interstitial
private var adIdInterstitialVideo:String = "ca-app-pub-3940256099942544/5135589807";
private var adIdRewardedVideo:String = "ca-app-pub-7541048939453624/2686610038";//Rewarded video
private var adIdNativeAdvanced:String = "ca-app-pub-3940256099942544/3986624511";
private var adIdNativeAdvancedVideo:String = "ca-app-pub-3940256099942544/2521693316";
private var adIdAppOpenAd:String = "ca-app-pub-3940256099942544/5662855259";
//ad units for interstitial and rewarded video ads. It was enough for me :)
private var interstitialAdUnit:InterstitialAd;
private var rewardedVideoAdUnit:RewardedVideoAd;
//callback function which grants rewards for the player
private var onRewardGrantedFunction:Function;//function (rewardType:String, rewardValue:int)
//this timer tries to load ads again after a previous load error
private var timer2LoadAdsAfterError:Timer;
//flags which define which ads should be loaded from the timer
private var mustLoadInterstitialFromTimer:Boolean = false;
private var mustLoadRewardedVideoFromTimer:Boolean = false;
private var arePersonalizedAdsAllowed:Boolean = true;
private var wasUMPCalled:Boolean = false;//did you call Google's UMP consent form
private var lastErrorCode:int =-1;//this is needed to detect the reasons why are the ads not available.
//There are 2 reasons: network connection
//or Google's UMP setting.
//According to developers, Google's UMP solution might prevent serving ads to players at all
//so in combination with wasUMPCalled the wrapper might give you information if UMP could be a reason
//Creation of a wrapper
public function AdvertsWrapper()
{
self = this;
if (Adverts.isSupported){
log("\nCALLING Adverts.service.setup")
Adverts.service.setup( AdvertPlatform.PLATFORM_ADMOB );
timer2LoadAdsAfterError = new Timer(10000, 0)
timer2LoadAdsAfterError.addEventListener(TimerEvent.TIMER, onUpdateAdsTimer);
timer2LoadAdsAfterError.start();
Adverts.service.addEventListener( AdvertsEvent.INITIALISED, initialisedHandler );
}else{
log("Adverts NOT supported")
}
}
//There are 2 ways to initialie the ads wrapper to fulfil GDPR
//1. You may integrate a custom "GDPR" gate, gather player's consent to show personalized or
//nonpersonalized ads, and initialize Adverts after that
//More on that: https://github.com/distriqt/ANE-Adverts/discussions/397
//In this case you should use this function
public function initializeAfterOwnPrivacyGate(mayShowPersonalizedAds:Boolean=true):void{
wasUMPCalled = false;
mustLoadInterstitialFromTimer = false;
mustLoadRewardedVideoFromTimer = false;
arePersonalizedAdsAllowed = mayShowPersonalizedAds;
initializeAdverts();
}
//2. Or you might want to use Google's UMP platform
//In this case you should use this function
//WARNING: the current Google's UMP provides a player to switch the ads off at all
//More on that: https://github.com/distriqt/ANE-Adverts/discussions/401
public function initializeAfterGooglesUMPForm():void{
log("\nCALLING initializeAfterGooglesUMPForm")
wasUMPCalled = false;
mustLoadInterstitialFromTimer = false;
mustLoadRewardedVideoFromTimer = false;
if (Adverts.service.ump.isSupported){
log("\nCALLING UMP getConsentInformation")
var consentInformation:ConsentInformation = Adverts.service.ump.getConsentInformation();
log(consentInformation);
log("getConsentStatus=",consentInformation.getConsentStatus());
log("getConsentType=",consentInformation.getConsentType());
consentInformation.addEventListener( ConsentInformationEvent.CONSENT_INFO_UPDATE_SUCCESS, updateConsentSuccessHandler );
consentInformation.addEventListener( ConsentInformationEvent.CONSENT_INFO_UPDATE_FAILURE, updateConsentFailureHandler );
Adverts.service.ump.addEventListener( UserMessagingPlatformEvent.CONSENT_FORM_LOAD_FAILURE, formLoadFailure );
Adverts.service.ump.addEventListener( UserMessagingPlatformEvent.CONSENT_FORM_LOAD_SUCCESS, formLoadSuccess );
Adverts.service.ump.addEventListener( UserMessagingPlatformEvent.CONSENT_FORM_DISMISSED, formDismissedHandler );
var params:ConsentRequestParameters = new ConsentRequestParameters()
//params.setConsentDebugSettings(new ConsentDebugSettings().setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA).addTestDeviceHashedId())
log("\nCALLING requestConsentInfoUpdate")
consentInformation.requestConsentInfoUpdate( params );
}else{
log("Adverts ump NOT Supported")
log("\nCALLING initializeAdverts (ump NOT Supported)")
initializeAdverts();
}
}
//If you have initialized Adverts from Google UMP Form and the ads are not shown because of the player's selection
//Then you can ask the player to reinitialize and select different option in the form
public function reInitGoogleUMPForm():void{
lastErrorCode =-1;
log("\nCALLING reInitGoogleUMPForm")
if (Adverts.service.ump.isSupported){
var consentInformation:ConsentInformation = Adverts.service.ump.getConsentInformation();
consentInformation.reset();
initializeAfterGooglesUMPForm();
}else{
log("Adverts ump NOT Supported")
}
}
//============================CONSENT CALLBACKS===================================================
private function updateConsentSuccessHandler(e:ConsentInformationEvent):void
{
log("- updateConsentSuccessHandler")
var consentInformation:ConsentInformation = Adverts.service.ump.getConsentInformation();
var sts:int = consentInformation.getConsentStatus();
log("ConsentStatus:", sts);
log("ConsentStatus.REQUIRED=", ConsentStatus.REQUIRED,"ConsentStatus.NOT_REQUIRED=", ConsentStatus.NOT_REQUIRED,"ConsentStatus.OBTAINED=", ConsentStatus.OBTAINED,"ConsentStatus.UNKNOWN=", ConsentStatus.UNKNOWN);
if (sts == ConsentStatus.REQUIRED)
{
if (consentInformation.isConsentFormAvailable())
{
log("\nCALLING loadConsentForm")
Adverts.service.ump.loadConsentForm();
}else{
log("\nCALLING Adverts.service.initialise (form not available)")
Adverts.service.initialise();
}
}else{
if (sts == ConsentStatus.OBTAINED){
wasUMPCalled = true;
}
log("\nCALLING initializeAdverts (consent not required or obtained)")
initializeAdverts();
}
}
private function updateConsentFailureHandler(e:ConsentInformationEvent):void
{
log("- updateConsentFailureHandler")
log( "ERROR: [" + e.error.errorID + "] " + e.error.message );
log("\nCALLING initializeAdverts (updateConsentFailure)")
initializeAdverts();
}
private function formLoadFailure(e:UserMessagingPlatformEvent):void
{
log("- formLoadFailure", e.error.errorID, e.error.name)
log("\nCALLING initializeAdverts (formLoadFailure)")
initializeAdverts();
}
private function formLoadSuccess(e:UserMessagingPlatformEvent):void
{
log("- formLoadSuccess")
log("\nCALLING showConsentForm")
wasUMPCalled = true;
Adverts.service.ump.showConsentForm();
}
private function formDismissedHandler(e:UserMessagingPlatformEvent):void
{
log("- formDismissedHandler")
log("\nCALLING initializeAdverts (form dismissed)")
initializeAdverts();
}
//============================INITZILIZATION===================================================
//The reason why it is private is because you should call either 1) initializeAfterOwnPrivacyGate or 2) initializeAfterGooglesUMPForm functions
private function initializeAdverts():void{
if (Adverts.isSupported){
Adverts.service.initialise();
}
}
private function initialisedHandler(e:AdvertsEvent):void
{
log("- initialisedHandler Platform is now initialised and ready to load ad")
log(e)
for each (var adapterStatus:AdapterStatus in e.adapterStatus)
{
log( "adapter: " + adapterStatus.name + " : " + adapterStatus.state + " [" + adapterStatus.latency + "] - " + adapterStatus.description );
}
if (Adverts.service.interstitials.isSupported)
{
if (!interstitialAdUnit){
log("creating interstitial")
interstitialAdUnit = Adverts.service.interstitials.createInterstitialAd();
log("setAdUnitId interstitial")
interstitialAdUnit.setAdUnitId(adIdInterstitial);
interstitialAdUnit.addEventListener( InterstitialAdEvent.LOADED, loadedInterstitialHandler );
interstitialAdUnit.addEventListener( InterstitialAdEvent.ERROR, errorInterstitialHandler );
//interstitial.addEventListener( InterstitialAdEventCLOSED, closedHandler );
interstitialAdUnit.addEventListener( FullScreenContentEvent.SHOW, showInterstitialHandler );
interstitialAdUnit.addEventListener( FullScreenContentEvent.FAILED_TO_SHOW, failedToShowInterstitialHandler );
interstitialAdUnit.addEventListener( FullScreenContentEvent.DISMISSED, dismissedInterstitialHandler );
}
preloadInterstitialAd();
}else{
log("interstitial NOT supported")
}
if (Adverts.service.rewardedVideoAds.isSupported){
if (!rewardedVideoAdUnit){
log("creating rewardedVideo")
rewardedVideoAdUnit = Adverts.service.rewardedVideoAds.createRewardedVideoAd();
rewardedVideoAdUnit.setAdUnitId(adIdRewardedVideo);
rewardedVideoAdUnit.addEventListener( RewardedVideoAdEvent.LOADED, loadedRVHandler );
rewardedVideoAdUnit.addEventListener( RewardedVideoAdEvent.ERROR, errorLoadRVHandler );
rewardedVideoAdUnit.addEventListener( FullScreenContentEvent.SHOW, showRVHandler );
rewardedVideoAdUnit.addEventListener( FullScreenContentEvent.DISMISSED, dismissedRVHandler );
rewardedVideoAdUnit.addEventListener( FullScreenContentEvent.FAILED_TO_SHOW, failed2ShowRVHandler );
rewardedVideoAdUnit.addEventListener( RewardedVideoAdEvent.REWARD, rewardRVHandler );
}
preloadRewardedVideoAd();
}else{
log("rewardedVideoAds NOT supported")
}
}
//============================AD UNITS LOADING===================================================
//preloading ad units takes place either after this ads unit had been shown or on a timer (in case if error took place at the previous ad unit loading)
private function onUpdateAdsTimer(e:TimerEvent):void
{
log("\nCALLING onUpdateAdsTimer");
if (interstitialAdUnit){
if (mustLoadInterstitialFromTimer){
preloadInterstitialAd();
}
}
if (rewardedVideoAdUnit){
if (mustLoadRewardedVideoFromTimer){
preloadRewardedVideoAd();
}
}
}
private function preloadInterstitialAd():void{
log("\nCALLING preloadInterstitialAd")
if (interstitialAdUnit){
interstitialAdUnit.load( new AdRequestBuilder().nonPersonalisedAds(!arePersonalizedAdsAllowed).build() );
mustLoadInterstitialFromTimer = false;
}
}
private function preloadRewardedVideoAd():void{
log("\nCALLING preloadRewardedVideoAd")
if (rewardedVideoAdUnit){
rewardedVideoAdUnit.load( new AdRequestBuilder().nonPersonalisedAds(!arePersonalizedAdsAllowed).build() );
mustLoadRewardedVideoFromTimer = false;
}
}
private function loadedInterstitialHandler(e:InterstitialAdEvent):void
{
log("- loadedHandler interstitial loaded and ready to be displayed")
}
private function errorInterstitialHandler(e:InterstitialAdEvent):void
{
log("- errorHandler Load error occurred. The errorCode will contain more information", "Error", e.errorCode);
mustLoadInterstitialFromTimer = true;
}
private function showInterstitialHandler(e:FullScreenContentEvent):void
{
log("- showInterstitialHandler The interstitial has been opened and is now visible to the user ")
}
private function failedToShowInterstitialHandler(e:FullScreenContentEvent):void
{
log("- failedToShowInterstitialHandler The ad failed to be shown", e.errorCode, e.errorMessage)
mustLoadInterstitialFromTimer = true;
}
private function dismissedInterstitialHandler(e:FullScreenContentEvent):void
{
log("- dismissedInterstitialHandler Control has returned to your application")
// you should reactivate any paused / stopped parts of your application.
preloadInterstitialAd();
}
private function loadedRVHandler(e:RewardedVideoAdEvent):void
{
log("- loadedRVHandler rewarded video ad loaded and ready to be displayed")
lastErrorCode =-1;
}
private function errorLoadRVHandler(e:RewardedVideoAdEvent):void
{
log("- errorLoadRVHandler Load error occurred. The errorCode will contain more information", "Error", e.errorCode, e.errorMessage )
lastErrorCode = e.errorCode;
// (e.errorCode==1) means "ads not available" (possibly due to Google UMP setting)
// (e.errorCode==2) means "network conection error"
mustLoadRewardedVideoFromTimer = true;
}
private function showRVHandler(e:FullScreenContentEvent):void
{
log("- showRVHandler The rewarded video ad has been shown and is now visible to the user")
}
private function dismissedRVHandler(e:FullScreenContentEvent):void
{
log("- dismissedRVHandler")
// Control has returned to your application
// you should reactivate any paused / stopped parts of your application.
preloadRewardedVideoAd();
}
private function failed2ShowRVHandler(e:FullScreenContentEvent):void
{
log("- failed2ShowRVHandler", e.errorCode, e.errorMessage)
// failed2ShowRVHandler
mustLoadRewardedVideoFromTimer = true;
}
private function rewardRVHandler(e:RewardedVideoAdEvent):void
{
log("- rewardRVHandler")
// Here you should reward your user
if (onRewardGrantedFunction){
onRewardGrantedFunction(e.rewardType, e.rewardAmount)
onRewardGrantedFunction = null;
}
}
//============================FINDING OUT THE STATES OF AD UNITS===================================================
//and the reasons which might prevent thm from showing
public function isRewardedVideoReady():Boolean{
if (!Adverts.isSupported){
return false
}
if (rewardedVideoAdUnit){
var res:Boolean = rewardedVideoAdUnit.isLoaded();
return res;
}else{
return false
}
}
public function isInterstitialReady():Boolean{
if (!Adverts.isSupported){
return false
}
if (interstitialAdUnit){
var res:Boolean = interstitialAdUnit.isLoaded();
return res;
}else{
return false
}
}
public function isRewardedAdsUnavailableBecauseOfNetwork():Boolean{
return (lastErrorCode==2)
}
public function isRewardedAdsUnavailableBecauseOfGoogleUMP():Boolean{
return (lastErrorCode==1) && wasUMPCalled;
}
//============================SHOWING AD UNITS===================================================
public function showInterstitialAd():void{
log("\nCALLING showInterstitialAd");
if (isInterstitialReady()){
interstitialAdUnit.show();
}
}
//onGranted: function (String, int)
public function showRewardedAd(onGranted:Function):void{
log("\nCALLING showRewardedAd");
if (isRewardedVideoReady()){
onRewardGrantedFunction = onGranted;
rewardedVideoAdUnit.show();
}
}
//============================LOGGING===================================================
//This is a logging function
//I recommend using Junkbyte console to log on device
//https://www.reddit.com/r/as3/comments/lyg16d/junkbyte_console_very_useful_tool_for_tracking/
private function log(...strings):void
{
Cc.log(strings);
//trace(strings)
}
}
}