-
Notifications
You must be signed in to change notification settings - Fork 0
/
at-smarttag-viewability.js
173 lines (173 loc) · 6.98 KB
/
at-smarttag-viewability.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(function () {
window.ATInternet = window.ATInternet || {};
window.ATInternet.Callbacks = window.ATInternet.Callbacks || {};
window.ATInternet.Callbacks.viewability = window.ATInternet.Callbacks.viewability || function (tag) {
var _at_final_blocks = [];
var _at_global_width = 0;
var _at_global_height = 0;
var _at_default_percent = 100;
var _init = function () {
_detectViewableTag();
_reload();
_addEventListeners();
};
var _detectViewableTag = function () {
var percent = tag.getConfig('viewabilityPercent') || _at_default_percent;
var at_blocks = document.querySelectorAll('[data-atview-id]');
var block;
for (var i = 0; i < at_blocks.length; i++) {
block = at_blocks[i];
if (_isValidBlock(block)) {
_at_final_blocks.push({
'label': block.getAttribute('data-atview-id'),
'percent': parseInt(block.getAttribute('data-atview-percent'), 10) || percent,
'width': _width(block),
'height': _height(block),
'posx': _posx(block),
'posy': _posy(block),
'processed': false
});
}
}
};
var _addEventListeners = function () {
if (window.addEventListener) {
window.addEventListener('scroll', _reload, false);
window.addEventListener('resize', _reload, false);
window.addEventListener('focus', _reload, false);
} else if (window.attachEvent) {
window.attachEvent('onscroll', _reload);
window.attachEvent('onresize', _reload)
}
if (document.attachEvent) {
document.attachEvent('onfocusin', _reload);
}
};
var _reload = function () {
_at_global_width = _getGlobalWidth();
_at_global_height = _getGlobalHeight();
_processBlocks();
};
var _getGlobalWidth = function () {
var width1 = _getValidSize(window.innerWidth, document.documentElement ? document.documentElement.clientWidth : 0),
width2 = document.body ? document.body.clientWidth : 0;
return (width1 || width2);
};
var _getGlobalHeight = function () {
var height1 = _getValidSize(window.innerHeight, document.documentElement ? document.documentElement.clientHeight : 0),
height2 = document.body ? document.body.clientHeight : 0;
return (height1 || height2);
};
var _getValidSize = function (win, doc) {
var result = win ? win : 0;
if (doc && (!result || (result > doc))) result = doc;
return result;
};
var _isValidBlock = function (block) {
var tag_names = ['DIV', 'TABLE', 'TR', 'TD', 'UL', 'LI', 'ARTICLE', 'FOOTER', 'ASIDE', 'HEADER', 'NAV', 'SECTION'],
node_name = block.nodeName;
for (var j = 0; j < tag_names.length; j++) {
if ((node_name === tag_names[j]) && (block.nodeType === 1)) {
return true;
}
}
return false;
};
var _isSafari = function () {
return /Safari/.test(navigator.userAgent);
};
var _isMac = function () {
return /Mac/.test(navigator.appVersion);
};
var _width = function (element) {
var width = element.offsetWidth || element.style.width || 0;
if (_isSafari() && _isMac() && (element.nodeName === 'TR') && element.firstChild && element.lastChild)
width = element.lastChild.offsetLeft + element.lastChild.offsetWidth - element.firstChild.offsetLeft;
return width;
};
var _height = function (element) {
var height = element.offsetHeight || element.style.height || 0;
if (_isSafari() && _isMac() && (element.nodeName === 'TR') && element.firstChild && element.lastChild)
height = element.lastChild.offsetTop + element.lastChild.offsetHeight - element.firstChild.offsetTop;
return height;
};
var _posx = function (element) {
return _pos(element, 'Left');
};
var _posy = function (element) {
var posy = _pos(element, 'Top');
if (_isSafari() && _isMac() && (element.nodeName === 'TR') && element.firstChild)
posy += element.firstChild.offsetTop;
return posy;
};
var _pos = function (element, type) {
if (typeof element.offsetParent !== 'undefined') {
for (var top = 0, left = 0; element; element = element.offsetParent) {
top += element.offsetTop;
left += element.offsetLeft;
}
if (type === 'Top') {
return top;
}
return left;
}
if (type === 'Top') {
return element.y;
}
return element.x;
};
var _getSizePart = function (blockPosition, scrollSize, blockSize, globalSize) {
if ((blockPosition >= scrollSize) && ((blockPosition + blockSize) <= (scrollSize + globalSize)))
return 100;
else if (((blockPosition < scrollSize) && ((blockPosition + blockSize) <= (scrollSize))) || (blockPosition >= (scrollSize + globalSize)))
return 0;
else if ((blockPosition <= scrollSize) && ((blockPosition + blockSize) >= (scrollSize + globalSize)))
return (globalSize / blockSize) * 100;
else if (blockPosition < scrollSize)
return ((blockPosition + blockSize - scrollSize) / blockSize) * 100;
else if ((blockPosition + blockSize) > (scrollSize + globalSize))
return ((scrollSize + globalSize - blockPosition) / blockSize) * 100;
};
var _processBlocks = function () {
var scTop = _getScrollTop();
var scLeft = _getScrollLeft();
var p = 0, pX = 0, pY = 0, Lx = 0, Ly = 0, areaA = 0, areaT = 0;
var block;
for (var m = 0; m < _at_final_blocks.length; m++) {
block = _at_final_blocks[m];
pY = _getSizePart(block.posy, scTop, block.height, _at_global_height);
pX = _getSizePart(block.posx, scLeft, block.width, _at_global_width);
Ly = (pY * block.height) / 100;
Lx = (pX * block.width) / 100;
areaA = Lx * Ly;
areaT = block.width * block.height;
p = Math.round((areaA / areaT) * 100);
if (p >= _at_final_blocks[m].percent && !_at_final_blocks[m].processed) {
tag.publisher.set({
impression: {
campaignId: '[' + _at_final_blocks[m].label + ']',
creation: '[' + _at_final_blocks[m].percent + ']'
}
});
tag.dispatch();
_at_final_blocks[m].processed = true;
}
}
};
var _getScrollTop = function () {
var pag = window.pageYOffset || 0,
st = document.documentElement ? document.documentElement.scrollTop : 0,
bst = document.body ? document.body.scrollTop : 0;
return Math.max(pag, st, bst);
};
var _getScrollLeft = function () {
var pag = window.pageXOffset || 0,
sl = document.documentElement ? document.documentElement.scrollLeft : 0,
bsl = document.body ? document.body.scrollLeft : 0;
return Math.max(pag, sl, bsl);
};
_init();
};
window.ATInternet.Utils = window.ATInternet.Utils || { dispatchCallbackEvent: function () { } };
window.ATInternet.Utils.dispatchCallbackEvent('viewability');
})();