Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery捕获站点范围的文件下载_Javascript_Jquery_Regex_Google Analytics_Tracking - Fatal编程技术网

Javascript 使用jQuery捕获站点范围的文件下载

Javascript 使用jQuery捕获站点范围的文件下载,javascript,jquery,regex,google-analytics,tracking,Javascript,Jquery,Regex,Google Analytics,Tracking,我正在尝试使用跟踪我网站上的下载,因此任何已下载的.JPGs、.PNGs、.PDFs等,我都希望通过执行一些跟踪代码来跟踪 显然有些东西工作不正常,因为我似乎无法启动\u trackEvent 这是驻留在gaAddons插件中的JavaScript /////////////////// // _trackDownloads jQuery(document).ready(function($) { // helper function - allow regex as jQuery se

我正在尝试使用跟踪我网站上的下载,因此任何已下载的.JPGs、.PNGs、.PDFs等,我都希望通过执行一些跟踪代码来跟踪

显然有些东西工作不正常,因为我似乎无法启动
\u trackEvent

这是驻留在gaAddons插件中的JavaScript

///////////////////
// _trackDownloads
jQuery(document).ready(function($) {
    // helper function - allow regex as jQuery selector
    $.expr[':'].regex = function(e, i, m) {
        var mP = m[3].split(','),
            l = /^(data|css):/,
            a = {
                method: mP[0].match(l) ? mP[0].split(':')[0] : 'attr',
                property: mP.shift().replace(l, '')
            },
            r = new RegExp(mP.join('').replace(/^\s+|\s+$/g, ''), 'ig');
        return r.test($(e)[a.method](a.property));
    };

    $('a:regex(href,"\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*)")$').live('click', function(e) {
        _gaq.push(['_trackEvent', 'download', 'click', this.href.replace(/^.*\/\//, '')]);
    });
});
我有上面的代码包括在页面和我的简单锚链接到下面的PDF文件

<a href="http://www.ayrshireminis.com/downloads/Files/pdfs/turnberry.pdf" target="blank">DOWNLOAD</a>


JavaScript是否有问题,或者是否有更简单的方法来检查jpg/png/pdf文件下载?我可能会忽略zip/ppt/tiff文件,因为它们不会出现在网站上。

我已经搜索了几个小时,发现了这个解决方案,希望这对其他人有所帮助:

jQuery(document).ready(function($) {
    var filetypes = /\.(zip|exe|pdf|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(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;
                    }
                });
            }
        });
});

嘿,这太好了。这是很久以前的事了,但你还记得这段代码的来源吗?@JakeB。对不起,杰克。我不。我不久前完成了那个项目,但我记得这个代码对我来说很好。没问题,只是好奇而已!我正在尝试一下,看起来正是我所需要的。谢谢