Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correlates client-side and server segment anonymous_id's #254

Merged
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def queue_job

job_params['current_user'] = (current_user.attribs || {}).to_h
job_params['action_controller.params'] = params
job_params['session_id'] = session.id

#TODO: make sure you can safely serialize the params
JobResolver.find_jobs(params).each do |job|
Expand Down
3 changes: 2 additions & 1 deletion app/views/shared/_analytics.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
window.analytics.identify('<%= current_user.id %>', {})
window.analytics.page({ userId: '<% current_user.id %>' });
<% else %>
window.analytics.page();
window.analytics.identify({ anonymousId: '<%= session.id %>' });
window.analytics.page({ anonymousId: '<%= session.id %>' });
Copy link
Member

Choose a reason for hiding this comment

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

Should we also set anonymousId when logged_in??

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I think so.

<% end %>
<% end %>
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class IdentifyUserJob < AnalyticsJob
def payload(params)
{
user_id: params['current_user']['id'] || "Anonymous",
anonymous_id: params['session_id'],
traits: params['data']
}
end
Expand Down
1 change: 1 addition & 0 deletions vendor/engines/saas/app/jobs/analytics/page_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def payload(params)
user = params['current_user'] ? params['current_user']['id'] : "Anonymous"
{
user_id: user,
anonymous_id: params['session_id'],
Copy link
Member

Choose a reason for hiding this comment

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

Should this be anonymousId rather than anonymous_id? Same question for IdentifyUserJob.

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, checked docs.

name: params['url'],
properties: { url: params['url'] }
}
Expand Down
3 changes: 2 additions & 1 deletion vendor/engines/saas/app/jobs/base_login_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def map_user(params)

{
'current_user' => params['user'],
'data' => params['user']
'data' => params['user'],
'session_id' => params['session_id']
}
end
end
3 changes: 2 additions & 1 deletion vendor/engines/saas/lib/saas/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def page_job
auth_request = request.params['code'] && request.params['state']
if request.referer !~ /github\.com/ && !logged_in? && !auth_request
Analytics::PageJob.perform_later({
'url' => "/login/#{params['action']}"
'url' => "/login/#{params['action']}",
'session_id' => request.session.id
})
end
end
Expand Down