<commit_before>﻿/*
 * Google Analytics Download Tracker
 * 
 * GADT sends virtual pageviews to Google Analytics when users download files.
 * 
 * Copyright (c) 2016 by <NAME> (https://basementmedia.no)
 * 
 * The pageviews use the actual url to the document, so you will find for example this
 * file http://mysite.com/files/document.docx in Google Analytics under Behaviour → 
 * Site Content → Content Drilldown → /files/
 * 
 * The advantage to using virtual pageviews over events, is that the statistics for how 
 * many people are downloading a file is easily compared to how many people are 
 * viewing a page on your site.
 */

(function () {

    var gadt = new (function () {

        // Filetypes to track
        var filetypes = /\.pdf$|\.zip$|\.od*|\.doc*|\.xls*|\.ppt*|\.exe$|\.dmg$|\.mp4$|\.mov$|\.avi$|\.mp3$/i;

        var track = function (event, link) {

            // Is Google Analytics loaded?
            if (typeof (window.ga) === "undefined" || window.ga === null) {
                console.log("Google Analytics is not loaded, cannot track download of file: " + link.pathname);
                return;
            }

            var filename = link.pathname;

            // Is this a link to a file?
            if (filename.search(filetypes) === -1) {
                return;
            }

            // Remove everything before the last slash in the path
            filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length);

            if (filename.length === 0) {
                return;
            }

            // Prevent the browser from downloading the file before
            // Google Analytics has had time to track it
            event.preventDefault();

            // Send virtual pageview to Google Analytics
            ga("send",
            {
                hitType: "pageview",
                page: link.pathname,
                title: filename,
                hitCallback: function () {
                    // When Google Analytics has tracked the download, 
                    // download file
                    console.log("Google Analytics tracked download of file: " + link.pathname);
                    document.location.href = link.href;
                }
            });

            // Send event to Google Analytics
            // (fallback if virtual pageview doesn't work)
            ga("send",
            {
                hitType: "event",
                eventCategory: "Downloaded file",
                eventAction: filename,
                eventLabel: link.pathname
            });

            // Wait 3 seconds, and if Google Analytics hasn't responded,
            // download file anyway
            setTimeout(function () {
                console.log("Google Analytics didn't respond, downloading file anyway: " + link.pathname);
                document.location.href = link.href;
            }, 3000);
        }

        var init = function () {
            //    for (var i = 0, l = links.length; i < l; i++) {
            //        //Compare the fileType to the whitelist
            //        var match = links[i].pathname.match(whitelist);

            //        //If the link is for a file download . . .
            //        if (typeof match !== "undefined" && match !== null) {
            //            //Call Entourage whenever the link is clicked
            //            links[i].onclick = autograph;
            //        }
            //    }
        }

        return {
            init: init,
            track: track
        }
    });

    // Add tracker to global scope
    window.gadt = gadt;

    // Run scripts after page is loaded
    //window.onload = gadt.init;
})();
<commit_msg>Adding tracking script automatically to all links on page. Added some more valid filetypes.<commit_after>﻿/*
 * Google Analytics Download Tracker
 * 
 * GADT sends virtual pageviews to Google Analytics when users download files.
 * 
 * Copyright (c) 2016 by <NAME> (https://basementmedia.no)
 * 
 * The pageviews use the actual url to the document, so you will find for example this
 * file http://mysite.com/files/document.docx in Google Analytics under Behaviour → 
 * Site Content → Content Drilldown → /files/
 * 
 * The advantage to using virtual pageviews over events, is that the statistics for how 
 * many people are downloading a file is easily compared to how many people are 
 * viewing a page on your site.
 */

(function () {

    var gadt = new (function () {

        // Filetypes to track
        var filetypes = /\.pdf$|\.zip$|\.od*|\.doc*|\.xls*|\.ppt*|\.exe$|\.dmg$|\.mp\d$|\.mov$|\.avi$|\.gif$|\.png$|\.wav$|\.jpg$|\.jpeg$|\.ogg$/i;

        var track = function (event) {

            // This script only works if it is called from a link <a> with a href attribute
            var link = this;
            if (link.href === null || link.href.length === 0) {
                return;
            }

            // Is Google Analytics loaded?
            if (typeof (ga) === "undefined" || ga === null || ga.loaded !== true) {
                console.log("Google Analytics is not loaded, cannot track download of file: " + link.pathname);
                return;
            }

            var filename = link.pathname;

            // Is this a link to a file?
            if (filename.search(filetypes) === -1) {
                return;
            }

            // Remove everything before the last slash in the path
            filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length);

            if (filename.length === 0) {
                return;
            }

            // Prevent the browser from downloading the file before
            // Google Analytics has had time to track it
            event.preventDefault();

            // Send virtual pageview to Google Analytics
            ga("send",
            {
                hitType: "pageview",
                page: link.pathname,
                title: filename,
                hitCallback: function () {
                    // When Google Analytics has tracked the download, 
                    // download file
                    console.log("Google Analytics tracked download of file: " + link.pathname);
                    document.location.href = link.href;
                }
            });

            // Send event to Google Analytics
            // (fallback if virtual pageview doesn't work)
            ga("send",
            {
                hitType: "event",
                eventCategory: "Downloaded file",
                eventAction: filename,
                eventLabel: link.pathname
            });

            // Wait 3 seconds, and if Google Analytics hasn't responded,
            // download file anyway
            setTimeout(function () {
                console.log("Google Analytics didn't respond, cannot track download of file: " + link.pathname);
                document.location.href = link.href;
            }, 3000);
        }

        var init = function () { 

            var links = document.links;
            var linksLength = links.length;

            for (var i = 0; i < linksLength; i++) {
                links[i].onclick = track;
            }


            //    for (var i = 0, l = links.length; i < l; i++) {
            //        //Compare the fileType to the whitelist
            //        var match = links[i].pathname.match(whitelist);

            //        //If the link is for a file download . . .
            //        if (typeof match !== "undefined" && match !== null) {
            //            //Call Entourage whenever the link is clicked
            //            links[i].onclick = autograph;
            //        }
            //    }
        }

        return {
            init: init,
            track: track
        }
    });

    // Add tracker to global scope
    window.gadt = gadt;

    // Run scripts after page is loaded
    window.onload = gadt.init;
})();

{'fraction_non_alphanumeric': 0.10800301053687908, 'fraction_numerical': 0.003763171098845961, 'mean_word_length': 1.4449555351119288, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1964942', 'n_tokens_mistral': 2114, 'n_tokens_neox': 1937, 'n_words': 689}