-
Notifications
You must be signed in to change notification settings - Fork 14
/
admediator_admob.lua
231 lines (177 loc) · 6.36 KB
/
admediator_admob.lua
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
------------------------------------------------------------
------------------------------------------------------------
-- Ad Mediator for Corona
--
-- Ad network mediation module for Ansca Corona
-- by Deniz Aydinoglu
--
-- he2apps.com
--
-- GitHub repository and documentation:
-- https://github.com/deniza/Ad-Mediator-for-Corona
------------------------------------------------------------
------------------------------------------------------------
local urlmodule = require("socket.url")
local instance = {}
local adServerUrl = "http://googleads.g.doubleclick.net"
local admobTestPublisherId = "a14e8f77524dde8"
local publisherId = ""
local platform = system.getInfo("model")
local submodel = system.getInfo("architectureInfo")
local deviceType = AdMediator.getPlatform()
local testMode
local appIdentifier
local userAgent = AdMediator.getUserAgentString()
local preqs = 0
local askip = 0
local ptime = 1
local starttime
local prevClickUrl
local prevOpenUrl
local jsVersion = "afma-sdk-i-v5.0.5"
local language = "en"
local adFormat = "320x50_mb"
--local adFormat = "728x90_as"
--local adFormat = "468x60_as"
local function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
local function decodeUrlEncodedString(str)
return string.gsub(str, "%%(%x%x)", function (h) return string.char(tonumber(h, 16)) end)
end
local function parse_query_string(query)
local parsed = {}
local pos = 0
query = string.gsub(query, "&", "&")
query = string.gsub(query, "<", "<")
query = string.gsub(query, ">", ">")
local function ginsert(qstr)
local first, last = string.find(qstr, "=")
if first then
parsed[string.sub(qstr, 0, first-1)] = string.sub(qstr, first+1)
end
end
while true do
local first, last = string.find(query, "&", pos)
if first then
ginsert(string.sub(query, pos, first-1));
pos = last+1
else
ginsert(string.sub(query, pos));
break;
end
end
return parsed
end
local function fixLink(link)
local i = string.find(link, "/aclk%?")
if i==1 then
link = string.gsub(link, "/aclk%?", adServerUrl.."/aclk?")
end
return link
end
local function webPopupListener( event )
if string.find(event.url, "file://",1,true) then
return true
elseif string.find(event.url, "gmsg://",1,true) then
local parsedUrl = urlmodule.parse(event.url)
local params = parse_query_string(parsedUrl.query)
local link = decodeUrlEncodedString(params.u)
if parsedUrl.path == "/click" then
if prevClickUrl ~= link then
network.request(link,"GET")
prevClickUrl = link
end
elseif parsedUrl.path == "/open" then
if prevOpenUrl ~= link then
timer.performWithDelay(10,function()
link = fixLink(link)
system.openURL(link)
native.cancelWebPopup()
end)
prevOpenUrl = link
end
end
return true
elseif string.find(event.url, "http://",1,true) or string.find(event.url, "https://",1,true) or string.find(event.url, "tel:",1,true) or string.find(event.url, "mailto:",1,true) then
local parsedUrl = urlmodule.parse(event.url)
if parsedUrl.host == "googleads.g.doubleclick.net" or parsedUrl.host == "www.googleadservices.com" then
return true
end
timer.performWithDelay(10,function()
system.openURL(event.url)
native.cancelWebPopup()
end)
return true
else
print("unknown protocol scheme", event.url)
return true
end
end
local function adRequestListener(event)
local available = true
local htmlContent = ""
if event.isError or not string.find(event.response, "<html>", 1, true) then
available = false
end
if available then
-- disable current viewport meta tag if any
htmlContent = string.gsub(event.response,'<meta name="viewport','<meta name="viewport_disabled')
-- insert ours
local metaTag = AdMediator.viewportMetaTagForPlatform() .. "<meta charset='utf-8'>"
htmlContent = string.gsub(htmlContent,'<head>','<head>'..metaTag)
end
Runtime:dispatchEvent({name="adMediator_adResponse",available=available,htmlContent=htmlContent})
end
function instance:init(networkParams)
publisherId = networkParams.publisherId
testMode = networkParams.test
appIdentifier = networkParams.appIdentifier or "com.yourcompany.yourapp"
platform = urlencode(platform)
submodel = urlencode(submodel)
if deviceType == AdMediator.PLATFORM_ANDROID then
jsVersion = "afma-sdk-a-v4.3.1"
end
print("admob init:",publisherId)
end
function instance:customWebPopupListener()
return webPopupListener
end
function instance:requestAd()
prevClickUrl = nil
prevOpenUrl = nil
local headers = {}
headers["User-Agent"] = userAgent
local params = {}
params.headers = headers
local requestUri = adServerUrl
if testMode then
publisherId = admobTestPublisherId
end
local now = os.time()
preqs = preqs + 1
if preqs == 1 then
starttime = now
end
askip = askip + 1
if askip > 4 then
askip = 0
end
local prl_net = ""
if preqs > 1 then
prl_net = "&prl="..math.random(500,600).."&net=wi"
ptime = (now-starttime) * 1000
end
requestUri = requestUri .. "/mads/gma?u_audio=1&hl="..language.."&preqs="..preqs.."&app_name="..appIdentifier.."&u_h=480&cap_bs=1&u_so=p&u_w=320&ptime="..ptime.."&js="..jsVersion.."&slotname="..publisherId.."&platform="..platform.."&submodel="..submodel.."&u_sd=2&format="..adFormat.."&output=html®ion=mobile_app&u_tz=-120&ex=1&client_sdk=1&askip="..askip.."&caps=SdkAdmobApiForAds&jsv=3"..prl_net
if testMode then
requestUri = requestUri .. "&adtest=on"
end
network.request(requestUri,"GET",adRequestListener,params)
end
return instance