@rediads/react-native-ads-gam 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/LICENSE +201 -0
- package/README.md +230 -2
- package/android/build.gradle +54 -0
- package/android/consumer-rules.pro +7 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/rediads/reactnative/gam/RediadsGAMBannerSlot.kt +99 -0
- package/android/src/main/java/com/rediads/reactnative/gam/RediadsGAMModule.kt +306 -0
- package/android/src/main/java/com/rediads/reactnative/gam/RediadsGAMRenderedModule.kt +233 -0
- package/app.plugin.js +2 -0
- package/build/RediadsGAM.d.ts +46 -0
- package/build/RediadsGAM.js +66 -0
- package/build/RediadsGAMBanner.d.ts +26 -0
- package/build/RediadsGAMBanner.js +40 -0
- package/build/RediadsGAMBannerBiddingOnly.d.ts +23 -0
- package/build/RediadsGAMBannerBiddingOnly.js +39 -0
- package/build/RediadsGAMBiddingOnlyFullScreenAd.d.ts +59 -0
- package/build/RediadsGAMBiddingOnlyFullScreenAd.js +128 -0
- package/build/RediadsGAMFullScreenAd.d.ts +70 -0
- package/build/RediadsGAMFullScreenAd.js +140 -0
- package/build/index.d.ts +6 -0
- package/build/index.js +7 -0
- package/build/native.d.ts +45 -0
- package/build/native.js +18 -0
- package/build/renderedNative.d.ts +34 -0
- package/build/renderedNative.js +15 -0
- package/build/types.d.ts +77 -0
- package/build/types.js +1 -0
- package/expo-module.config.json +18 -0
- package/ios/RediadsAdsGAM.podspec +34 -0
- package/ios/RediadsGAMModule.swift +355 -0
- package/ios/RediadsGAMRenderedModule.swift +208 -0
- package/package.json +57 -4
- package/plugin/withRediadsGAM.js +161 -0
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { RediadsAdEvent, RediadsAdFormat, RediadsReward } from '@rediads/react-native-ads';
|
|
2
|
+
/**
|
|
3
|
+
* The GAM targeting keys of a Rediads bid: `hb_pb` (price bucket), `hb_bidder`, `hb_format`,
|
|
4
|
+
* `hb_cache_id`, `hb_cache_host`, `hb_cache_path`, `hb_env`, `hb_size`, `hb_uuid` (video), and
|
|
5
|
+
* bidder-specific copies. Empty without a bid.
|
|
6
|
+
*/
|
|
7
|
+
export type RediadsGAMTargeting = Record<string, string>;
|
|
8
|
+
/** Sizes of GAM banners that take Rediads bids: 320x50 and 300x250 (video needs 300x250). */
|
|
9
|
+
export type RediadsGAMBannerSize = '320x50' | '300x250';
|
|
10
|
+
/** What a bid is for, as `fetchTargeting` takes it. */
|
|
11
|
+
export type RediadsGAMTargetingRequest = {
|
|
12
|
+
type: 'banner';
|
|
13
|
+
/** The slot name, sent as gpid: the slot in Rediads analytics. */
|
|
14
|
+
placement: string;
|
|
15
|
+
/** Defaults to 300x250. */
|
|
16
|
+
size?: RediadsGAMBannerSize;
|
|
17
|
+
/** Defaults to display. */
|
|
18
|
+
format?: RediadsAdFormat;
|
|
19
|
+
} | {
|
|
20
|
+
type: 'interstitial';
|
|
21
|
+
placement: string;
|
|
22
|
+
format?: RediadsAdFormat;
|
|
23
|
+
}
|
|
24
|
+
/** Rewarded ads are video. */
|
|
25
|
+
| {
|
|
26
|
+
type: 'rewarded';
|
|
27
|
+
placement: string;
|
|
28
|
+
};
|
|
29
|
+
export interface RediadsGAMTargetingResult {
|
|
30
|
+
/** The keys to add to the GAM request's custom targeting; empty without a bid. */
|
|
31
|
+
targeting: RediadsGAMTargeting;
|
|
32
|
+
/** Why there is no bid (no fill, timeout, SDK not started...), or null. */
|
|
33
|
+
error: string | null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* What happened to a bidding-only GAM ad (RediadsGAM*BiddingOnly).
|
|
37
|
+
*
|
|
38
|
+
* - `bid`: the Rediads auction finished, just before GAM loads. `targeting` has the keys added to the
|
|
39
|
+
* GAM request; `error` says why there are none. GAM loads anyway, with the publisher's other demand.
|
|
40
|
+
* - `loaded`: GAM has an ad. `lineItemId` and `creativeId` say what GAM served (a Rediads line item
|
|
41
|
+
* when ours won).
|
|
42
|
+
* - `failed`: GAM had no ad, or couldn't show it. `error` is Google's message.
|
|
43
|
+
* - `impression`, `clicked`: as Google records them.
|
|
44
|
+
* - `presented`, `dismissed`: a full-screen ad went on and off screen; for a banner, what a click
|
|
45
|
+
* opened over the app.
|
|
46
|
+
* - `rewarded`: the user earned the GAM ad unit's reward (`reward`).
|
|
47
|
+
*/
|
|
48
|
+
export type RediadsGAMBiddingOnlyEventType = 'bid' | 'loaded' | 'failed' | 'impression' | 'clicked' | 'presented' | 'dismissed' | 'rewarded';
|
|
49
|
+
export interface RediadsGAMBiddingOnlyEvent {
|
|
50
|
+
type: RediadsGAMBiddingOnlyEventType;
|
|
51
|
+
/** The slot name the ad was created with. */
|
|
52
|
+
placement: string;
|
|
53
|
+
error?: string;
|
|
54
|
+
/** `bid`: the Rediads keys added to the GAM request. */
|
|
55
|
+
targeting?: RediadsGAMTargeting;
|
|
56
|
+
/** `loaded`: GAM's line item and creative. */
|
|
57
|
+
lineItemId?: string;
|
|
58
|
+
creativeId?: string;
|
|
59
|
+
/** `rewarded`: the GAM ad unit's reward. */
|
|
60
|
+
reward?: RediadsReward;
|
|
61
|
+
}
|
|
62
|
+
export type RediadsGAMBiddingOnlyListener = (event: RediadsGAMBiddingOnlyEvent) => void;
|
|
63
|
+
/**
|
|
64
|
+
* SDK-rendered GAM: who drew the ad. `rediads`: a Rediads line item won and the Rediads SDK draws our
|
|
65
|
+
* ad (with Rediads trackers); `gam`: GAM's own demand won and Google's view shows it.
|
|
66
|
+
*/
|
|
67
|
+
export type RediadsGAMRenderer = 'rediads' | 'gam';
|
|
68
|
+
/**
|
|
69
|
+
* An event of an SDK-rendered GAM ad (RediadsGAMBanner, RediadsGAMInterstitial, RediadsGAMRewarded):
|
|
70
|
+
* the same events as the main package's ads, whichever source won. `loaded` carries `renderer`.
|
|
71
|
+
*/
|
|
72
|
+
export interface RediadsGAMEvent extends RediadsAdEvent {
|
|
73
|
+
/** `loaded`: who will show (full screen) or shows (banner) the ad. */
|
|
74
|
+
renderer?: RediadsGAMRenderer;
|
|
75
|
+
}
|
|
76
|
+
export type RediadsGAMListener = (event: RediadsGAMEvent) => void;
|
|
77
|
+
export type { RediadsAdFormat, RediadsReward };
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"platforms": [
|
|
3
|
+
"apple",
|
|
4
|
+
"android"
|
|
5
|
+
],
|
|
6
|
+
"apple": {
|
|
7
|
+
"modules": [
|
|
8
|
+
"RediadsGAMModule",
|
|
9
|
+
"RediadsGAMRenderedModule"
|
|
10
|
+
]
|
|
11
|
+
},
|
|
12
|
+
"android": {
|
|
13
|
+
"modules": [
|
|
14
|
+
"com.rediads.reactnative.gam.RediadsGAMModule",
|
|
15
|
+
"com.rediads.reactnative.gam.RediadsGAMRenderedModule"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'RediadsAdsGAM'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = 'Rediads'
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = { :ios => '16.4' }
|
|
14
|
+
s.swift_version = '5.9'
|
|
15
|
+
s.source = { git: '' }
|
|
16
|
+
s.static_framework = true
|
|
17
|
+
|
|
18
|
+
s.dependency 'ExpoModulesCore'
|
|
19
|
+
# The Rediads SDK's GAM classes (SDK-rendered and bidding only); they need Google's Mobile Ads SDK.
|
|
20
|
+
# Binary pods from Rediads' pod list (github.com/rediads-hq/rediads-ios), which the config plugins
|
|
21
|
+
# add as a Podfile source; see README.md for bare React Native. Same SDK version as
|
|
22
|
+
# @rediads/react-native-ads.
|
|
23
|
+
s.dependency 'RediadsGAM', '0.1.4'
|
|
24
|
+
s.dependency 'RediadsDisplay', '0.1.4'
|
|
25
|
+
# Google's Mobile Ads SDK: the app's own version (react-native-google-mobile-ads pins one), 12.0.0
|
|
26
|
+
# or newer, as the Rediads pods ask. Never raised by us: with nothing pinned, CocoaPods takes the newest.
|
|
27
|
+
s.dependency 'Google-Mobile-Ads-SDK', '>= 12.0.0'
|
|
28
|
+
|
|
29
|
+
s.pod_target_xcconfig = {
|
|
30
|
+
'DEFINES_MODULE' => 'YES',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
s.source_files = '**/*.{h,m,mm,swift}'
|
|
34
|
+
end
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import GoogleMobileAds
|
|
3
|
+
import RediadsGAM
|
|
4
|
+
|
|
5
|
+
/// Google Ad Manager, bidding only, through the SDK's RediadsGAM*BiddingOnly classes: they fetch a
|
|
6
|
+
/// Rediads bid and add its `hb_` keys to the GAM request; GAM runs its own auction and renders.
|
|
7
|
+
/// `fetchTargeting` returns just the keys, for apps that load GAM with another library. Interstitials
|
|
8
|
+
/// and rewarded ads go by the ID JavaScript gives them (src/RediadsGAMFullScreenAd.ts). Everything
|
|
9
|
+
/// runs on the main queue, where Google's SDK calls back.
|
|
10
|
+
public class RediadsGAMModule: Module {
|
|
11
|
+
/// Interstitials and rewarded ads by ID. Main queue only.
|
|
12
|
+
private var ads: [Int: GAMFullScreenSlot] = [:]
|
|
13
|
+
/// Bidding-only objects whose `fetchTargeting` hasn't answered yet. Main queue only.
|
|
14
|
+
private var fetching: [UUID: NSObject] = [:]
|
|
15
|
+
|
|
16
|
+
public func definition() -> ModuleDefinition {
|
|
17
|
+
Name("RediadsGAM")
|
|
18
|
+
|
|
19
|
+
Constant("googleSdkVersion") { GoogleMobileAds.string(for: MobileAds.shared.versionNumber) }
|
|
20
|
+
|
|
21
|
+
Events("onGAMEvent")
|
|
22
|
+
|
|
23
|
+
AsyncFunction("start") { (testDeviceIds: [String], promise: Promise) in
|
|
24
|
+
if !testDeviceIds.isEmpty {
|
|
25
|
+
MobileAds.shared.requestConfiguration.testDeviceIdentifiers = testDeviceIds
|
|
26
|
+
}
|
|
27
|
+
MobileAds.shared.start { _ in
|
|
28
|
+
promise.resolve(nil)
|
|
29
|
+
}
|
|
30
|
+
}.runOnQueue(.main)
|
|
31
|
+
|
|
32
|
+
AsyncFunction("fetchTargeting") {
|
|
33
|
+
(type: String, placement: String, size: String, format: String, promise: Promise) in
|
|
34
|
+
let id = UUID()
|
|
35
|
+
let done: ([String: String], NSError?) -> Void = { targeting, error in
|
|
36
|
+
DispatchQueue.main.async {
|
|
37
|
+
self.fetching[id] = nil
|
|
38
|
+
var result: [String: Any] = ["targeting": targeting]
|
|
39
|
+
if let error { result["error"] = error.localizedDescription }
|
|
40
|
+
promise.resolve(result)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
switch type {
|
|
44
|
+
case "interstitial":
|
|
45
|
+
let ad = RediadsGAMInterstitialBiddingOnly(placement: placement, format: Self.format(format))
|
|
46
|
+
self.fetching[id] = ad
|
|
47
|
+
ad.fetchTargeting(completion: done)
|
|
48
|
+
case "rewarded":
|
|
49
|
+
let ad = RediadsGAMRewardedBiddingOnly(placement: placement)
|
|
50
|
+
self.fetching[id] = ad
|
|
51
|
+
ad.fetchTargeting(completion: done)
|
|
52
|
+
default:
|
|
53
|
+
let ad = RediadsGAMBannerBiddingOnly(
|
|
54
|
+
placement: placement, size: Self.bannerSize(size), format: Self.format(format))
|
|
55
|
+
self.fetching[id] = ad
|
|
56
|
+
ad.fetchTargeting(completion: done)
|
|
57
|
+
}
|
|
58
|
+
}.runOnQueue(.main)
|
|
59
|
+
|
|
60
|
+
AsyncFunction("loadInterstitial") {
|
|
61
|
+
(adId: Int, placement: String, adUnitId: String, format: String, promise: Promise) in
|
|
62
|
+
self.ads.removeValue(forKey: adId)?.discard()
|
|
63
|
+
let slot = GAMFullScreenSlot(placement: placement, send: self.forward(adId))
|
|
64
|
+
self.ads[adId] = slot
|
|
65
|
+
let loader = RediadsGAMInterstitialBiddingOnly(placement: placement, format: Self.format(format))
|
|
66
|
+
slot.loader = loader
|
|
67
|
+
let request = AdManagerRequest()
|
|
68
|
+
loader.load(
|
|
69
|
+
gamAdUnitID: adUnitId,
|
|
70
|
+
request: request,
|
|
71
|
+
bidCompletion: { error in slot.bid(request, error) },
|
|
72
|
+
completion: { ad, error in
|
|
73
|
+
slot.loader = nil
|
|
74
|
+
if let ad {
|
|
75
|
+
slot.interstitial = ad
|
|
76
|
+
ad.fullScreenContentDelegate = slot
|
|
77
|
+
slot.loaded(ad.responseInfo)
|
|
78
|
+
} else {
|
|
79
|
+
slot.send(["type": "failed", "error": error?.localizedDescription ?? "GAM returned no ad."])
|
|
80
|
+
}
|
|
81
|
+
promise.resolve(error?.localizedDescription)
|
|
82
|
+
})
|
|
83
|
+
}.runOnQueue(.main)
|
|
84
|
+
|
|
85
|
+
AsyncFunction("loadRewarded") { (adId: Int, placement: String, adUnitId: String, promise: Promise) in
|
|
86
|
+
self.ads.removeValue(forKey: adId)?.discard()
|
|
87
|
+
let slot = GAMFullScreenSlot(placement: placement, send: self.forward(adId))
|
|
88
|
+
self.ads[adId] = slot
|
|
89
|
+
let loader = RediadsGAMRewardedBiddingOnly(placement: placement)
|
|
90
|
+
slot.loader = loader
|
|
91
|
+
let request = AdManagerRequest()
|
|
92
|
+
loader.load(
|
|
93
|
+
gamAdUnitID: adUnitId,
|
|
94
|
+
request: request,
|
|
95
|
+
bidCompletion: { error in slot.bid(request, error) },
|
|
96
|
+
completion: { ad, error in
|
|
97
|
+
slot.loader = nil
|
|
98
|
+
if let ad {
|
|
99
|
+
slot.rewarded = ad
|
|
100
|
+
ad.fullScreenContentDelegate = slot
|
|
101
|
+
slot.loaded(ad.responseInfo)
|
|
102
|
+
} else {
|
|
103
|
+
slot.send(["type": "failed", "error": error?.localizedDescription ?? "GAM returned no ad."])
|
|
104
|
+
}
|
|
105
|
+
promise.resolve(error?.localizedDescription)
|
|
106
|
+
})
|
|
107
|
+
}.runOnQueue(.main)
|
|
108
|
+
|
|
109
|
+
AsyncFunction("isReady") { (adId: Int) -> Bool in
|
|
110
|
+
self.ads[adId]?.isReady ?? false
|
|
111
|
+
}.runOnQueue(.main)
|
|
112
|
+
|
|
113
|
+
AsyncFunction("show") { (adId: Int) in
|
|
114
|
+
guard let slot = self.ads[adId] else { return }
|
|
115
|
+
// The top view controller, above any modal React Native has presented.
|
|
116
|
+
guard let host = self.appContext?.utilities?.currentViewController() else {
|
|
117
|
+
slot.send(["type": "failed", "error": "There is no view controller to show the ad from."])
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
slot.show(from: host)
|
|
121
|
+
}.runOnQueue(.main)
|
|
122
|
+
|
|
123
|
+
AsyncFunction("destroy") { (adId: Int) in
|
|
124
|
+
self.ads.removeValue(forKey: adId)?.discard()
|
|
125
|
+
}.runOnQueue(.main)
|
|
126
|
+
|
|
127
|
+
View(RediadsGAMBannerSlot.self) {
|
|
128
|
+
Events("onAdEvent")
|
|
129
|
+
|
|
130
|
+
Prop("placement") { (view: RediadsGAMBannerSlot, value: String) in
|
|
131
|
+
view.placement = value
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Prop("adUnitId") { (view: RediadsGAMBannerSlot, value: String) in
|
|
135
|
+
view.adUnitId = value
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
Prop("size") { (view: RediadsGAMBannerSlot, value: String) in
|
|
139
|
+
view.size = value
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
Prop("format") { (view: RediadsGAMBannerSlot, value: String) in
|
|
143
|
+
view.format = value
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
OnViewDidUpdateProps { (view: RediadsGAMBannerSlot) in
|
|
147
|
+
view.loadIfReady()
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/// Sends an ad's events to JavaScript under its ID.
|
|
153
|
+
private func forward(_ adId: Int) -> ([String: Any]) -> Void {
|
|
154
|
+
return { [weak self] payload in
|
|
155
|
+
var payload = payload
|
|
156
|
+
payload["adId"] = adId
|
|
157
|
+
self?.sendEvent("onGAMEvent", payload)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// The JavaScript format names: display, video, displayAndVideo.
|
|
162
|
+
static func format(_ name: String) -> RediadsAdFormat {
|
|
163
|
+
switch name {
|
|
164
|
+
case "video": return .video
|
|
165
|
+
case "displayAndVideo": return .displayAndVideo
|
|
166
|
+
default: return .display
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static func bannerSize(_ name: String) -> CGSize {
|
|
171
|
+
name == "320x50"
|
|
172
|
+
? RediadsGAMBannerBiddingOnly.banner320x50 : RediadsGAMBannerBiddingOnly.mediumRectangle300x250
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// The Rediads keys in a GAM request, after the SDK added them.
|
|
176
|
+
static func rediadsKeys(_ request: AdManagerRequest) -> [String: String] {
|
|
177
|
+
var keys: [String: String] = [:]
|
|
178
|
+
for (key, value) in request.customTargeting ?? [:] where key.hasPrefix("hb_") {
|
|
179
|
+
keys[key] = "\(value)"
|
|
180
|
+
}
|
|
181
|
+
return keys
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// The `loaded` event's fields: GAM's line item and creative.
|
|
185
|
+
static func describe(_ info: GoogleMobileAds.ResponseInfo?) -> [String: Any] {
|
|
186
|
+
var fields: [String: Any] = [:]
|
|
187
|
+
let extras = info?.extras ?? [:]
|
|
188
|
+
if let lineItem = extras["line_item_id"] { fields["lineItemId"] = "\(lineItem)" }
|
|
189
|
+
if let creative = extras["creative_id"] { fields["creativeId"] = "\(creative)" }
|
|
190
|
+
return fields
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// One interstitial or rewarded ad: the Rediads loader while it loads, then Google's ad.
|
|
195
|
+
final class GAMFullScreenSlot: NSObject, FullScreenContentDelegate {
|
|
196
|
+
let placement: String
|
|
197
|
+
private let forward: ([String: Any]) -> Void
|
|
198
|
+
/// RediadsGAMInterstitialBiddingOnly or RediadsGAMRewardedBiddingOnly, kept until GAM answers.
|
|
199
|
+
var loader: NSObject?
|
|
200
|
+
var interstitial: AdManagerInterstitialAd?
|
|
201
|
+
var rewarded: GoogleMobileAds.RewardedAd?
|
|
202
|
+
private var shown = false
|
|
203
|
+
private var discarded = false
|
|
204
|
+
|
|
205
|
+
init(placement: String, send: @escaping ([String: Any]) -> Void) {
|
|
206
|
+
self.placement = placement
|
|
207
|
+
forward = send
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/// Stops this ad's events, as Android's `destroy` does: a load under an ID in use (JavaScript reuses an
|
|
211
|
+
/// ad's ID when it loads again) replaces the slot, and none of the old one's events (a late `loaded`, the
|
|
212
|
+
/// `dismissed` of an ad still on screen) reach JavaScript as the new ad's. A load in flight still
|
|
213
|
+
/// resolves its own promise.
|
|
214
|
+
func discard() {
|
|
215
|
+
discarded = true
|
|
216
|
+
interstitial?.fullScreenContentDelegate = nil
|
|
217
|
+
rewarded?.fullScreenContentDelegate = nil
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
var isReady: Bool {
|
|
221
|
+
!shown && (interstitial != nil || rewarded != nil)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// Sends an event; nil values are left out.
|
|
225
|
+
func send(_ payload: [String: Any?]) {
|
|
226
|
+
guard !discarded else { return }
|
|
227
|
+
var event: [String: Any] = ["placement": placement]
|
|
228
|
+
for (key, value) in payload {
|
|
229
|
+
if let value { event[key] = value }
|
|
230
|
+
}
|
|
231
|
+
forward(event)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
func bid(_ request: AdManagerRequest, _ error: Error?) {
|
|
235
|
+
send(["type": "bid", "targeting": RediadsGAMModule.rediadsKeys(request), "error": error?.localizedDescription])
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
func loaded(_ info: GoogleMobileAds.ResponseInfo) {
|
|
239
|
+
send(["type": "loaded"].merging(RediadsGAMModule.describe(info)) { first, _ in first })
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
func show(from host: UIViewController) {
|
|
243
|
+
guard isReady else {
|
|
244
|
+
send(["type": "failed", "error": shown ? "The ad was already shown." : "The ad is not loaded yet."])
|
|
245
|
+
return
|
|
246
|
+
}
|
|
247
|
+
shown = true
|
|
248
|
+
if let ad = interstitial {
|
|
249
|
+
ad.present(from: host)
|
|
250
|
+
} else if let ad = rewarded {
|
|
251
|
+
ad.present(from: host) { [weak self, weak ad] in
|
|
252
|
+
guard let reward = ad?.adReward else { return }
|
|
253
|
+
self?.send(["type": "rewarded", "rewardType": reward.type, "rewardAmount": reward.amount.intValue])
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// FullScreenContentDelegate: Google's events for the ad GAM served.
|
|
259
|
+
|
|
260
|
+
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
|
|
261
|
+
send(["type": "impression"])
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
|
|
265
|
+
send(["type": "clicked"])
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
|
|
269
|
+
send(["type": "failed", "error": error.localizedDescription])
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
|
|
273
|
+
send(["type": "presented"])
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
|
|
277
|
+
send(["type": "dismissed"])
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/// One GAM banner slot. The banner is made once React has set the props and loaded once; JavaScript
|
|
282
|
+
/// gives the view a new key to load a new one (bidding-only banners refresh by loading again).
|
|
283
|
+
final class RediadsGAMBannerSlot: ExpoView, BannerViewDelegate {
|
|
284
|
+
let onAdEvent = EventDispatcher()
|
|
285
|
+
var placement = ""
|
|
286
|
+
var adUnitId = ""
|
|
287
|
+
var size = "300x250"
|
|
288
|
+
var format = "display"
|
|
289
|
+
private var bannerView: AdManagerBannerView?
|
|
290
|
+
private var rediads: RediadsGAMBannerBiddingOnly?
|
|
291
|
+
|
|
292
|
+
func loadIfReady() {
|
|
293
|
+
guard rediads == nil, !placement.isEmpty, !adUnitId.isEmpty else { return }
|
|
294
|
+
let bannerView = AdManagerBannerView(adSize: size == "320x50" ? AdSizeBanner : AdSizeMediumRectangle)
|
|
295
|
+
bannerView.adUnitID = adUnitId
|
|
296
|
+
bannerView.rootViewController = appContext?.utilities?.currentViewController()
|
|
297
|
+
// Set before RediadsGAMBannerBiddingOnly.load, which puts its stand-in in front and passes calls on.
|
|
298
|
+
bannerView.delegate = self
|
|
299
|
+
addSubview(bannerView)
|
|
300
|
+
self.bannerView = bannerView
|
|
301
|
+
|
|
302
|
+
let rediads = RediadsGAMBannerBiddingOnly(
|
|
303
|
+
placement: placement, size: RediadsGAMModule.bannerSize(size), format: RediadsGAMModule.format(format))
|
|
304
|
+
self.rediads = rediads
|
|
305
|
+
let request = AdManagerRequest()
|
|
306
|
+
rediads.load(bannerView, request: request) { [weak self] error in
|
|
307
|
+
if bannerView.rootViewController == nil {
|
|
308
|
+
bannerView.rootViewController = self?.appContext?.utilities?.currentViewController()
|
|
309
|
+
}
|
|
310
|
+
self?.send([
|
|
311
|
+
"type": "bid", "targeting": RediadsGAMModule.rediadsKeys(request),
|
|
312
|
+
"error": error?.localizedDescription,
|
|
313
|
+
])
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
override func layoutSubviews() {
|
|
318
|
+
super.layoutSubviews()
|
|
319
|
+
bannerView?.center = CGPoint(x: bounds.midX, y: bounds.midY)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private func send(_ payload: [String: Any?]) {
|
|
323
|
+
var event: [String: Any] = ["placement": placement]
|
|
324
|
+
for (key, value) in payload {
|
|
325
|
+
if let value { event[key] = value }
|
|
326
|
+
}
|
|
327
|
+
onAdEvent(event)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// BannerViewDelegate: Google's events for the ad GAM served.
|
|
331
|
+
|
|
332
|
+
func bannerViewDidReceiveAd(_ bannerView: GoogleMobileAds.BannerView) {
|
|
333
|
+
send(["type": "loaded"].merging(RediadsGAMModule.describe(bannerView.responseInfo)) { first, _ in first })
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
func bannerView(_ bannerView: GoogleMobileAds.BannerView, didFailToReceiveAdWithError error: Error) {
|
|
337
|
+
send(["type": "failed", "error": error.localizedDescription])
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
func bannerViewDidRecordImpression(_ bannerView: GoogleMobileAds.BannerView) {
|
|
341
|
+
send(["type": "impression"])
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
func bannerViewDidRecordClick(_ bannerView: GoogleMobileAds.BannerView) {
|
|
345
|
+
send(["type": "clicked"])
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
func bannerViewWillPresentScreen(_ bannerView: GoogleMobileAds.BannerView) {
|
|
349
|
+
send(["type": "presented"])
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
func bannerViewDidDismissScreen(_ bannerView: GoogleMobileAds.BannerView) {
|
|
353
|
+
send(["type": "dismissed"])
|
|
354
|
+
}
|
|
355
|
+
}
|