Sha256: fabee888022711d947f2179c2743b7933fa6fc6318bc7b11b30cddb4d0c7c4ab

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module Ahoy
  class Tracker

    def initialize(options = {})
      @controller = options[:controller]
      @request = options[:request] || @controller.try(:request)
    end

    def track(name, properties = {}, options = {})
      if (Ahoy.track_bots or !bot?) and !exclude?
        # publish to each subscriber
        options = options.dup
        if @controller
          options[:controller] ||= @controller
          options[:user] ||= Ahoy.fetch_user(@controller)
          if @controller.respond_to?(:current_visit)
            options[:visit] ||= @controller.current_visit
          end
          if @controller.respond_to?(:current_visit_token)
            options[:visit_token] = @controller.current_visit_token
          end
          if @controller.respond_to?(:current_visitor_token)
            options[:visitor_token] = @controller.current_visitor_token
          end
        end
        options[:time] ||= Time.zone.now
        options[:id] ||= Ahoy.generate_id

        subscribers = Ahoy.subscribers
        if subscribers.any?
          subscribers.each do |subscriber|
            subscriber.track(name, properties, options)
          end
        else
          $stderr.puts "No subscribers"
        end
      end

      true
    end

    protected

    def bot?
      @bot ||= Browser.new(ua: @request.user_agent).bot?
    end

    def exclude?
      if Ahoy.exclude_method
        if Ahoy.exclude_method.arity == 1
          Ahoy.exclude_method.call(@controller)
        else
          Ahoy.exclude_method.call(@controller, @request)
        end
      else
        false
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ahoy_matey-0.3.1 lib/ahoy/tracker.rb
ahoy_matey-0.3.0 lib/ahoy/tracker.rb