Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/services/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def track_event(event, attributes = {})
new_session_success_state: first_success_state_this_session?,
success_state: success_state_token(event),
path: request&.path,
session_duration: session_duration,
user_id: attributes[:user_id] || user.uuid,
locale: I18n.locale,
}
Expand Down Expand Up @@ -121,6 +122,16 @@ def browser_attributes
}
end

def session_duration
@session[:session_started_at].present? ? Time.zone.now - session_started_at : nil
end

def session_started_at
value = @session[:session_started_at]
return value unless value.is_a?(String)
Time.zone.parse(value)
Comment on lines +131 to +132
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider doing this as a positive check, instead of a negative check

Suggested change
return value unless value.is_a?(String)
Time.zone.parse(value)
value.kind_of?(String) : Time.zone.parse(value) : value

end

# rubocop:disable Layout/LineLength
ACCOUNT_RESET_VISIT = 'Account deletion and reset visited'
DOC_AUTH = 'Doc Auth' # visited or submitted is appended
Expand Down
35 changes: 35 additions & 0 deletions spec/services/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
success_state: success_state,
new_event: true,
path: path,
session_duration: nil,
}

expect(ahoy).to receive(:track).
Expand All @@ -73,6 +74,7 @@
success_state: success_state,
new_event: nil,
path: path,
session_duration: nil,
}

expect(ahoy).to receive(:track).
Expand All @@ -95,6 +97,7 @@
new_session_path: true,
new_event: true,
path: path,
session_duration: nil,
}

expect(ahoy).to receive(:track).
Expand Down Expand Up @@ -132,6 +135,7 @@
path: path,
new_session_success_state: true,
success_state: success_state,
session_duration: nil,
}

expect(ahoy).to receive(:track).
Expand Down Expand Up @@ -192,4 +196,35 @@
end.to_not raise_error
end
end

it 'tracks session duration' do
freeze_time do
analytics = Analytics.new(
user: current_user,
request: request,
sp: 'http://localhost:3000',
session: { session_started_at: 7.seconds.ago },
ahoy: ahoy,
)

analytics_hash = {
event_properties: {},
user_id: current_user.uuid,
locale: I18n.locale,
git_sha: IdentityConfig::GIT_SHA,
git_branch: IdentityConfig::GIT_BRANCH,
new_session_success_state: true,
success_state: success_state,
new_session_path: true,
new_event: true,
path: path,
session_duration: 7.0,
}

expect(ahoy).to receive(:track).
with('Trackable Event', analytics_hash.merge(request_attributes))

analytics.track_event('Trackable Event')
end
end
end