Skip to content

Commit

Permalink
Create visit for visitable if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Feb 27, 2018
1 parent 1e72c7d commit 13c9b3c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.1 [unreleased]

- Create visit for `visitable` if needed
- Better handling of visit duration and expiration in JavaScript

## 2.0.0

- Removed dependency on jQuery
Expand Down
4 changes: 4 additions & 0 deletions lib/ahoy/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def generate_id
Ahoy.token_generator.call
end

def visit_or_create
visit
end

protected

def bot?
Expand Down
10 changes: 7 additions & 3 deletions lib/ahoy/database_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def track_visit(data)
end

def track_event(data)
# if we don't have a visit, let's try to create one first
ahoy.track_visit unless visit

visit = visit_or_create
if visit
event = event_model.new(slice_data(event_model, data))
event.visit = visit
Expand Down Expand Up @@ -51,6 +49,12 @@ def visit
@visit ||= visit_model.where(visit_token: ahoy.visit_token).first if ahoy.visit_token
end

# if we don't have a visit, let's try to create one first
def visit_or_create
ahoy.track_visit unless visit
visit
end

protected

def visit_model
Expand Down
2 changes: 1 addition & 1 deletion lib/ahoy/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def visitable(name = :visit, **options)
end
class_eval %{
def set_ahoy_visit
self.#{name} ||= RequestStore.store[:ahoy].try(:visit)
self.#{name} ||= RequestStore.store[:ahoy].try(:visit_or_create)
end
}
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ahoy/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def visit
@visit ||= @store.visit
end

def visit_or_create
@visit ||= @store.visit_or_create
end

def new_visit?
!existing_visit_token
end
Expand Down

0 comments on commit 13c9b3c

Please sign in to comment.