Last active
November 7, 2018 10:29
-
-
Save fergusKe/2a7406f56d4ce9c255177ff64dabbeb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var dataLayer = window.dataLayer || []; | |
var eventString = 'cusevent'; | |
var eventCategoryString = '張曼娟活動頁'; | |
var areaIndex = 0; | |
var areaIndexArr = []; | |
$(window).scroll(function() { | |
var windowHeight = $(window).height(); | |
var scrollPosition; | |
$('header, .area2, .area3').each(function (i) { | |
var $this = $(this); | |
var thisTop = $this.offset().top; | |
var winHalfHeight = windowHeight / 2; | |
var scrollTop = $(window).scrollTop(); | |
scrollPosition = scrollTop + windowHeight; | |
// 滑到超過螢幕的一半 | |
if (scrollPosition >= thisTop + winHalfHeight) { | |
areaIndex = i; | |
} | |
}); | |
// 還沒有滑到過的區域 | |
if (areaIndexArr.indexOf(areaIndex) === -1) { | |
areaIndexArr.push(areaIndex); | |
var areaName = ''; | |
switch(areaIndex) { | |
case 0: | |
areaName = '1.文學繪本'; | |
break; | |
case 1: | |
areaName = '2.歷年著作'; | |
break; | |
case 2: | |
areaName = '3.文學學堂'; | |
break; | |
default: | |
areaName = ''; | |
} | |
pushDatalayer('滾軸事件', areaName); | |
} | |
}).scroll(); | |
function pushDatalayer(thisAction, thisLabel) { | |
dataLayer.push({ | |
event: eventString, | |
eventCategory: eventCategoryString, | |
eventAction: thisAction, | |
eventLabel: thisLabel | |
}); | |
} | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment