Sha256: 112919a509690aa5a2fe8c14121beb9bf965103e230854a11751fd86f8d74007

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

module Ahoy
  class VisitProperties

    REQUEST_KEYS = [:ip, :user_agent, :referrer, :landing_page, :platform, :app_version, :os_version]
    TRAFFIC_SOURCE_KEYS = [:referring_domain, :search_keyword]
    UTM_PARAMETER_KEYS = [:utm_source, :utm_medium, :utm_term, :utm_content, :utm_campaign]
    TECHNOLOGY_KEYS = [:browser, :os, :device_type]
    LOCATION_KEYS = [:country, :region, :city]

    KEYS = REQUEST_KEYS + TRAFFIC_SOURCE_KEYS + UTM_PARAMETER_KEYS + TECHNOLOGY_KEYS + LOCATION_KEYS

    delegate *REQUEST_KEYS, to: :request_deckhand
    delegate *TRAFFIC_SOURCE_KEYS, to: :traffic_source_deckhand
    delegate *(UTM_PARAMETER_KEYS + [:landing_params]), to: :utm_parameter_deckhand
    delegate *TECHNOLOGY_KEYS, to: :technology_deckhand
    delegate *LOCATION_KEYS, to: :location_deckhand

    def initialize(request, options = {})
      @request = request
      @options = options
    end

    def [](key)
      send(key)
    end

    def keys
      KEYS
    end

    def to_hash
      keys.inject({}){|memo, key| memo[key] = send(key); memo }
    end

    protected

    def request_deckhand
      @request_deckhand ||= Deckhands::RequestDeckhand.new(@request, @options)
    end

    def traffic_source_deckhand
      @traffic_source_deckhand ||= Deckhands::TrafficSourceDeckhand.new(request_deckhand.referrer)
    end

    def utm_parameter_deckhand
      @utm_parameter_deckhand ||= Deckhands::UtmParameterDeckhand.new(request_deckhand.landing_page)
    end

    def technology_deckhand
      @technology_deckhand ||= Deckhands::TechnologyDeckhand.new(request_deckhand.user_agent)
    end

    def location_deckhand
      @location_deckhand ||= Deckhands::LocationDeckhand.new(request_deckhand.ip)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ahoy_matey-1.1.0 lib/ahoy/visit_properties.rb
ahoy_matey-1.0.2 lib/ahoy/visit_properties.rb
ahoy_matey-1.0.1 lib/ahoy/visit_properties.rb
ahoy_matey-1.0.0 lib/ahoy/visit_properties.rb