forked from bubjavier/react-native-dfp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRNDFPBanner.js
More file actions
164 lines (143 loc) · 4.5 KB
/
RNDFPBanner.js
File metadata and controls
164 lines (143 loc) · 4.5 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
import React from 'react';
import {
requireNativeComponent,
View,
ViewPropTypes,
Platform,
} from 'react-native';
import PropTypes from 'prop-types';
const RNBanner = requireNativeComponent('RNDFPBanner', DFPBanner);
export default class DFPBanner extends React.Component {
state = {
height: null,
width: null,
};
onSizeChange = ({ nativeEvent }) => {
if (this.props.onSizeChange) {
const { height, width } = nativeEvent;
this.setState({ height, width });
}
};
onAdViewDidFailToReceiveAd = ({ nativeEvent }) => {
if (this.props.onAdViewDidFailToReceiveAd) {
this.props.onAdViewDidFailToReceiveAd(nativeEvent.error);
}
};
render() {
const {
adUnitID,
testDeviceID,
dimensions,
customTargeting,
style,
} = this.props;
const width = this.state.width || this.props.style.width;
const height = this.state.height || this.props.style.height;
let { bannerSize, adSizes } = this.props;
// Dimensions gets highest priority
if (dimensions && dimensions.width && dimensions.height) {
bannerSize = undefined;
adSizes = undefined;
}
// AdSizes gets second priority
if (adSizes && adSizes.length > 0) {
bannerSize = undefined;
}
// Default to something if nothing is set
if (
!bannerSize &&
(!dimensions || !dimensions.width || !dimensions.height) &&
(!adSizes || !adSizes.length > 0)
) {
bannerSize = 'smartBannerPortrait';
}
console.log({ style, width, height });
return (
<View style={style}>
<RNBanner
style={{ width, height }}
bannerSize={bannerSize}
adSizes={adSizes}
dimensions={dimensions}
testDeviceID={testDeviceID}
adUnitID={adUnitID}
onSizeChange={this.onSizeChange}
onAdViewDidReceiveAd={this.props.onAdViewDidReceiveAd}
onAdViewDidFailToReceiveAd={this.onAdViewDidFailToReceiveAd}
onAdViewWillPresentScreen={this.props.onAdViewWillPresentScreen}
onAdViewWillDismissScreen={this.props.onAdViewWillDismissScreen}
onAdViewDidDismissScreen={this.props.onAdViewDidDismissScreen}
onAdViewWillLeaveApplication={this.props.onAdViewWillLeaveApplication}
onAdViewEvent={this.props.onAdViewEvent}
customTargeting={customTargeting}
/>
</View>
);
}
}
DFPBanner.propTypes = {
style: ViewPropTypes.style,
/**
* Mobile Ads iOS library banner size constants
* (https://developers.google.com/admob/ios/banner)
* banner (320x50, Standard Banner for Phones and Tablets)
* largeBanner (320x100, Large Banner for Phones and Tablets)
* mediumRectangle (300x250, IAB Medium Rectangle for Phones and Tablets)
* fullBanner (468x60, IAB Full-Size Banner for Tablets)
* leaderboard (728x90, IAB Leaderboard for Tablets)
* smartBannerPortrait (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
* smartBannerLandscape (Screen width x 32|50|90, Smart Banner for Phones and Tablets)
*
* banner is default
*/
bannerSize: PropTypes.string,
/**
* Custom targeting to add to the dfp request
*/
customTargeting: PropTypes.shape({
amzn_b: PropTypes.string,
amzn_h: PropTypes.string,
amznp: Platform.OS === 'ios' ? PropTypes.array : PropTypes.string,
amznslots: PropTypes.string,
}),
/**
* Custom banner size (instead of using bannerSize)
*/
dimensions: PropTypes.shape({
height: PropTypes.number,
width: PropTypes.number,
}),
/**
* Array of some combination of bannerSize and dimensions that are valid for the ad
* Example: ['mediumRectangle', { width: 320, height: 400 }, 'smartBannerPortrait']
*/
adSizes: PropTypes.array,
/**
* Mobile ad unit ID
*/
adUnitID: PropTypes.string,
/**
* Test device ID
*/
testDeviceID: PropTypes.string,
/**
* Mobile ads iOS library events
*/
onAdViewDidReceiveAd: PropTypes.func,
onAdViewDidFailToReceiveAd: PropTypes.func,
onAdViewWillPresentScreen: PropTypes.func,
onAdViewWillDismissScreen: PropTypes.func,
onAdViewDidDismissScreen: PropTypes.func,
onAdViewWillLeaveApplication: PropTypes.func,
onAdViewEvent: PropTypes.func,
// ...View.propTypes,
};
DFPBanner.defaultProps = {
onAdViewDidReceiveAd: () => {},
onAdViewDidFailToReceiveAd: () => {},
onAdViewWillPresentScreen: () => {},
onAdViewWillDismissScreen: () => {},
onAdViewDidDismissScreen: () => {},
onAdViewWillLeaveApplication: () => {},
onAdViewEvent: () => {},
};