-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll-depth-jquery
56 lines (46 loc) · 1.93 KB
/
scroll-depth-jquery
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
// needs to be updated to factor in window re-sizes. Will sort later.
jQuery(function($) {
var longPage = document.querySelector('.row .col-md-8') ? document.querySelector('.row .col-md-8') : undefined;
// checks to see if it's 8/4 template which usually has multiple H2 tags - was planned to be targeted at specific pages.
if (typeof(longPage) !== 'undefined') {
var currentmax = 0;
// how far you've gone down page.
var ftimer = 0;
var callBackTime = 100;
// buffer to avoid spam
var test = $('article h2').map(function() {
return {
"offset": ($(this).offset().top - $('header .primary-nav').height()),
"label": $(this).text()
};
});
// gathering co-ords and headings.
var headings = jQuery.map(test, function(value) {
return value.label;
});
// array of headings
var positions = jQuery.map(test, function(value) {
return value.offset;
});
// array of y co-ords
var bottom = $(window).height() + $(window).scrollTop() - $('header .primary-nav').height();
// current position
function trackHeading() {
bottom = $(window).height() + $(window).scrollTop() - $('header .primary-nav').height();
while (bottom > positions[currentmax]) {
dataLayer.push({"heading" : headings[currentmax], "event" : "scrollheading", "headingdepth" : currentmax + 1 });
//above pushes a custom event to GTM's dataLayer. A custom event trigger would need to be setup to listen for the "scrollheading event"
console.log(headings[currentmax] + ' (' + (currentmax + 1) + ')');
// for debugging ^
currentmax++;
}
}
$(window).scroll(function() {
if (ftimer) {
clearTimeout(ftimer);
}
ftimer = setTimeout(trackHeading, callBackTime);
// as you scroll down, re-checks the position and outputs. Would create a resize function for live. Unnecessary.
});
}
});