我有一行Javascript可触发Facebook像素自定义事件,以了解用户在网页上滚动的距离。其中一种代码可实现50%滚动并正确触发像素。
我尝试修改代码以25%,50%,75%和100%滚动触发Facebook像素,但无法使其触发任何事件。我在下面放了工作代码和非工作修改代码。
<script type = "text/javascript" >
/*
This code works for firing a Facebook Custom Event "SCROLLED 50 PERCENT" when someone scrolls 50% of a web page
*/
function getScrollPercent() {
var o = document.documentElement,
r = document.body,
e = "scrollHeight",
t = (o.scrollTop || r.scrollTop) / ((o[e] || r[e]) - o.clientHeight) * 100;
return Math.round(t)
}
var scrollFired = !1,
props = {
pm_path: window.location.pathname
};
window.addEventListener("scroll", function(o) {
getScrollPercent() >= 50 && !scrollFired && (fbq("trackCustom", "SCROLLED 50 PERCENT", props), scrollFired = !0)
});
</script>
<script type="text/javascript">
/*
This the modified code to fire multiple scroll events, but cannot get it to fire any scroll events.
*/
function getScrollPercent(){
var o=document.documentElement,
r=document.body,
e="scrollHeight",
t=(o.scrollTop||r.scrollTop)/((o[e]||r[e])-o.clientHeight)*100;
return Math.round(t);
var tracker = window.ScrollTracker();
tracker.on({
percentages: {
every: [25] // Tracks 25%, 50%, 75%, and 100%
}
}, handler);}
var scrollFired=!1,
props={pm_path:window.location.pathname};
if(getScrollPercent()>=25){
window.addEventListener("scroll",function(o){
getScrollPercent()>=25&&!scrollFired&&(fbq("trackCustom","SCROLLED 25 PERCENT",props),scrollFired=!0)});
}
else if(getScrollPercent()>=50){
window.addEventListener("scroll",function(o){
getScrollPercent()>=50&&!scrollFired&&(fbq("trackCustom","SCROLLED 50 PERCENT",props),scrollFired=!0)});
}
else if(getScrollPercent()>=75){
window.addEventListener("scroll",function(o){
getScrollPercent()>=75&&!scrollFired&&(fbq("trackCustom","SCROLLED 75 PERCENT",props),scrollFired=!0)});
}
else if(getScrollPercent()>=100){
window.addEventListener("scroll",function(o){
getScrollPercent()>=100&&!scrollFired&&(fbq("trackCustom","SCROLLED 100 PERCENT",props),scrollFired=!0)});
}
$rootScope.$on('$routeChangeSuccess', function() {
$scrollTracker.reset();
});
</script>
我希望代码触发25%,50%,75和100%滚动深度的Facebook自定义事件。