<commit_before>module Rack

  class GoogleTracker

    TRACKER_CODE = <<-EOTC
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    EOTC

    ASYNC_TRACKER_CODE = <<-EOTC
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-xxxxxx-x']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
      })();

    </script>
    EOTC

    def initialize(app, options = {})
      raise ArgumentError, "Tracker must be set!" unless options[:tracker] and !options[:tracker].empty?
      @app     = app
      @tracker = options[:tracker]
      @async   = options[:async]
      @async   = true if options[:async].nil?
      @env     = options[:env] || "production"
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      @status, @headers, @response = @app.call(env)
      return [@status, @headers, @response] unless @headers['Content-Type'] =~ /html/ && Rails.env.casecmp(@env) == 0
      @headers.delete('Content-Length')
      response = Rack::Response.new([], @status, @headers)
      @response.each do |fragment|
        response.write(inject_tracker(fragment))
      end
      response.finish
    end

    def inject_tracker(response)
      if @async
        tracker_code = ASYNC_TRACKER_CODE.sub(/UA-xxxxxx-x/, @tracker)
      else
        tracker_code = TRACKER_CODE.sub(/UA-xxxxxx-x/, @tracker)
      end
      response.sub!(/<\/body>/, "\n#{tracker_code}\n</body>") rescue response
    end

  end

end<commit_msg>Neutralise a slight railsism<commit_after>module Rack

  class GoogleTracker

    TRACKER_CODE = <<-EOTC
    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    try{
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    EOTC

    ASYNC_TRACKER_CODE = <<-EOTC
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-xxxxxx-x']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
      })();

    </script>
    EOTC

    def initialize(app, options = {})
      raise ArgumentError, "Tracker must be set!" unless options[:tracker] and !options[:tracker].empty?
      @app     = app
      @tracker = options[:tracker]
      @async   = options[:async]
      @async   = true if options[:async].nil?
      @env     = options[:env] || "production"
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      @status, @headers, @response = @app.call(env)
      return [@status, @headers, @response] unless @headers['Content-Type'] =~ /html/ && defined?(Rails) && Rails.env.casecmp(@env) == 0
      @headers.delete('Content-Length')
      response = Rack::Response.new([], @status, @headers)
      @response.each do |fragment|
        response.write(inject_tracker(fragment))
      end
      response.finish
    end

    def inject_tracker(response)
      if @async
        tracker_code = ASYNC_TRACKER_CODE.sub(/UA-xxxxxx-x/, @tracker)
      else
        tracker_code = TRACKER_CODE.sub(/UA-xxxxxx-x/, @tracker)
      end
      response.sub!(/<\/body>/, "\n#{tracker_code}\n</body>") rescue response
    end

  end

end
{'fraction_non_alphanumeric': 0.17205024674742037, 'fraction_numerical': 0.0031404217137729925, 'mean_word_length': 2.9460176991150444, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5497644', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1475, 'n_words': 273}