-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanner.js
93 lines (92 loc) · 2.62 KB
/
banner.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
(function () {
var log_impression = function (title, has_clicked, cost, cost_type, elem) {
var obj = {
cost_type: cost_type,
cost: cost,
clicked: has_clicked
}
if (window.ax) {
return window.ax({
id: 'banner' + (has_clicked ? '_clicked' : '') + ':' + title,
properties: obj
})
}
}
var win = window,
doc = document,
docElem = doc && doc.documentElement,
viewportW = function () {
var a = docElem.clientWidth,
b = win.innerWidth
return a < b ? b : a
},
viewportH = function () {
var a = docElem.clientHeight,
b = win.innerHeight
return a < b ? b : a
},
calibrate = function (coords, cushion) {
var o = {}
cushion = +cushion || 0
o.width =
(o.right = coords.right + cushion) - (o.left = coords.left - cushion)
o.height =
(o.bottom = coords.bottom + cushion) - (o.top = coords.top - cushion)
return o
},
rectangle = function (el, cushion) {
el = el && !el.nodeType ? el[0] : el
if (!el || el.nodeType !== 1) {
return false
}
return calibrate(el.getBoundingClientRect(), cushion)
},
inViewport = function (el, cushion) {
var r = rectangle(el, cushion)
return (
!!r &&
r.bottom >= 0 &&
r.right >= 0 &&
r.top <= viewportH() &&
r.left <= viewportW()
)
}
var send_impression = function (elem, attr, clicked) {
elem.className = elem.className + ' ax-tracked'
var title = elem.getAttribute(attr),
cost = elem.getAttribute('data-ax-content-cost'),
cost_type = elem.getAttribute('data-ax-content-cost-type')
if (title) {
log_impression(title, clicked, cost, cost_type, elem)
}
}
setInterval(function () {
var banners = doc.querySelectorAll(
'[data-ax-content-view]:not(.ax-tracked)'
)
for (var i = 0, j = banners.length; i < j; i++) {
if (inViewport(banners[i], -100)) {
send_impression(banners[i], 'data-ax-content-view', false)
}
}
}, 500)
document.addEventListener('click', function (e) {
var self = e.target
if (self.matches('[data-ax-content-click]:not(.ax-tracked)')) {
send_impression(self, 'data-ax-content-click', true)
if (self.nodeName === 'A') {
setTimeout(function () {
var target = self.getAttribute('target'),
href = self.getAttribute('href')
if (target) {
window.open(href, target)
} else {
window.location = href
}
}, 300)
return false
}
return true
}
})
}())