javascript 使用dataLayer的Google Analytics滚动跟踪代码段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 使用dataLayer的Google Analytics滚动跟踪代码段相关的知识,希望对你有一定的参考价值。

// https://github.com/felix/gtm-scrolldepth
var startTime = +new Date;
var scrollCache = [];
documentElement = document.documentElement;

function throttle(func, wait) {
    var context, args, result;
    var timeout = null;
    var previous = 0;
    var later = function () {
        previous = new Date;
        timeout = null;
        result = func.apply(context, args);
    };
    return function () {
        var now = new Date;
        if (!previous) previous = now;
        var remaining = wait - (now - previous);
        context = this;
        args = arguments;
        if (remaining <= 0) {
            clearTimeout(timeout);
            timeout = null;
            previous = now;
            result = func.apply(context, args);
        } else if (!timeout) {
            timeout = setTimeout(later, remaining);
        }
        return result;
    };
}

function calculateMarks(docHeight) {
    return {
        '20%': parseInt(docHeight * 0.20, 10),
        '40%': parseInt(docHeight * 0.50, 10),
        '60%': parseInt(docHeight * 0.60, 10),
        '80%': parseInt(docHeight * 0.80, 10),
        '100%': docHeight - 5
    };
}

function checkMarks(marks, scrollDistance, timing) {
    // Check each active mark
    for (key in marks) {
        if (scrollCache.indexOf(key) === -1 && scrollDistance >= marks[key]) {
            
            dataLayer.push({
                'event': 'scrollDepth',
                'eventCategory': 'Scroll Depth',
                'eventAction': 'Percentage', 
                'eventLabel': key, 
                'eventTiming': timing,
            })
            scrollCache.push(key);
            sendEvent('Percentage', key, scrollDistance, timing);
        }
    }
}

function rounded(scrollDistance) {
    // Returns String
    return (Math.floor(scrollDistance / 250) * 250).toString();
}

function getDocumentHeight() {
    return Math.max(
        document.body.scrollHeight || 0, documentElement.scrollHeight || 0,
        document.body.offsetHeight || 0, documentElement.offsetHeight || 0,
        document.body.clientHeight || 0, documentElement.clientHeight || 0
    );
}

function getScrollDistance() {
    return window.pageYOffset || document.body.scrollTop ||
        documentElement.scrollTop || 0;
}

function getWindowHeight() {
    return window.innerHeight || documentElement.clientHeight ||
        document.body.clientHeight || 0;
}

window.onscroll = throttle(function () {
    var docHeight = getDocumentHeight(),
        winHeight = getWindowHeight(),
        scrollDistance = getScrollDistance() + winHeight,

        // Recalculate percentage marks
        marks = calculateMarks(docHeight),

        // Timing
        timing = +new Date - startTime;

        // If all marks already hit, unbind scroll event
        if (scrollCache.length >= 5) {
            //$window.off('scroll.scrollDepth');
            return;
        }

    checkMarks(marks, scrollDistance, timing);

}, 500);

以上是关于javascript 使用dataLayer的Google Analytics滚动跟踪代码段的主要内容,如果未能解决你的问题,请参考以下文章

postMessage 未推入 dataLayer

连接显示为null,磨损未从DataLayer接收

html Danske Spil - 示例CoreTag - Vegas.dk - 使用dataLayer

尽管正在执行DataLayer推送,但未触发Google跟踪代码管理器代码

GTM“dataLayer.push”是从前端开发人员更新数据层的唯一方法吗?

Caffe Layers Code Analysis-DataLayer