javascript - Google Analytics trackEvent 内部链接无法正常工作

标签 javascript jquery google-analytics

我正在使用 ga.js。我的代码正在记录一些内部链接,但在我测试过的页面上,内部链接显示的事件总数为 1,而实际上应该是 50。我还查看了事件中的实时报告。我所有的外在都会立即显现出来,但我的内在却没有。我不明白为什么除了内部链接之外一切都有效。我的代码如下,

        <script type="text/javascript">
            if (typeof jQuery != 'undefined') {
                jQuery(document).ready(function ($) {
                    var pdffiletype = /\.(pdf)$/i;
                    var filetypes = /\.(zip|exe|doc*|xls*|ppt*|mp3)$/i;
                    var baseHref = '';
                    if (jQuery('base').attr('href') != undefined)
                        baseHref = jQuery('base').attr('href');
                    jQuery('a').each(function () {
                        var href = jQuery(this).attr('href');
                        if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                            jQuery(this).click(function () {
                                var extLink = href.replace(/^https?\:\/\//i, '');
                                _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
                                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                                    setTimeout(function () { location.href = href; }, 200);
                                    return false;
                                }
                            });
                        }
                        else if (href && href.match(/^javascript\:/i)) {
                            //do not track
                            return true;
                        }
                        else if (href && href.match(/^mailto\:/i)) {
                            jQuery(this).click(function () {
                                var mailLink = href.replace(/^mailto\:/i, '');
                                _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
                            });
                        }
                        else if (href && href.match(pdffiletype)) {
                            jQuery(this).click(function () {
                                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                                var filePath = href;
                                _gaq.push(['_trackEvent', 'PDF', 'Click', $(this).html(), 0]);
                                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                                    setTimeout(function () { location.href = baseHref + href; }, 200);
                                    return false;
                                }
                            });
                        }
                        else if (href && href.match(filetypes)) {
                            jQuery(this).click(function () {
                                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                                var filePath = href;
                                _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                                    setTimeout(function () { location.href = baseHref + href; }, 200);
                                    return false;
                                }
                            });
                        }
                        else if (href) {
                            //assume internal link

                            _gaq.push(['_trackEvent', 'Internal', 'Click', href]);
                        }

                    });
                });
            }
        </script>

最佳答案

只需更改此 ligne : _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);到 ga('发送', '事件', '下载', '点击', filePath); 它再次为我工作

关于javascript - Google Analytics trackEvent 内部链接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295149/

相关文章:

javascript - 将下拉文本长度分解为下一行

javascript - 填充 Dojo AccordionContainer

javascript - Momentjs解析通配符

javascript - 使用Jquery选择相同类型元素中的元素

javascript - window.setInterval() 不会立即加载 div 内容并中断 ColdFusion sessionTimeout

javascript - 想要将 AJAX 时事通讯注册跟踪为 Google Analytics 事件

google-analytics - 使用 CSP nonce 加载 Google Analytics js 文件

javascript - 如何在 React VR 项目中实现 Google Analytics 事件跟踪?

javascript - 尝试将 CSS 注入(inject)网页而不是将 CSS 注入(inject)自身的扩展

javascript - jQuery setinterval 函数更改 css