<commit_before># yo, easy server-side tracking for Google Analytics... hey!
require 'uri'
require 'net/http'
require 'ipaddr'
require 'cgi'
require 'net/http/persistent'

require "#{File.dirname(__FILE__)}/parameter_map"
require "#{File.dirname(__FILE__)}/campaign"
require "#{File.dirname(__FILE__)}/custom_vars"
require "#{File.dirname(__FILE__)}/event"
require "#{File.dirname(__FILE__)}/page_view"
require "#{File.dirname(__FILE__)}/version"

module GabbaGMP

  class GoogleAnalyticsInvalidParameterError < RuntimeError; end
  class GoogleAnalyticsRequiredParameterMissingError < RuntimeError; end
  class GoogleAnalyticsParameterNotFoundError < RuntimeError; end
  class NoGoogleAnalyticsAccountError < RuntimeError; end
  class NoGoogleAnalyticsDomainError < RuntimeError; end
  class GoogleAnalyticsNetworkError < RuntimeError; end

  class GabbaGMP
    GOOGLE_HOST = "www.google-analytics.com"
    BEACON_PATH = "/collect"
    USER_AGENT = "Gabba GMP #{VERSION} Agent"

    include ParameterMap
    include CustomVars
    include Event
    include PageView

    ESCAPES = %w{ ' ! * ) }

    attr_accessor :debug

    # Public: Initialize Gabba Google Analytics Tracking Object.
    #
    # ga_tracking_id  - A String containing your Google Analytics account id.
    # request         - The request this tracker relates to.
    # cookies         - The cookies object for this request. Will be updated with the client_id cookie
    # options         - Allows for:
    #                           -client_id_cookie_expiry = Set the expiry of the visitor cookie manually
    #                           -client_id_cookie_sym = The symbol to store the visitor id cookie under
    # Example:
    #
    #   g = GabbaGMP::GabbaGMP.new("UT-1234", "mydomain.com")
    #
    def initialize(ga_tracking_id, request, cookies, options = {})
      client_id_cookie = options[:client_id_cookie_sym]
      client_id_cookie = :utm_visitor_uuid if !client_id_cookie.present? or !client_id_cookie.kind_of? Symbol
      
      if !cookies[client_id_cookie].present?
        cookie_expiry = options[:client_id_cookie_expiry] ? options[:client_id_cookie_expiry] : 1.year.from_now
        cookies[client_id_cookie] = { value: "#{SecureRandom.uuid}", expires: cookie_expiry}
      end
      
      @sessionopts = {protocol_version: 1, 
                      tracking_id: ga_tracking_id, 
                      document_host: request.host, 
                      client_id: cookies[client_id_cookie], 
                      user_ip_address: request.remote_ip, 
                      user_agent: request.user_agent,
                      user_language: preferred_language(request.accept_language)}
        
      @sessionopts[:document_referrer] = request.referrer if request.referrer.present?
        
      debug = false
    end

    def preferred_language(language)
      return "" unless language.present?
      
      language_arr = language.split(",").map {|lang_pref| lang_pref.split(";")}
      language_arr[0][0] #just get the first language. Will probably be correct.
    end

    # Public: Set the session's parameters. This will be added to all actions that are sent to analytics.
    #  
    #  See::  ParameterMap:GA_PARAMS
    def add_options(options)
      options.keys.each do |key| 
        raise GoogleAnalyticsParameterNotFoundError, "Parameter '#{key}'" unless GA_PARAMS[key].present?
      end
      
      @sessionopts.merge!(options)
      self
    end

    # Public: Set the campaign details from a campaign object. You can also use your own Campaign object so long
    #  as they support the 5 methods (name, source, medium, keyword, content)
    def campaign=(campaign)<commit_msg>Lowercase the language, as some browsers use mixed case and GA seperates it.<commit_after># yo, easy server-side tracking for Google Analytics... hey!
require 'uri'
require 'net/http'
require 'ipaddr'
require 'cgi'
require 'net/http/persistent'

require "#{File.dirname(__FILE__)}/parameter_map"
require "#{File.dirname(__FILE__)}/campaign"
require "#{File.dirname(__FILE__)}/custom_vars"
require "#{File.dirname(__FILE__)}/event"
require "#{File.dirname(__FILE__)}/page_view"
require "#{File.dirname(__FILE__)}/version"

module GabbaGMP

  class GoogleAnalyticsInvalidParameterError < RuntimeError; end
  class GoogleAnalyticsRequiredParameterMissingError < RuntimeError; end
  class GoogleAnalyticsParameterNotFoundError < RuntimeError; end
  class NoGoogleAnalyticsAccountError < RuntimeError; end
  class NoGoogleAnalyticsDomainError < RuntimeError; end
  class GoogleAnalyticsNetworkError < RuntimeError; end

  class GabbaGMP
    GOOGLE_HOST = "www.google-analytics.com"
    BEACON_PATH = "/collect"
    USER_AGENT = "Gabba GMP #{VERSION} Agent"

    include ParameterMap
    include CustomVars
    include Event
    include PageView

    ESCAPES = %w{ ' ! * ) }

    attr_accessor :debug

    # Public: Initialize Gabba Google Analytics Tracking Object.
    #
    # ga_tracking_id  - A String containing your Google Analytics account id.
    # request         - The request this tracker relates to.
    # cookies         - The cookies object for this request. Will be updated with the client_id cookie
    # options         - Allows for:
    #                           -client_id_cookie_expiry = Set the expiry of the visitor cookie manually
    #                           -client_id_cookie_sym = The symbol to store the visitor id cookie under
    # Example:
    #
    #   g = GabbaGMP::GabbaGMP.new("UT-1234", "mydomain.com")
    #
    def initialize(ga_tracking_id, request, cookies, options = {})
      client_id_cookie = options[:client_id_cookie_sym]
      client_id_cookie = :utm_visitor_uuid if !client_id_cookie.present? or !client_id_cookie.kind_of? Symbol
      
      if !cookies[client_id_cookie].present?
        cookie_expiry = options[:client_id_cookie_expiry] ? options[:client_id_cookie_expiry] : 1.year.from_now
        cookies[client_id_cookie] = { value: "#{SecureRandom.uuid}", expires: cookie_expiry}
      end
      
      @sessionopts = {protocol_version: 1, 
                      tracking_id: ga_tracking_id, 
                      document_host: request.host, 
                      client_id: cookies[client_id_cookie], 
                      user_ip_address: request.remote_ip, 
                      user_agent: request.user_agent,
                      user_language: preferred_language(request.accept_language)}
        
      @sessionopts[:document_referrer] = request.referrer if request.referrer.present?
        
      debug = false
    end

    def preferred_language(language)
      return "" unless language.present?
      
      language_arr = language.split(",").map {|lang_pref| lang_pref.split(";")}
      language_arr[0][0].downcase #just get the first language. Will probably be correct.
    end

    # Public: Set the session's parameters. This will be added to all actions that are sent to analytics.
    #  
    #  See::  ParameterMap:GA_PARAMS
    def add_options(options)
      options.keys.each do |key| 
        raise GoogleAnalyticsParameterNotFoundError, "Parameter '#{key}'" unless GA_PARAMS[key].present?
      end
      
      @sessionopts.merge!(options)
      self
    end

    # Public: Set the campaign details from a campaign object. You can also use your own Campaign object so long
    #  as they support the 5 methods (name, source, medium, keyword, content)
    def campaign=(campaign)
{'fraction_non_alphanumeric': 0.08523489932885905, 'fraction_numerical': 0.0024161073825503354, 'mean_word_length': 2.9994632313472893, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1974068', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2067, 'n_words': 565}