[Facebook Pixel] – Ghi lại độ sâu cuộn: 10%, 30%, 50% và 90%

Ngày đăng: 20/08/2020 07:42 Sáng

[Facebook Pixel] – Ghi lại độ sâu cuộn trang: 10%, 30%, 50% và 90%

function getCurrentPosition() {
    return window.pageYOffset ||
        (document.documentElement || document.body.parentNode || document.body).scrollTop;
}
function getScrollableHeight() {
    var d = Math.max(
        document.body.scrollHeight, document.documentElement.scrollHeight,
        document.body.offsetHeight, document.documentElement.offsetHeight,
        document.body.clientHeight, document.documentElement.clientHeight
    )
    var w = window.innerHeight ||
        (document.documentElement || document.body).clientHeight;
    if (d > w) {
        return d - w;
    }
    return 0; // not scrollable
}
var checkPoints = [10, 30, 50, 70, 90];
var reached = 0;
var scrollableHeight = getScrollableHeight();

window.addEventListener('resize', function () {
    scrollableHeight = getScrollableHeight();
});
window.addEventListener('scroll', function () {
    var current;
    if (scrollableHeight == 0) {
        current = 100;
    } else {
        var current = getCurrentPosition() / scrollableHeight * 100;
    }
    if (current > reached) {
        reached = current;
        // checkpoint and send events
        while (checkPoints.length > 0) {
            var c = checkPoints[0];
            if (c <= reached) {
                checkPoints.shift();
                fbq('trackCustom', 'ViewContentCheckPoint', {
                    depth: c,
                });
            } else {
                break;
            }
        }
    }
}, false);

Ví dụ ở trên kiểm tra độ sâu cuộn trên trình duyệt và sẽ kích hoạt sự kiện tùy chỉnh ViewContentCheckpoint nếu vượt quá giá trị điểm kiểm tra, 10%, 30%, 50% và 90%. Điều này cho phép bạn phân tích dữ liệu theo phần trăm độ sâu cuộn.

5/5 - (1 bình chọn)