Skip to content

Commit

Permalink
Fix NoMethodError when cookies == :none
Browse files Browse the repository at this point in the history
When tracking with no visit,

```
Ahoy.cookies = :none
Ahoy::Tracker.new.track_event(:would_fail)
```

would raise NoMethodError: undefined method `remote_ip' for nil.
Since we explicitly don't have a request, allow visitor_token_helper
to fall back on generate_id by not calling visitor_anonymity_set when
there is no request.
  • Loading branch information
rgarner committed Apr 11, 2024
1 parent 8e5df40 commit a0d10c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ahoy/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def existing_visitor_token
end

def visitor_anonymity_set
@visitor_anonymity_set ||= Digest::UUID.uuid_v5(UUID_NAMESPACE, ["visitor", Ahoy.mask_ip(request.remote_ip), request.user_agent].join("/"))
@visitor_anonymity_set ||= Digest::UUID.uuid_v5(
UUID_NAMESPACE, ["visitor", Ahoy.mask_ip(request.remote_ip), request.user_agent].join("/")
) if request
end

def visit_cookie
Expand Down
15 changes: 15 additions & 0 deletions test/tracker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ def test_no_request
assert_nil event.user_id
end

def test_no_request_with_cookies_none
old_cookies = Ahoy.cookies
Ahoy.cookies = :none

ahoy = Ahoy::Tracker.new
ahoy.track("Some event", some_prop: true)

event = Ahoy::Event.last
assert_equal "Some event", event.name
assert_equal({"some_prop" => true}, event.properties)
assert_nil event.user_id
ensure
Ahoy.cookies = old_cookies
end

def test_no_cookies
request = ActionDispatch::TestRequest.create

Expand Down

0 comments on commit a0d10c1

Please sign in to comment.