Commit f15f08a2 authored by Vitaly Slobodin's avatar Vitaly Slobodin

Merge branch 'jswain_glex_tracking' into 'master'

Track experiments with `Tracking.mixin`

See merge request gitlab-org/gitlab!58420
parents 85b117d0 6447dc75
...@@ -27,22 +27,33 @@ const DEFAULT_SNOWPLOW_OPTIONS = { ...@@ -27,22 +27,33 @@ const DEFAULT_SNOWPLOW_OPTIONS = {
pageUnloadTimer: 10, pageUnloadTimer: 10,
}; };
const addExperimentContext = (opts) => {
const { experiment, ...options } = opts;
if (experiment) {
const data = getExperimentData(experiment);
if (data) {
const context = { schema: TRACKING_CONTEXT_SCHEMA, data };
return { ...options, context };
}
}
return options;
};
const createEventPayload = (el, { suffix = '' } = {}) => { const createEventPayload = (el, { suffix = '' } = {}) => {
const action = (el.dataset.trackAction || el.dataset.trackEvent) + (suffix || ''); const action = (el.dataset.trackAction || el.dataset.trackEvent) + (suffix || '');
let value = el.dataset.trackValue || el.value || undefined; let value = el.dataset.trackValue || el.value || undefined;
if (el.type === 'checkbox' && !el.checked) value = false; if (el.type === 'checkbox' && !el.checked) value = false;
let context = el.dataset.trackContext; const context = addExperimentContext({
if (el.dataset.trackExperiment) { experiment: el.dataset.trackExperiment,
const data = getExperimentData(el.dataset.trackExperiment); context: el.dataset.trackContext,
if (data) context = { schema: TRACKING_CONTEXT_SCHEMA, data }; });
}
const data = { const data = {
label: el.dataset.trackLabel, label: el.dataset.trackLabel,
property: el.dataset.trackProperty, property: el.dataset.trackProperty,
value, value,
context, ...context,
}; };
return { return {
...@@ -150,7 +161,8 @@ export default class Tracking { ...@@ -150,7 +161,8 @@ export default class Tracking {
return localCategory || opts.category; return localCategory || opts.category;
}, },
trackingOptions() { trackingOptions() {
return { ...opts, ...this.tracking }; const options = addExperimentContext(opts);
return { ...options, ...this.tracking };
}, },
}, },
methods: { methods: {
......
...@@ -254,7 +254,7 @@ describe('Tracking', () => { ...@@ -254,7 +254,7 @@ describe('Tracking', () => {
expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {}); expect(eventSpy).toHaveBeenCalledWith('_category_', 'nested_event', {});
}); });
it('brings in experiment data if linked to an experiment', () => { it('includes experiment data if linked to an experiment', () => {
const mockExperimentData = { const mockExperimentData = {
variant: 'candidate', variant: 'candidate',
experiment: 'repo_integrations_link', experiment: 'repo_integrations_link',
...@@ -326,6 +326,30 @@ describe('Tracking', () => { ...@@ -326,6 +326,30 @@ describe('Tracking', () => {
mixin.computed.tracking = { foo: 'baz', baz: 'bar' }; mixin.computed.tracking = { foo: 'baz', baz: 'bar' };
expect(mixin.computed.trackingOptions()).toEqual({ foo: 'baz', baz: 'bar' }); expect(mixin.computed.trackingOptions()).toEqual({ foo: 'baz', baz: 'bar' });
}); });
it('includes experiment data if linked to an experiment', () => {
const mockExperimentData = {
variant: 'candidate',
experiment: 'darkMode',
};
getExperimentData.mockReturnValue(mockExperimentData);
const mixin = Tracking.mixin({ foo: 'bar', experiment: 'darkMode' });
expect(mixin.computed.trackingOptions()).toEqual({
foo: 'bar',
context: {
schema: TRACKING_CONTEXT_SCHEMA,
data: mockExperimentData,
},
});
});
it('does not include experiment data if experiment data does not exist', () => {
const mixin = Tracking.mixin({ foo: 'bar', experiment: 'lightMode' });
expect(mixin.computed.trackingOptions()).toEqual({
foo: 'bar',
});
});
}); });
describe('trackingCategory', () => { describe('trackingCategory', () => {
......
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