/
max_scroll.js
executable file
·117 lines (103 loc) · 2.89 KB
/
max_scroll.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
/**
* GAS - Google Analytics on Steroids
*
* Max-Scroll Tracking Plugin
*
* Copyright 2011, Cardinal Path and Direct Performance
* Licensed under the GPLv3 license.
*
* @author Eduardo Cereto <eduardocereto@gmail.com>
*/
var _maxScrollOpts;
/**
* Get current browser viewpane heigtht
*
* @return {number} height.
*/
function _get_window_height() {
return window.innerHeight || documentElement.clientHeight ||
document.body.clientHeight || 0;
}
/**
* Get current absolute window scroll position
*
* @return {number} YScroll.
*/
function _get_window_Yscroll() {
return window.pageYOffset || document.body.scrollTop ||
documentElement.scrollTop || 0;
}
/**
* Get current absolute document height
*
* @return {number} Current document height.
*/
function _get_doc_height() {
return Math.max(
document.body.scrollHeight || 0, documentElement.scrollHeight || 0,
document.body.offsetHeight || 0, documentElement.offsetHeight || 0,
document.body.clientHeight || 0, documentElement.clientHeight || 0
);
}
/**
* Get current vertical scroll percentage
*
* @return {number} Current vertical scroll percentage.
*/
function _get_scroll_percentage() {
return (
(_get_window_Yscroll() + _get_window_height()) / _get_doc_height()
) * 100;
}
var _t = null;
var _max_scroll = 0;
function _update_scroll_percentage(now) {
if (_t) {
clearTimeout(_t);
}
if (now === true) {
_max_scroll = Math.max(_get_scroll_percentage(), _max_scroll);
return;
}
_t = setTimeout(function () {
_max_scroll = Math.max(_get_scroll_percentage(), _max_scroll);
}, 400);
}
function _sendMaxScroll() {
_update_scroll_percentage(true);
_max_scroll = Math.floor(_max_scroll);
if (_max_scroll <= 0 || _max_scroll > 100) return;
var bucket = (_max_scroll > 10 ? 1 : 0) * (
Math.floor((_max_scroll - 1) / 10) * 10 + 1
);
bucket = String(bucket) + '-' +
String(Math.ceil(_max_scroll / 10) * 10);
_gas.push(['_trackEvent',
_maxScrollOpts['category'],
url,
bucket,
Math.floor(_max_scroll),
true // non-interactive
]);
}
/**
* Tracks the max Scroll on the page.
*
* @param {object} opts GAS Options to be used.
* @this {GasHelper} The Ga Helper object
*/
function _trackMaxScroll(opts) {
if (!this._maxScrollTracked) {
this._maxScrollTracked = true;
} else {
//Oops double tracking detected.
return;
}
_maxScrollOpts = opts || {};
_maxScrollOpts['category'] = _maxScrollOpts['category'] || 'Max Scroll';
this._addEventListener(window, 'scroll', _update_scroll_percentage);
this._addEventListener(window, 'beforeunload', _sendMaxScroll);
}
_gas.push(['_addHook', '_gasTrackMaxScroll', _trackMaxScroll]);
// Old API to be deprecated on v2.0
_gas.push(['_addHook', '_trackMaxScroll', _trackMaxScroll]);