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

Breaking change on events with_data() method introduced with v1.13.0 release #184

Merged
merged 3 commits into from
Dec 10, 2024
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
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ event = DfE::Analytics::Event.new
.with_user(current_user)
.with_request_details(request)
.with_namespace('some_namespace')
.with_data(some: 'custom details about event')
.with_data(data: { some: 'custom details about event' })
```

If you need to include hidden PII, you can use the `hidden_data` key which will allow all fields listed to be sent separately to BigQuery where they will be hidden.
Expand All @@ -323,17 +323,30 @@ event = DfE::Analytics::Event.new
.with_request_details(request)
.with_namespace('some_namespace')
.with_data(
data:
{
data:
{
some: 'custom details about event'
},
hidden_data: {
hidden_data: {
some_hidden: 'some data to be hidden',
more_hidden: 'more data to be hidden,
}
)
```

**NOTE**:

Backwards compatability for the `with_data` method was added after v1.15.2 that allows the following call:

```ruby
event
.with_data(
some: 'custom details about event'
)
```

Backwards compatibility was missing in analytics versions v1.13.0 - v1.15.2 inclusive. This means that any custom events or other events using the `with_data` method for adding event data, sent from applications with analytics versions not backwards compatible, would have lost those events (unless event data is sent using the `data` and/or `hidden_data` keys).

Once all the events have been constructed, simply send them to your analytics:

```ruby
Expand Down
3 changes: 3 additions & 0 deletions lib/dfe/analytics/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def with_data(hash)
@event_hash.deep_merge!(data: hash_to_kv_pairs(hash[:data])) if hash.include?(:data)
@event_hash.deep_merge!(hidden_data: hash_to_kv_pairs(hash[:hidden_data])) if hash.include?(:hidden_data)

# For backwards compatibility pre v1.13.0 add hash with :data key IF :data nor :hidden_data keys exist
@event_hash.deep_merge!(data: hash_to_kv_pairs(hash)) if hash.exclude?(:data) && hash.exclude?(:hidden_data)

self
end

Expand Down
15 changes: 15 additions & 0 deletions spec/dfe/analytics/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ def find_data_pair(output, key)
expect(updated_event_hash['data']).to eq([])
expect(updated_event_hash['hidden_data']).to eq([])
end

it 'remain backwards compatible when with_data is called without the :data and :hidden_data keys' do
event.with_data(some: 'custom details about event', ethnic_background: 'Red')
updated_event_hash = event.as_json

data_some_key = updated_event_hash['data'].find { |d| d['key'] == 'some' }
expect(data_some_key).not_to be_nil
expect(data_some_key['value']).to eq(['custom details about event'])

data_ethnic_background_key = updated_event_hash['data'].find { |d| d['key'] == 'ethnic_background' }
expect(data_ethnic_background_key).not_to be_nil
expect(data_ethnic_background_key['value']).to eq(['Red'])

expect(updated_event_hash['hidden_data']).to be_nil
end
end

describe 'handling invalid UTF-8' do
Expand Down
Loading