<commit_before>module Ahoy
  class Tracker
    attr_reader :request, :controller

    def initialize(options = {})
      @store = Ahoy::Store.new(options.merge(ahoy: self))
      @controller = options[:controller]
      @request = options[:request] || @controller.try(:request)
    end

    def track(name, properties = {}, options = {})
      unless exclude?
        options = options.dup

        options[:time] ||= Time.zone.now
        options[:id] ||= generate_id

        @store.track_event(name, properties, options)
      end

      true
    rescue => e
      report_exception(e)
    end

    def track_visit(options = {})
      @visit_token = request.params["visit_token"] || generate_id
      @visitor_token = request.params["visitor_token"] || generate_id

      options[:time] ||= Time.zone.now

      unless exclude?
        @store.track_visit(options)
      end

      true
    rescue => e
      report_exception(e)
    end

    def authenticate(user)
      @store.authenticate(user)
    rescue => e
      report_exception(e)
    end

    def current_visit
      @current_visit ||= @store.current_visit
    end

    def visit_token
      @visit_token ||= request.headers["Ahoy-Visit"] || request.cookies["ahoy_visit"]
    end

    def visitor_token
      @visitor_token ||= existing_visitor_token || current_visit.try(:visitor_token) || generate_id
    end

    def set_visitor_cookie<commit_msg>Better code<commit_after>module Ahoy
  class Tracker
    attr_reader :request, :controller

    def initialize(options = {})
      @store = Ahoy::Store.new(options.merge(ahoy: self))
      @controller = options[:controller]
      @request = options[:request] || @controller.try(:request)
    end

    def track(name, properties = {}, options = {})
      unless exclude?
        options = options.dup

        options[:time] ||= Time.zone.now
        options[:id] ||= generate_id

        @store.track_event(name, properties, options)
      end

      true
    rescue => e
      report_exception(e)
    end

    def track_visit(options = {})
      @visit_token = request.params["visit_token"] || generate_id
      @visitor_token = request.params["visitor_token"] || generate_id

      unless exclude?
        options = options.dup

        options[:time] ||= Time.zone.now

        @store.track_visit(options)
      end

      true
    rescue => e
      report_exception(e)
    end

    def authenticate(user)
      @store.authenticate(user)
    rescue => e
      report_exception(e)
    end

    def current_visit
      @current_visit ||= @store.current_visit
    end

    def visit_token
      @visit_token ||= request.headers["Ahoy-Visit"] || request.cookies["ahoy_visit"]
    end

    def visitor_token
      @visitor_token ||= existing_visitor_token || current_visit.try(:visitor_token) || generate_id
    end

    def set_visitor_cookie
{'fraction_non_alphanumeric': 0.11364433532094002, 'fraction_numerical': 0.0, 'mean_word_length': 2.6990920881971467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1635375', 'n_tokens_mistral': 873, 'n_tokens_neox': 848, 'n_words': 172}