-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_page_app.js
131 lines (116 loc) · 3.53 KB
/
single_page_app.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
/**
* Scroll tracking plugin for GTM
* Description - this code was derived from the Google On Steroid plugin: https://github.com/CardinalPath/gas/blob/master/src/plugins/max_scroll.js
* Author - Bill Tripple, [email protected]
* Version - 1.1
* Modified by: Ahmed Awwad
*/
//(function(){
window.CP_ScrollTracking = (function () {
/* configuration start */
var SETTINGS={
"scroll_grouping":"25",
"data_layer_event_name":"scroll",
"data_layer_event_param1_name":"percent_scrolled"
}
/* configuration ends */
var documentElement = document.documentElement;
var _current_bucket=0;
var _max_scroll = 0;
// add event listener function
function addListener( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else {
obj.addEventListener( type, fn, false );
}
}
// remove event listener function
function removeListener( obj, type, fn ) {
if ( obj.detachEvent ) {
obj.detachEvent( 'on'+type, obj[type+fn] );
obj[type+fn] = null;
} else {
obj.removeEventListener( type, fn, false );
}
}
/**
* Get current browser viewpane height
*
* @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;
}
function _resetScroll(){
_current_bucket=0;
_max_scroll=0
}
function _update_scroll_percentage() {
_max_scroll = Math.max(_get_scroll_percentage(), _max_scroll);
var bucket = (_max_scroll > SETTINGS.scroll_grouping ? 1 : 0) * (Math.floor((_max_scroll) / SETTINGS.scroll_grouping) * SETTINGS.scroll_grouping);
if(_max_scroll>95){bucket=100;} // close enough
if(bucket>_current_bucket){
//console.log("bucket"+bucket);
_current_bucket=_max_scroll;
if(bucket==100){
removeListener( window, "scroll", _update_scroll_percentage )
}
if (typeof(dataLayer) !== 'undefined') {
var dL ={};
dL.event = SETTINGS.data_layer_event_name;
dL[SETTINGS.data_layer_event_param1_name] = bucket;
dataLayer.push(dL)
//_satellite.track("scroll",{"bucket":bucket});
}
}
}
return{ // public interface
init: function(){
addListener( window, "scroll", _update_scroll_percentage );
return true;
},
newPage:function(){
_resetScroll();
addListener( window, "scroll", _update_scroll_percentage );
}
}
})();
try{
CP_ScrollTracking.init();
}catch(ex){console.log(ex);}
//})();