Commit 09a37d36 authored by Alper Akgun's avatar Alper Akgun

Merge branch 'ali/update-gitlab-standard' into 'master'

Allow extra data to be sent with Snowplow events

See merge request gitlab-org/gitlab!57504
parents 5e2abe50 933e1074
......@@ -314,6 +314,7 @@ Custom event tracking and instrumentation can be added by directly calling the `
| `project` | Project | nil | The project associated with the event. |
| `user` | User | nil | The user associated with the event. |
| `namespace` | Namespace | nil | The namespace associated with the event. |
| `extra` | Hash | `{}` | Additional keyword arguments are collected into a hash and sent with the event. |
Tracking can be viewed as either tracking user behavior, or can be used for instrumentation to monitor and visualize performance over time in an area or aspect of code.
......@@ -495,6 +496,7 @@ The [`StandardContext`](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/g
| `namespace_id` | **{dotted-circle}** | integer | |
| `environment` | **{check-circle}** | string (max 32 chars) | Name of the source environment, such as `production` or `staging` |
| `source` | **{check-circle}** | string (max 32 chars) | Name of the source application, such as `gitlab-rails` or `gitlab-javascript` |
| `extra` | **{dotted-circle}** | JSON | Any additional data associated with the event, in the form of key-value pairs |
### Default Schema
......
......@@ -9,8 +9,8 @@ module Gitlab
Gitlab::CurrentSettings.snowplow_enabled?
end
def event(category, action, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil) # rubocop:disable Metrics/ParameterLists
contexts = [Tracking::StandardContext.new(project: project, user: user, namespace: namespace).to_context, *context]
def event(category, action, label: nil, property: nil, value: nil, context: [], project: nil, user: nil, namespace: nil, **extra) # rubocop:disable Metrics/ParameterLists
contexts = [Tracking::StandardContext.new(project: project, user: user, namespace: namespace, **extra).to_context, *context]
snowplow.event(category, action, label: label, property: property, value: value, context: contexts)
product_analytics.event(category, action, label: label, property: property, value: value, context: contexts)
......
......@@ -3,11 +3,11 @@
module Gitlab
module Tracking
class StandardContext
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-3'
GITLAB_STANDARD_SCHEMA_URL = 'iglu:com.gitlab/gitlab_standard/jsonschema/1-0-4'
GITLAB_RAILS_SOURCE = 'gitlab-rails'
def initialize(namespace: nil, project: nil, user: nil, **data)
@data = data
def initialize(namespace: nil, project: nil, user: nil, **extra)
@extra = extra
end
def to_context
......@@ -35,8 +35,9 @@ module Gitlab
def to_h
{
environment: environment,
source: source
}.merge(@data)
source: source,
extra: @extra
}
end
end
end
......
......@@ -58,10 +58,16 @@ RSpec.describe Gitlab::Tracking::StandardContext do
end
context 'with extra data' do
subject { described_class.new(foo: 'bar') }
subject { described_class.new(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2') }
it 'creates a Snowplow context with the given data' do
expect(snowplow_context.to_json.dig(:data, :foo)).to eq('bar')
it 'includes extra data in `extra` hash' do
expect(snowplow_context.to_json.dig(:data, :extra)).to eq(extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
end
end
context 'without extra data' do
it 'contains an empty `extra` hash' do
expect(snowplow_context.to_json.dig(:data, :extra)).to be_empty
end
end
......
......@@ -51,7 +51,7 @@ RSpec.describe Gitlab::Tracking do
expect(Gitlab::Tracking::StandardContext)
.to receive(:new)
.with(project: project, user: user, namespace: namespace)
.with(project: project, user: user, namespace: namespace, extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
.and_call_original
expect_any_instance_of(klass).to receive(:event) do |_, category, action, args|
......@@ -66,7 +66,8 @@ RSpec.describe Gitlab::Tracking do
end
described_class.event('category', 'action', label: 'label', property: 'property', value: 1.5,
context: [other_context], project: project, user: user, namespace: namespace)
context: [other_context], project: project, user: user, namespace: namespace,
extra_key_1: 'extra value 1', extra_key_2: 'extra value 2')
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment