修改跟踪器以使用ga.js和analytics.js

时间:2017-03-29 03:47:00

标签: javascript google-analytics

我正在修改ga.js的现有文档和出站链接跟踪器,以便与analytics.js一起使用,以便将其添加到我们的新旧网站中。

我添加了2个IF语句但不确定我是否已正确转换它(尤其是href和'b._trackEvent'参数)。这是我的尝试:

function docLinkTracking(thing){
    href = $(thing).attr('href');
    if (href) {
        var href_lower = href.toLowerCase();
        var filetypes = /\.(zip|exe|pdf|doc.?|xls.?|ppt.?|mp3|mp4|flv|txt|csv|vsd)$/i;
        var file_extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : 'undefined';
        file_extension = file_extension.toString();

        //track documents links
        if (href.match(filetypes)) {
            //my guesses..
            if (typeof ga !== 'undefined') {
                ga('send', 'event', 'Downloads', file_extension, {'nonInteraction': href});
                ga('send', 'b.event', 'Downloads', file_extension, {'nonInteraction': href});
            }
            //original
            if (typeof _gaq !== 'undefined') {
                _gaq.push(['_trackEvent', 'Downloads', file_extension, href]);
                _gaq.push(['b._trackEvent', 'Downloads', file_extension, href]);
            }
        }

        //track outbound links
        if (href_lower.substr(0, 4) == "http") {
            var domain = document.domain.replace("www.", '');
            if (href_lower.indexOf(domain) == -1) {
                href = href.replace("http://", '');
                href = href.replace("https://", '');
                //my guesses..
                if (typeof ga !== 'undefined') {
                    ga('send', 'event', 'Outbound Traffic', {'nonInteraction': href});
                    ga('send', 'b.event', 'Outbound Traffic', {'nonInteraction': href});
                }
                //original
                if (typeof _gaq !== 'undefined') {                    
                    _gaq.push(['_trackEvent', 'Outbound Traffic', href]);
                    _gaq.push(['b._trackEvent', 'Outbound Traffic', href]);
                }
        }
    }
}

请帮我纠正'// my猜测..'块,因为我需要传递此代码才能实现。

0 个答案:

没有答案