Skip to content

Commit

Permalink
Add custom ad display
Browse files Browse the repository at this point in the history
  • Loading branch information
7kfpun committed Sep 8, 2018
1 parent 3c653e2 commit 88695bb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
12 changes: 6 additions & 6 deletions app/elements/ad-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export default class AdCustom extends Component {
imageUrl: ad.imageUrl,
destinationUrl: ad.destinationUrl,
});
}

tracker.logEvent(`ad-custom-${this.props.client}-impression`, {
client: this.props.client,
url: ad.imageUrl,
destinationUrl: ad.destinationUrl,
});
tracker.logEvent(`ad-custom-${this.props.client}-impression`, {
client: this.props.client,
url: ad.imageUrl,
destinationUrl: ad.destinationUrl,
});
}
}

onOpenAd = (url) => {
Expand Down
32 changes: 31 additions & 1 deletion app/elements/admob.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import firebase from 'react-native-firebase';
import AdCustom from './ad-custom';

import { config } from '../config';
import { getAd } from '../utils/firebase-config';

const { AdRequest, Banner } = firebase.admob;
const request = new AdRequest();
Expand Down Expand Up @@ -52,25 +53,53 @@ export default class Admob extends Component {
state = {
isReceived: false,
isReceivedFailed: false,
isShowAdCustom: false,
};

componentDidMount() {
this.setIsReceivedFailedTimeout = setTimeout(() => {
if (!this.state.isReceived) {
if (!this.state.isReceived && !this.state.isShowAdCustom) {
this.setState({ isReceivedFailed: true });
}
}, 30 * 1000);

this.checkShinbaAd();
this.checkShinbaAdInterval = setInterval(() => {
this.checkShinbaAd();
}, 60 * 1000);
}

componentWillUnmount() {
if (this.setIsReceivedFailedTimeout) clearTimeout(this.setIsReceivedFailedTimeout);
if (this.checkShinbaAdInterval) clearInterval(this.checkShinbaAdInterval);
}

async checkShinbaAd() {
const ad = await getAd('shinba');
const isShowAdCustom = ad.impressionRate > Math.random();
console.log('isShowAdCustom', isShowAdCustom);

if (isShowAdCustom && ad.impressionRate > 0) {
this.setState({
isShowAdCustom: true,
key: Math.random(),
});
} else {
this.setState({
isShowAdCustom: false,
});
}
}

render() {
if (this.state.isReceivedFailed) {
return <AdCustom />;
}

if (this.state.isShowAdCustom) {
return <AdCustom client="shinba" />;
}

let height = 50;
if (this.props.bannerSize === 'LARGE_BANNER') {
height = 100;
Expand All @@ -89,6 +118,7 @@ export default class Admob extends Component {
}}
>
<Banner
key={this.state.key}
size={this.props.bannerSize}
unitId={this.props.unitId && config.admob[this.props.unitId]}
request={request.build()}
Expand Down

0 comments on commit 88695bb

Please sign in to comment.