在jquery中调用滚动事件中的谷歌分析

calling Google analytics in scroll event in jquery

本文关键字:谷歌 滚动 jquery 调用 事件      更新时间:2023-09-26

我有以下代码在我的主页谷歌分析。

<script type="text/javascript">
(function (i, s, o, g, r, a, m) {
    i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
        (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date(); a = s.createElement(o),
    m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', '@System.Configuration.ConfigurationManager.AppSettings["GoogleAnalyticsID"]', 'auto');
ga('send', 'pageview');
</script>

我需要在js中调用谷歌分析滚动事件如何调用谷歌分析功能滚动事件?你能帮我解决这个问题吗?div ?

一个在我公司工作的人开发了一个与Google Analytics Classic一起使用的库,我采用了Track Scroll插件并将其改编为fire to Universal Google Analytics,这里是代码:(如果您有任何疑问,请问我)

/**
 * GAS - Google Analytics on Steroids
 *
 * Max-Scroll Tracking Plugin
 *
 * Copyright 2011, Cardinal Path and Direct Performance
 * Licensed under the GPLv3 license.
 *
 * @author Eduardo Cereto <eduardocereto@gmail.com>
 */
var _maxScrollOpts;
/**
 * Get current browser viewpane heigtht
 *
 * @return {number} height.
 */
function _get_window_height() {
    return window.innerHeight || documentElement.clientHeight ||
        document.body.clientHeight || 0;
}
/**
 * Get current absolute window scroll position
 *
 * @return {number} YScroll.
 */
function _get_window_Yscroll() {
    return window.pageYOffset || document.body.scrollTop ||
        documentElement.scrollTop || 0;
}
/**
 * Get current absolute document height
 *
 * @return {number} Current document height.
 */
function _get_doc_height() {
    return Math.max(
        document.body.scrollHeight || 0, documentElement.scrollHeight || 0,
        document.body.offsetHeight || 0, documentElement.offsetHeight || 0,
        document.body.clientHeight || 0, documentElement.clientHeight || 0
    );
}
/**
 * Get current vertical scroll percentage
 *
 * @return {number} Current vertical scroll percentage.
 */
function _get_scroll_percentage() {
    return (
        (_get_window_Yscroll() + _get_window_height()) / _get_doc_height()
    ) * 100;
}
var _t = null;
var _max_scroll = 0;
function _update_scroll_percentage(now) {
    if (_t) {
        clearTimeout(_t);
    }
    if (now === true) {
        _max_scroll = Math.max(_get_scroll_percentage(), _max_scroll);
        return;
    }
    _t = setTimeout(function () {
        _max_scroll = Math.max(_get_scroll_percentage(), _max_scroll);
    }, 400);
}
function _sendMaxScroll() {
    _update_scroll_percentage(true);
    _max_scroll = Math.floor(_max_scroll);
    if (_max_scroll <= 0 || _max_scroll > 100) return;
    var bucket = (_max_scroll > 10 ? 1 : 0) * (
        Math.floor((_max_scroll - 1) / 10) * 10 + 1
    );
    bucket = String(bucket) + '-' +
        String(Math.ceil(_max_scroll / 10) * 10);
    ga('send',
        'event',
        _maxScrollOpts['category'],
        url,
        bucket,
        Math.floor(_max_scroll),
        true // non-interactive
    );
}
/**
 * Tracks the max Scroll on the page.
 *
 * @param {object} opts GAS Options to be used.
 * @this {GasHelper} The Ga Helper object
 */
function _trackMaxScroll(opts) {
    if (!this._maxScrollTracked) {
        this._maxScrollTracked = true;
    } else {
        //Oops double tracking detected.
        return;
    }
    _maxScrollOpts = opts || {};
    _maxScrollOpts['category'] = _maxScrollOpts['category'] || 'Max Scroll';
    this._addEventListener(window, 'scroll', _update_scroll_percentage);
    this._addEventListener(window, 'beforeunload', _sendMaxScroll);
}
_trackMaxScroll();