-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (136 loc) · 4.18 KB
/
index.js
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
function login() {
const userId = document.getElementById('user-id').value;
if (!userId) {
return;
}
const userProfile = {
userId: userId,
birthYear: 1980,
};
if (window.BuzzAdBenefit) {
const genders = {
MALE: window.BuzzAdBenefit.Gender.MALE,
FEMALE: window.BuzzAdBenefit.Gender.FEMALE,
}
userProfile.gender = genders.FEMALE;
window.BuzzAdBenefit.setUserProfile(userProfile);
setLoginUi(userId);
}
}
function setLoginUi(userId) {
document.getElementById('user-id').value = userId;
document.getElementById('user-id').disabled = true;
document.getElementById('login-button').disabled = true;
}
function showToast(message, bad) {
if (bad) {
toastr.error(message);
} else {
toastr.info(message);
}
}
function log(message, bad) {
console.log(message);
showToast(message, bad);
}
(function() {
const userIdView = document.getElementById('user-id');
userIdView.value += Math.random().toString(36).substring(7);
})();
/**
* Setup BuzzAdBenefit SDK
*/
(function() {
if (!BuzzAdBenefit) {
return;
}
// Initiate SDK
const config = {
appId: APP_ID
}
BuzzAdBenefit.init(config);
function loadAd() {
setLoginUi(BuzzAdBenefit.instance.core.userProfile.userId);
// Setup Ad Placement
const loadConfig = {
unitId: {
android: UNIT_ID_ANDROID,
ios: UNIT_ID_IOS,
}
}
BuzzAdBenefit.loadAd(loadConfig)
.then(function (nativeAd) {
log('ON AD LOADED: An ad is loaded.');
populateAd(nativeAd);
}, function(error) {
if (error.message === 'EMPTY_RESPONSE') {
log('ON LOAD ERROR: NO FILL', true);
} else {
log('ON LOAD ERROR: An error is detected: ' + error.code + ', ' + error.message, true);
}
hideAd();
});
}
function onError(error) {
console.log(error.message);
}
function reloadAd() {
loadAd();
}
window.reloadAd = reloadAd;
BuzzAdBenefit.ensureAuthenticated
.then(loadAd, onError);
function updateCtaView(ctaView, nativeAd) {
var ctaTextHeader = '';
const classReward = 'reward';
const classParticipated = 'participated';
ctaView.classList.remove(classReward);
ctaView.classList.remove(classParticipated);
if (nativeAd.reward) {
if (nativeAd.participated) {
ctaView.classList.add(classParticipated);
} else {
ctaView.classList.add(classReward);
ctaTextHeader = '+' + nativeAd.reward + ' ';
}
}
ctaView.innerText = ctaTextHeader + nativeAd.callToAction;
}
function renderAd(placementView, nativeAd) {
placementView.style.display = '';
placementView.getElementsByClassName('icon')[0].setAttribute('src', nativeAd.iconUrl);
placementView.getElementsByClassName('name')[0].innerHTML = nativeAd.title;
placementView.getElementsByClassName('body')[0].innerHTML = nativeAd.description;
updateCtaView(placementView.getElementsByClassName('cta')[0], nativeAd);
}
function hideAd() {
const placementView = document.getElementById('placement1');
placementView.style.display = 'none';
}
function populateAd(nativeAd) {
const adListener = {
onImpressed: function(placementView, nativeAd) {
log('ON IMPRESSED: The ad is impressed.');
},
onClicked: function(placementView, nativeAd) {
log('ON CLICKED: The ad is clicked.');
},
onRewardRequested: function(placementView, nativeAd) {
log('ON REWARD REQUESTED: Reward is requested.');
},
onRewarded: function(placementView, nativeAd, result) {
log('ON REWARDED: The result of Reward: ' + result);
},
onParticipated: function(placementView, nativeAd) {
log('ON PARTICIPATED: The ad is set to particiated.');
updateCtaView(placementView.getElementsByClassName('cta')[0], nativeAd);
},
onVideoError: function(placementView, nativeAd, errorCode, errorMessage) {
log('ON VIDEO ERROR: An error is detected: ' + errorCode + '\n' + errorMessage);
},
};
const placementView = document.getElementById('placement1');
BuzzAdBenefit.registerNativeAd(nativeAd, placementView, adListener);
renderAd(placementView, nativeAd);
}
})();