<commit_before>module Staccato
  # The `Hit` module enables a class to track the appropriate parameters
  #   to Google Analytics given a defined set of `FIELDS` in a map between
  #   the option name and its specified GA field name
  # 
  # @author <NAME>
  module Hit
    # this module is included into each model hit type
    #   to share the common behavior required to hit
    #   the Google Analytics /collect api endpoint
    def self.included(model)
      model.extend Forwardable

      model.class_eval do
        attr_accessor :tracker, :options

        def_delegators :@options, *model::FIELDS.keys
      end
    end

    # sets up a new hit
    # @param tracker [Staccato::Tracker] the tracker to collect to
    # @param options [Hash] options for the specific hit type
    def initialize(tracker, options = {})
      self.tracker = tracker
      self.options = OptionSet.new(options)
    end

    # return the fields for this hit type
    # @return [Hash] the field definitions
    def fields
      self.class::FIELDS
    end

    # collects the parameters from options for this hit type
    def params
      base_params.merge(global_options_params).merge(hit_params).reject {|_,v| v.nil?}
    end

    # is this a non interactive hit
    # @return [Integer, nil]
    def non_interactive
      1 if options[:non_interactive] # defaults to nil
    end

    # post the hit to GA collection endpoint
    # @return [Net::HTTPOK] the GA api always returns 200 OK
    def track!
      post(Staccato.tracking_uri, params)
    end

    private
    # @private
    def post(uri, params = {})
      Net::HTTP.post_form(uri, params)
    end

    # @private
    def base_params
      {
        'v' => 1,
        'tid' => tracker.id,
        'cid' => tracker.client_id,
        'ni' => non_interactive,
        't' => type.to_s
      }
    end

    # @private
    def global_options_params
      {
        'ni' => non_interactive,
        'xid' => options[:experiment_id],
        'xvar' => options[:experiment_variant]
      }
    end

    # @private
    def hit_params
      Hash[fields.map {|field,key| [key, options[field]]}]<commit_msg>adds global options for user_language, referrer, and encoding<commit_after>module Staccato
  # The `Hit` module enables a class to track the appropriate parameters
  #   to Google Analytics given a defined set of `FIELDS` in a map between
  #   the option name and its specified GA field name
  # 
  # @author <NAME>
  module Hit
    # this module is included into each model hit type
    #   to share the common behavior required to hit
    #   the Google Analytics /collect api endpoint
    def self.included(model)
      model.extend Forwardable

      model.class_eval do
        attr_accessor :tracker, :options

        def_delegators :@options, *model::FIELDS.keys
      end
    end

    # sets up a new hit
    # @param tracker [Staccato::Tracker] the tracker to collect to
    # @param options [Hash] options for the specific hit type
    def initialize(tracker, options = {})
      self.tracker = tracker
      self.options = OptionSet.new(options)
    end

    # return the fields for this hit type
    # @return [Hash] the field definitions
    def fields
      self.class::FIELDS
    end

    # collects the parameters from options for this hit type
    def params
      base_params.merge(global_options_params).merge(hit_params).reject {|_,v| v.nil?}
    end

    # is this a non interactive hit
    # @return [Integer, nil]
    def non_interactive
      1 if options[:non_interactive] # defaults to nil
    end

    # post the hit to GA collection endpoint
    # @return [Net::HTTPOK] the GA api always returns 200 OK
    def track!
      post(Staccato.tracking_uri, params)
    end

    private
    # @private
    def post(uri, params = {})
      Net::HTTP.post_form(uri, params)
    end

    # @private
    def base_params
      {
        'v' => 1,
        'tid' => tracker.id,
        'cid' => tracker.client_id,
        'ni' => non_interactive,
        't' => type.to_s
      }
    end

    # @private
    def global_options_params
      {
        'dr' => options[:referrer],
        'de' => options[:encoding],
        'ul' => options[:user_language],
        'ni' => non_interactive,
        'xid' => options[:experiment_id],
        'xvar' => options[:experiment_variant]
      }
    end

    # @private
    def hit_params
      Hash[fields.map {|field,key| [key, options[field]]}]
{'fraction_non_alphanumeric': 0.08998875140607424, 'fraction_numerical': 0.0022497187851518562, 'mean_word_length': 2.449185415050427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7418695', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1315, 'n_words': 442}