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

allow user id field to be configurable #42

Merged
merged 1 commit into from
Aug 31, 2022
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
9 changes: 6 additions & 3 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ en:
The environment we’re running in. This value will be attached
to all events we send to BigQuery.
default: ENV.fetch('RAILS_ENV', 'development')



user_identifier:
description: |
A proc which will be called with the user object, and which should
return the identifier for the user. This is useful for systems with
users that don't use the id field.
default: proc { |user| user&.id }
6 changes: 6 additions & 0 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def self.config
bigquery_timeout
enable_analytics
environment
user_identifier
]

@config ||= Struct.new(*configurables).new
Expand All @@ -74,6 +75,7 @@ def self.configure
config.log_only ||= false
config.async ||= true
config.queue ||= :default
config.user_identifier ||= proc { |user| user&.id }
end

def self.initialize!
Expand Down Expand Up @@ -175,5 +177,9 @@ def self.entity_model_mapping
end

private_class_method :entity_model_mapping

def self.user_identifier(user)
config.user_identifier.call(user)
end
end
end
2 changes: 1 addition & 1 deletion lib/dfe/analytics/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def with_response_details(rack_response)

def with_user(user)
@event_hash.merge!(
user_id: user&.id
user_id: DfE::Analytics.user_identifier(user)
)

self
Expand Down
27 changes: 27 additions & 0 deletions spec/dfe/analytics/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ def as_json
end
end

describe 'with_user' do
let(:regular_user_class) { Struct.new(:id) }

it 'uses user.id by default' do
event = described_class.new
id = rand(1000)
output = event.with_user(regular_user_class.new(id)).as_json
expect(output['user_id']).to eq id
end

context 'users that use uuid as an identifier' do
let(:custom_user_class) { Struct.new(:uuid) }

before do
allow(DfE::Analytics).to receive(:user_identifier, &:uuid)
end

it 'uses the user_identifier proc to extract user id' do
event = described_class.new
uuid = SecureRandom.uuid
output = event.with_user(custom_user_class.new(uuid)).as_json

expect(output['user_id']).to eq uuid
end
end
end

def fake_request(overrides = {})
attrs = {
uuid: '123',
Expand Down
23 changes: 23 additions & 0 deletions spec/dfe/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,27 @@
expect(DfE::Analytics.entities_for_analytics).to eq [Candidate.table_name.to_sym]
end
end

describe '#user_identifier' do
let(:user_class) { Struct.new(:id) }
let(:id) { rand(1000) }
let(:user) { user_class.new(id) }

it 'calls the user_identifier configation' do
expect(described_class.user_identifier(user)).to eq id
end

context 'with a customised user_identifier proc' do
let(:user_class) { Struct.new(:identifier) }

before do
allow(described_class.config).to receive(:user_identifier)
.and_return(->(user) { user.identifier })
end

it 'delegates to the provided proc' do
expect(described_class.user_identifier(user)).to eq id
end
end
end
end