向Google跟踪代码管理器添加自定义html时出错

时间:2017-02-08 07:34:17

标签: javascript google-tag-manager

我真的不是JS,你能告诉我这段代码有什么问题吗?我试图添加这个但是出现错误“第74行出错,第3行:解析错误。”)'预期'。我真的不知道要修理什么。

<script id="gtm-scroll-tracking" type="text/javascript">
    ; (function (document, window, config) {
        // Browser dependencies, script fails silently
        if (!document.querySelector || !document.body.getBoundingClientRect) {
            return false;
        }
        // Get our dataLayer ready, in case we're not in GTM or we've got a special name
        var dataLayerName = config.dataLayerName || 'dataLayer';
        var dataLayer = window[dataLayerName] || (window[dataLayerName] = []);
        var cache = {};
        // Initialize our distances, for later
        config.distances = config.distances || {};
        checkDepth();
        addEvent(window, 'scroll', throttle(checkDepth, 500));
        function getMarks(_docHeight, _offset) {
            var marks = {};
            var percents = [];
            var pixels = []
            if (config.distances.percentages) {
                if (config.distances.percentages.each) {
                    percents = percents.concat(config.distances.percentages.each);
                }
                if (config.distances.percentages.every) {
                    var _every = every_(config.distances.percentages.every, 100);
                    percents = percents.concat(_every);
                }
            }
            if (config.distances.pixels) {
                if (config.distances.pixels.each) {
                    pixels = pixels.concat(config.distances.pixels.each);
                }
                if (config.distances.pixels.every) {
                    var _every = every_(config.distances.pixels.every, _docHeight);
                    pixels = pixels.concat(_every);
                }
            }
            marks = addMarks_(marks, percents, '%', _docHeight, _offset);
            marks = addMarks_(marks, pixels, 'px', _docHeight, _offset);
            return marks;
        }
        function addMarks_(marks, points, symbol, _docHeight, _offset) {
            var i;
            for (i = 0; i < points.length; i++) {
                var _point = parseInt(points[i], 10);
                var height = symbol !== '%' ? _point + _offset : _docHeight *
                    (_point / 100) + _offset;
                var mark = _point + symbol;
                if (height <= _docHeight + _offset) { marks[mark] = height; }
            }
            return marks;
        }
        function every_(n, total) {
            var n = parseInt(n, 10);
            var _num = total / n;
            var arr = [];
            for (i = 1; i < _num + 1; i++) { arr.push(i * n); }
            return arr;
        }
        function checkDepth() {
            var _bottom = parseBorder_(config.bottom);
            var _top = parseBorder_(config.top);
            var height = docHeight(_bottom, _top);
            var marks = getMarks(height, (_top || 0));
            var _curr = currentPosition();
            for (key in marks) {
                if (_curr > marks[key] && !cache[key]) {
                    cache[key] = true;
                    fireAnalyticsEvent(key);
                }
            }
        }
        function fireAnalyticsEvent(distance) {
            dataLayer.push({
                'event': 'scrollTracking',
                'attributes': { 'distance': distance }
            });
        }
    }
    function parseBorder_(border) {
        if (typeof border === 'Number' || parseInt(border, 10)) {
            return parseInt(border, 10);
        }
        try {
            // If we have an element or a query selector, poll getBoundingClientRect
            var el = border.nodeType && border.nodeType === 1 ? border :
                document.querySelector(border);
            var docTop = document.body.getBoundingClientRect().top;
            var _elTop = Math.floor(el.getBoundingClientRect().top - docTop);
            return _elTop;
        } catch (e) { return void (0); }
    }
    // Adapted from
    https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY
    function currentPosition() {
        var supportPageOffset = window.pageXOffset !== undefined;
        var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
        var currScrollTop = supportPageOffset ?
            window.pageYOffset :
            isCSS1Compat ?
                document.documentElement.scrollTop :
                document.body.scrollTop;
        return parseInt(currScrollTop, 10) + parseInt(viewportHeight(), 10);
    }
    function viewportHeight() {
        var elem = (document.compatMode === "CSS1Compat") ?
            document.documentElement :
            document.body;
        return elem.clientHeight;
    }
    function docHeight(_bottom, _top) {
        var body = document.body;
        var html = document.documentElement;
        var height = Math.max(body.scrollHeight, body.offsetHeight,
            html.clientHeight, html.scrollHeight, html.offsetHeight);
        if (_top) { height = height - _top; }
        if (_bottom) { height = _bottom - _top; }
        return height - 5;
    }
    /*
     * Throttle function borrowed from:
     * Underscore.js 1.5.2
     * http://underscorejs.org
     *
  (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative
  Reporters & Editors
     * Underscore may be freely distributed under the MIT license.
     */
    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;
        };
    }
    // Cross-browser compliant event listener
    function addEvent(el, evt, fn) {
        if (el.addEventListener) { return el.addEventListener(evt, fn); }
        if (el.attachEvent) {
            return el.attachEvent('on' + evt, function (evt) {
                // Call the event to ensure uniform 'this' handling, pass it event
                fn.call(el, evt);
            });
        }
        if (typeof el['on' + evt] === 'undefined' || el['on' + evt] === null) {
            return el['on' + evt] = function (evt) {
                // Call the event to ensure uniform 'this' handling, pass it event
                fn.call(el, evt); \
            }
        }
    }
    })(document, window,
        {
            // False if you just use the default dataLayer variable, otherwise enter it here
            'dataLayerName': false,
            'distances': {
                // Configure percentages of page you'd like to see if users scroll past
                'percentages': {
                    'each': [10, 90],
                    'every': 25
                },
                // Configure for pixel measurements of page you'd like to see if users scroll past
                'pixels': {
                    'each': [],
                    'every': null
                }
            },
            // Accepts a number, DOM element, or query selector to determine the top of the scrolling area
            'top': null,
            // Accepts a number, DOM element, or query selector to determine the bottom of the scrolling area
            'bottom': null,
        });
</script>

在尝试预览(调试)时,该工具告诉我:

  

第74行出错,第3行:解析错误。 ')'预期

0 个答案:

没有答案