JSFiddle - React, Tailwind, and code Playground

by epoch

JavaScript

/**
 * GA.js
 *
 * Google Analytics Stub Substition
 *
 * If GACode is passed as an option it is used, otherwise pulled from file specified in GACodePath
 *
 * Author: Arvind Gupta
 */

(function($) {
    // Local variables
    var settings;

    // Register jQuery function
    $.fn.GA = function(options) {

        // List of all available settings with default values
        settings = $.extend({
            'GACodePath': '/GA.txt',
            'GACode': 0
        }, options);

        if (!settings['GACode']) { //Try to load GACode from file
            var getFileContents = function(u) {
                return $.ajax({
                    type: "GET",
                    url: '/echo/json',
                    async: false
                }).responseText.replace(/\r\n/g, '\n');
            };

            var v = getFileContents(settings['GACodePath']);

            settings['GACode'] = (v ? v : 0);
        }

        if (!settings['GACode']) return;

        var content = '',
            script = document.createElement("script");

        content += ("var _gaq = _gaq || [];\n");
        content += ("_gaq.push(['_setAccount', '") + ('' + settings['GACode']) + ("']);\n");
        content += ("_gaq.push(['_trackPageview']);\n");
        content += ("(function() {\n  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n })();");

        script.type = "text/javascript";
        script.text = content;
        $('head').append(script);
    };

})(jQuery);

// Call plugin immediately
$().GA();