Skip to content
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['guid']

#TODO: make sure you can safely serialize the params
JobResolver.find_jobs(params).each do |job|
Expand Down
1 change: 0 additions & 1 deletion app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def private
end
:protected
def login
request.env['warden'].logout if github_authenticated? :default
github_authenticate! :private
end

Expand Down
2 changes: 0 additions & 2 deletions app/controllers/login_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ def logout
redirect_to "/"
end
def public
request.env['warden'].logout if github_authenticated? :private
github_authenticate! :default
@user = gh.user
@emails = @user.emails.all
redirect_to params[:redirect_to] || "/"
end
def private
request.env['warden'].logout if github_authenticated? :default
github_authenticate! :private
@user = gh.user
@emails = @user.emails.all
Expand Down
15 changes: 11 additions & 4 deletions app/views/shared/_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<script>
<% if ENV['SEGMENTIO_KEY'] %>
window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById("analytics-js")){var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.io/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION="2.0.9",
window.analytics.load("<%= ENV["SEGMENTIO_KEY"] %>");
window.analytics.load("<%= ENV['SEGMENTIO_KEY'] %>");
<% if logged_in? %>
window.analytics.identify('<%= current_user.id %>', {})
window.analytics.page({ userId: '<% current_user.id %>' });
window.analytics.identify("<%= current_user.id %>", {}, {
anonymousId: "<%= session['guid'] %>"
})
window.analytics.page({
userId: "<%= current_user.id %>"
}, {
anonymousId: "<%= session['guid'] %>"
});
<% else %>
window.analytics.page();
window.analytics.identify({}, { anonymousId: "<%= session['guid'] %>" });
window.analytics.page({}, { anonymousId: "<%= session['guid'] %>" });
<% end %>
<% end %>
</script>
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Bundler.require(*Rails.groups)

require File.expand_path('../../lib/core_extensions/string', __FILE__)
require File.expand_path('../../lib/hu_board/middleware', __FILE__)

Octokit.api_endpoint = ENV["GITHUB_API_ENDPOINT"] if ENV["GITHUB_API_ENDPOINT"]
Octokit.web_endpoint = ENV["GITHUB_WEB_ENDPOINT"] if ENV["GITHUB_WEB_ENDPOINT"]
Expand Down Expand Up @@ -79,6 +80,7 @@ class Application < Rails::Application

config.middleware.use Rack::Attack
config.middleware.use PDFKit::Middleware, {print_media_type: true}, only: %r[^/settings]
config.middleware.use HuBoard::Middleware::Session

# !!! This addresses a Rails Behaviour that coaxes empty arrays in the params hash into nils
# see https://github.com/rails/rails/pull/13188
Expand Down
1 change: 1 addition & 0 deletions lib/hu_board/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative 'middleware/session'
17 changes: 17 additions & 0 deletions lib/hu_board/middleware/session.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module HuBoard
module Middleware
class Session
def initialize(app)
@app = app
end

def call(env)
if env['rack.session']['guid'].nil?
env['rack.session']['guid'] = SecureRandom.uuid
end

@app.call(env)
end
end
end
end
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['guid']
})
end
end
Expand Down