ankane / ahoy

Simple, powerful, first-party analytics for Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

With Ahoy.cookies = true, getting error when tracking: undefined method `remote_ip' for nil:NilClass

nbastoWM opened this issue · comments

commented

Hi,

If I set Ahoy.cookies = true in initializers, I always get an error when tracking:
<NoMethodError: undefined method `remote_ip' for nil:NilClass

I have a concern that I use in each controller I want to track:

module ApiAhoyable
  extend ActiveSupport::Concern

  included do
    skip_before_action :track_ahoy_visit
    after_action :ahoy_track
    skip_after_action :ahoy_track, only: [:not_viewed]
  end

  def ahoy_track
    begin
      event_name = params[:controller].gsub('/','_') + "_" + action_name
      # retirar o action e o controller, e os wrap_parameters....
      AhoyService.track(event_name,
          params.except(:action, :controller, controller_name.singularize.to_sym,
            :file, :image))
    rescue => exception
      Dev.log exception
    end
  end

end

and the service that tracks:

class AhoyService
  def self.track(event_name, params)
    ahoy = Ahoy::Tracker.new
    if (User.current.present?)
      ahoy.authenticate(User.current)
    end
    ahoy.track(event_name, params)
  end
end

My initializer:

class Ahoy::Store < Ahoy::DatabaseStore
  protected
  def visit_model
    Ahoy::Visit
  end
end

# set to true for JavaScript tracking
Ahoy.api = false
Ahoy.cookies = false

Ahoy.server_side_visits = :when_needed

The error occurs when running ahoy.track(event_name, params).
If I set Ahoy.cookies to true all goes well.
Am I doing anything wrong?

Hey @nbastoWM, you'll need to pass the controller or request option to Ahoy::Tracker.new to use it with Ahoy.cookies = false (also, based on the last part of the issue, I think the title and intro should be false instead of true).