Commit b75c135b authored by Axel Garcia's avatar Axel Garcia Committed by Miguel Rincon

Allow `extra` parameter for Snowplow events

Changelog: changed
MR: !56869
parent 38235936
export const SNOWPLOW_JS_SOURCE = 'gitlab-javascript';
import { SNOWPLOW_JS_SOURCE } from './constants';
export default function getStandardContext({ extra = {} } = {}) {
const { schema, data = {} } = { ...window.gl?.snowplowStandardContext };
return {
schema,
data: {
...data,
source: SNOWPLOW_JS_SOURCE,
extra: extra || data.extra,
},
};
}
import { omitBy, isUndefined } from 'lodash'; import { omitBy, isUndefined } from 'lodash';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants'; import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getExperimentData } from '~/experimentation/utils'; import { getExperimentData } from '~/experimentation/utils';
import getStandardContext from './get_standard_context';
const standardContext = { ...window.gl?.snowplowStandardContext };
export const STANDARD_CONTEXT = {
schema: standardContext.schema,
data: {
...(standardContext.data || {}),
source: 'gitlab-javascript',
},
};
const DEFAULT_SNOWPLOW_OPTIONS = { const DEFAULT_SNOWPLOW_OPTIONS = {
namespace: 'gl', namespace: 'gl',
...@@ -44,19 +35,41 @@ const addExperimentContext = (opts) => { ...@@ -44,19 +35,41 @@ const addExperimentContext = (opts) => {
}; };
const createEventPayload = (el, { suffix = '' } = {}) => { const createEventPayload = (el, { suffix = '' } = {}) => {
const action = (el.dataset.trackAction || el.dataset.trackEvent) + (suffix || ''); const {
let value = el.dataset.trackValue || el.value || undefined; trackAction,
trackEvent,
trackValue,
trackExtra,
trackExperiment,
trackContext,
trackLabel,
trackProperty,
} = el?.dataset || {};
const action = (trackAction || trackEvent) + (suffix || '');
let value = trackValue || el.value || undefined;
if (el.type === 'checkbox' && !el.checked) value = false; if (el.type === 'checkbox' && !el.checked) value = false;
let extra = trackExtra;
if (extra !== undefined) {
try {
extra = JSON.parse(extra);
} catch (e) {
extra = undefined;
}
}
const context = addExperimentContext({ const context = addExperimentContext({
experiment: el.dataset.trackExperiment, experiment: trackExperiment,
context: el.dataset.trackContext, context: trackContext,
}); });
const data = { const data = {
label: el.dataset.trackLabel, label: trackLabel,
property: el.dataset.trackProperty, property: trackProperty,
value, value,
extra,
...context, ...context,
}; };
...@@ -88,8 +101,10 @@ const dispatchEvent = (category = document.body.dataset.page, action = 'generic' ...@@ -88,8 +101,10 @@ const dispatchEvent = (category = document.body.dataset.page, action = 'generic'
// eslint-disable-next-line @gitlab/require-i18n-strings // eslint-disable-next-line @gitlab/require-i18n-strings
if (!category) throw new Error('Tracking: no category provided for tracking.'); if (!category) throw new Error('Tracking: no category provided for tracking.');
const { label, property, value } = data; const { label, property, value, extra = {} } = data;
const contexts = [STANDARD_CONTEXT];
const standardContext = getStandardContext({ extra });
const contexts = [standardContext];
if (data.context) { if (data.context) {
contexts.push(data.context); contexts.push(data.context);
...@@ -165,13 +180,18 @@ export default class Tracking { ...@@ -165,13 +180,18 @@ export default class Tracking {
throw new Error('Unable to enable form event tracking without allow rules.'); throw new Error('Unable to enable form event tracking without allow rules.');
} }
contexts.unshift(STANDARD_CONTEXT); // Ignore default/standard schema
const standardContext = getStandardContext();
const userProvidedContexts = contexts.filter(
(context) => context.schema !== standardContext.schema,
);
const mappedConfig = { const mappedConfig = {
forms: { whitelist: config.forms?.allow || [] }, forms: { whitelist: config.forms?.allow || [] },
fields: { whitelist: config.fields?.allow || [] }, fields: { whitelist: config.fields?.allow || [] },
}; };
const enabler = () => window.snowplow('enableFormTracking', mappedConfig, contexts); const enabler = () => window.snowplow('enableFormTracking', mappedConfig, userProvidedContexts);
if (document.readyState !== 'loading') enabler(); if (document.readyState !== 'loading') enabler();
else document.addEventListener('DOMContentLoaded', enabler); else document.addEventListener('DOMContentLoaded', enabler);
...@@ -220,7 +240,8 @@ export function initDefaultTrackers() { ...@@ -220,7 +240,8 @@ export function initDefaultTrackers() {
window.snowplow('enableActivityTracking', 30, 30); window.snowplow('enableActivityTracking', 30, 30);
// must be after enableActivityTracking // must be after enableActivityTracking
window.snowplow('trackPageView', null, [STANDARD_CONTEXT]); const standardContext = getStandardContext();
window.snowplow('trackPageView', null, [standardContext]);
if (window.snowplowOptions.formTracking) Tracking.enableFormTracking(opts.formTrackingConfig); if (window.snowplowOptions.formTracking) Tracking.enableFormTracking(opts.formTrackingConfig);
if (window.snowplowOptions.linkClickTracking) window.snowplow('enableLinkClickTracking'); if (window.snowplowOptions.linkClickTracking) window.snowplow('enableLinkClickTracking');
......
...@@ -155,13 +155,13 @@ Snowplow JS adds many [web-specific parameters](https://docs.snowplowanalytics.c ...@@ -155,13 +155,13 @@ Snowplow JS adds many [web-specific parameters](https://docs.snowplowanalytics.c
## Implementing Snowplow JS (Frontend) tracking ## Implementing Snowplow JS (Frontend) tracking
GitLab provides `Tracking`, an interface that wraps the [Snowplow JavaScript Tracker](https://github.com/snowplow/snowplow/wiki/javascript-tracker) for tracking custom events. The simplest way to use it is to add `data-` attributes to clickable elements and dropdowns. There is also a Vue mixin (exposing a `track` method), and the static method `Tracking.event`. Each of these requires at minimum a `category` and an `action`. Additional data can be provided that adheres to our [Structured event taxonomy](#structured-event-taxonomy). GitLab provides `Tracking`, an interface that wraps the [Snowplow JavaScript Tracker](https://github.com/snowplow/snowplow/wiki/javascript-tracker) for tracking custom events. The simplest way to use it is to add `data-` attributes to clickable elements and dropdowns. There is also a Vue mixin (exposing a `track` method), and the static method `Tracking.event`. Each of these requires at minimum a `category` and an `action`. You can provide additional [Structured event taxonomy](#structured-event-taxonomy) properties along with an `extra` object that accepts key-value pairs.
| field | type | default value | description | | field | type | default value | description |
|:-----------|:-------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |:-----------|:-------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `category` | string | `document.body.dataset.page` | Page or subsection of a page that events are being captured within. | | `category` | string | `document.body.dataset.page` | Page or subsection of a page that events are being captured within. |
| `action` | string | generic | Action the user is taking. Clicks should be `click` and activations should be `activate`, so for example, focusing a form field would be `activate_form_input`, and clicking a button would be `click_button`. | | `action` | string | generic | Action the user is taking. Clicks should be `click` and activations should be `activate`, so for example, focusing a form field would be `activate_form_input`, and clicking a button would be `click_button`. |
| `data` | object | `{}` | Additional data such as `label`, `property`, `value`, and `context` as described in our [Structured event taxonomy](#structured-event-taxonomy). | | `data` | object | `{}` | Additional data such as `label`, `property`, `value`, `context` (as described in our [Structured event taxonomy](#structured-event-taxonomy)), and `extra` (key-value pairs object). |
### Usage recommendations ### Usage recommendations
...@@ -171,7 +171,7 @@ GitLab provides `Tracking`, an interface that wraps the [Snowplow JavaScript Tra ...@@ -171,7 +171,7 @@ GitLab provides `Tracking`, an interface that wraps the [Snowplow JavaScript Tra
### Tracking with data attributes ### Tracking with data attributes
When working within HAML (or Vue templates) we can add `data-track-*` attributes to elements of interest. All elements that have a `data-track-action` attribute automatically have event tracking bound on clicks. When working within HAML (or Vue templates) we can add `data-track-*` attributes to elements of interest. All elements that have a `data-track-action` attribute automatically have event tracking bound on clicks. You can provide extra data as a valid JSON string using `data-track-extra`.
Below is an example of `data-track-*` attributes assigned to a button: Below is an example of `data-track-*` attributes assigned to a button:
...@@ -184,6 +184,7 @@ Below is an example of `data-track-*` attributes assigned to a button: ...@@ -184,6 +184,7 @@ Below is an example of `data-track-*` attributes assigned to a button:
data-track-action="click_button" data-track-action="click_button"
data-track-label="template_preview" data-track-label="template_preview"
data-track-property="my-template" data-track-property="my-template"
data-track-extra='{ "template_variant": "primary" }'
/> />
``` ```
...@@ -197,6 +198,7 @@ Below is a list of supported `data-track-*` attributes: ...@@ -197,6 +198,7 @@ Below is a list of supported `data-track-*` attributes:
| `data-track-label` | false | The `label` as described in our [Structured event taxonomy](#structured-event-taxonomy). | | `data-track-label` | false | The `label` as described in our [Structured event taxonomy](#structured-event-taxonomy). |
| `data-track-property` | false | The `property` as described in our [Structured event taxonomy](#structured-event-taxonomy). | | `data-track-property` | false | The `property` as described in our [Structured event taxonomy](#structured-event-taxonomy). |
| `data-track-value` | false | The `value` as described in our [Structured event taxonomy](#structured-event-taxonomy). If omitted, this is the element's `value` property or an empty string. For checkboxes, the default value is the element's checked attribute or `false` when unchecked. | | `data-track-value` | false | The `value` as described in our [Structured event taxonomy](#structured-event-taxonomy). If omitted, this is the element's `value` property or an empty string. For checkboxes, the default value is the element's checked attribute or `false` when unchecked. |
| `data-track-extra` | false | A key-value pairs object passed as a valid JSON string. This is added to the `extra` property in our [`gitlab_standard`](#gitlab_standard) schema. |
| `data-track-context` | false | The `context` as described in our [Structured event taxonomy](#structured-event-taxonomy). | | `data-track-context` | false | The `context` as described in our [Structured event taxonomy](#structured-event-taxonomy). |
#### Available helpers #### Available helpers
...@@ -287,6 +289,7 @@ export default { ...@@ -287,6 +289,7 @@ export default {
// category: '', // category: '',
// property: '', // property: '',
// value: '', // value: '',
// extra: {},
}, },
}; };
}, },
...@@ -357,6 +360,10 @@ button.addEventListener('click', () => { ...@@ -357,6 +360,10 @@ button.addEventListener('click', () => {
Tracking.event('dashboard:projects:index', 'click_button', { Tracking.event('dashboard:projects:index', 'click_button', {
label: 'create_from_template', label: 'create_from_template',
property: 'template_preview', property: 'template_preview',
extra: {
templateVariant: 'primary',
valid: true,
},
}); });
}); });
``` ```
...@@ -381,6 +388,10 @@ describe('MyTracking', () => { ...@@ -381,6 +388,10 @@ describe('MyTracking', () => {
expect(Tracking.event).toHaveBeenCalledWith(undefined, 'click_button', { expect(Tracking.event).toHaveBeenCalledWith(undefined, 'click_button', {
label: 'create_from_template', label: 'create_from_template',
property: 'template_preview', property: 'template_preview',
extra: {
templateVariant: 'primary',
valid: true,
},
}); });
}); });
}); });
......
import { SNOWPLOW_JS_SOURCE } from '~/tracking/constants';
import getStandardContext from '~/tracking/get_standard_context';
describe('~/tracking/get_standard_context', () => {
beforeEach(() => {
window.gl = window.gl || {};
window.gl.snowplowStandardContext = {};
});
it('returns default object if called without server context', () => {
expect(getStandardContext()).toStrictEqual({
schema: undefined,
data: {
source: SNOWPLOW_JS_SOURCE,
extra: {},
},
});
});
it('returns filled object if called with server context', () => {
window.gl.snowplowStandardContext = {
schema: 'iglu:com.gitlab/gitlab_standard',
data: {
environment: 'testing',
},
};
expect(getStandardContext()).toStrictEqual({
schema: 'iglu:com.gitlab/gitlab_standard',
data: {
environment: 'testing',
source: SNOWPLOW_JS_SOURCE,
extra: {},
},
});
});
it('always overrides `source` property', () => {
window.gl.snowplowStandardContext = {
data: {
source: 'custom_source',
},
};
expect(getStandardContext().data.source).toBe(SNOWPLOW_JS_SOURCE);
});
it('accepts optional `extra` property', () => {
const extra = { foo: 'bar' };
expect(getStandardContext({ extra }).data.extra).toBe(extra);
});
});
import { setHTMLFixture } from 'helpers/fixtures'; import { setHTMLFixture } from 'helpers/fixtures';
import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants'; import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants';
import { getExperimentData } from '~/experimentation/utils'; import { getExperimentData } from '~/experimentation/utils';
import Tracking, { initUserTracking, initDefaultTrackers, STANDARD_CONTEXT } from '~/tracking'; import Tracking, { initUserTracking, initDefaultTrackers } from '~/tracking';
import getStandardContext from '~/tracking/get_standard_context';
jest.mock('~/experimentation/utils', () => ({ getExperimentData: jest.fn() })); jest.mock('~/experimentation/utils', () => ({ getExperimentData: jest.fn() }));
describe('Tracking', () => { describe('Tracking', () => {
let standardContext;
let snowplowSpy; let snowplowSpy;
let bindDocumentSpy; let bindDocumentSpy;
let trackLoadEventsSpy; let trackLoadEventsSpy;
let enableFormTracking; let enableFormTracking;
beforeAll(() => {
window.gl = window.gl || {};
window.gl.snowplowStandardContext = {
schema: 'iglu:com.gitlab/gitlab_standard',
data: {
environment: 'testing',
source: 'unknown',
extra: {},
},
};
standardContext = getStandardContext();
});
beforeEach(() => { beforeEach(() => {
getExperimentData.mockReturnValue(undefined); getExperimentData.mockReturnValue(undefined);
...@@ -59,7 +75,7 @@ describe('Tracking', () => { ...@@ -59,7 +75,7 @@ describe('Tracking', () => {
it('should activate features based on what has been enabled', () => { it('should activate features based on what has been enabled', () => {
initDefaultTrackers(); initDefaultTrackers();
expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30); expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30);
expect(snowplowSpy).toHaveBeenCalledWith('trackPageView', null, [STANDARD_CONTEXT]); expect(snowplowSpy).toHaveBeenCalledWith('trackPageView', null, [standardContext]);
expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking'); expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking');
expect(snowplowSpy).not.toHaveBeenCalledWith('enableLinkClickTracking'); expect(snowplowSpy).not.toHaveBeenCalledWith('enableLinkClickTracking');
...@@ -93,45 +109,41 @@ describe('Tracking', () => { ...@@ -93,45 +109,41 @@ describe('Tracking', () => {
navigator.msDoNotTrack = undefined; navigator.msDoNotTrack = undefined;
}); });
describe('builds the standard context', () => { it('tracks to snowplow (our current tracking system)', () => {
let standardContext; Tracking.event('_category_', '_eventName_', { label: '_label_' });
beforeAll(async () => {
window.gl = window.gl || {};
window.gl.snowplowStandardContext = {
schema: 'iglu:com.gitlab/gitlab_standard',
data: {
environment: 'testing',
source: 'unknown',
},
};
jest.resetModules();
({ STANDARD_CONTEXT: standardContext } = await import('~/tracking'));
});
it('uses server data', () => { expect(snowplowSpy).toHaveBeenCalledWith(
expect(standardContext.schema).toBe('iglu:com.gitlab/gitlab_standard'); 'trackStructEvent',
expect(standardContext.data.environment).toBe('testing'); '_category_',
'_eventName_',
'_label_',
undefined,
undefined,
[standardContext],
);
}); });
it('overrides schema source', () => { it('allows adding extra data to the default context', () => {
expect(standardContext.data.source).toBe('gitlab-javascript'); const extra = { foo: 'bar' };
});
});
it('tracks to snowplow (our current tracking system)', () => { Tracking.event('_category_', '_eventName_', { extra });
Tracking.event('_category_', '_eventName_', { label: '_label_' });
expect(snowplowSpy).toHaveBeenCalledWith( expect(snowplowSpy).toHaveBeenCalledWith(
'trackStructEvent', 'trackStructEvent',
'_category_', '_category_',
'_eventName_', '_eventName_',
'_label_',
undefined, undefined,
undefined, undefined,
[STANDARD_CONTEXT], undefined,
[
{
...standardContext,
data: {
...standardContext.data,
extra,
},
},
],
); );
}); });
...@@ -165,14 +177,14 @@ describe('Tracking', () => { ...@@ -165,14 +177,14 @@ describe('Tracking', () => {
}); });
describe('.enableFormTracking', () => { describe('.enableFormTracking', () => {
it('tells snowplow to enable form tracking', () => { it('tells snowplow to enable form tracking, with only explicit contexts', () => {
const config = { forms: { allow: ['form-class1'] }, fields: { allow: ['input-class1'] } }; const config = { forms: { allow: ['form-class1'] }, fields: { allow: ['input-class1'] } };
Tracking.enableFormTracking(config, ['_passed_context_']); Tracking.enableFormTracking(config, ['_passed_context_', standardContext]);
expect(snowplowSpy).toHaveBeenCalledWith( expect(snowplowSpy).toHaveBeenCalledWith(
'enableFormTracking', 'enableFormTracking',
{ forms: { whitelist: ['form-class1'] }, fields: { whitelist: ['input-class1'] } }, { forms: { whitelist: ['form-class1'] }, fields: { whitelist: ['input-class1'] } },
[{ data: { source: 'gitlab-javascript' }, schema: undefined }, '_passed_context_'], ['_passed_context_'],
); );
}); });
...@@ -203,7 +215,7 @@ describe('Tracking', () => { ...@@ -203,7 +215,7 @@ describe('Tracking', () => {
'_label_', '_label_',
undefined, undefined,
undefined, undefined,
[STANDARD_CONTEXT], [standardContext],
); );
}); });
}); });
...@@ -226,6 +238,8 @@ describe('Tracking', () => { ...@@ -226,6 +238,8 @@ describe('Tracking', () => {
<div data-track-${term}="nested_event"><span class="nested"></span></div> <div data-track-${term}="nested_event"><span class="nested"></span></div>
<input data-track-bogus="click_bogusinput" data-track-label="_label_" value="_value_"/> <input data-track-bogus="click_bogusinput" data-track-label="_label_" value="_value_"/>
<input data-track-${term}="click_input3" data-track-experiment="example" value="_value_"/> <input data-track-${term}="click_input3" data-track-experiment="example" value="_value_"/>
<input data-track-${term}="event_with_extra" data-track-extra='{ "foo": "bar" }' />
<input data-track-${term}="event_with_invalid_extra" data-track-extra="invalid_json" />
`); `);
}); });
...@@ -301,6 +315,20 @@ describe('Tracking', () => { ...@@ -301,6 +315,20 @@ describe('Tracking', () => {
context: { schema: TRACKING_CONTEXT_SCHEMA, data: mockExperimentData }, context: { schema: TRACKING_CONTEXT_SCHEMA, data: mockExperimentData },
}); });
}); });
it('supports extra data as JSON', () => {
document.querySelector(`[data-track-${term}="event_with_extra"]`).click();
expect(eventSpy).toHaveBeenCalledWith('_category_', 'event_with_extra', {
extra: { foo: 'bar' },
});
});
it('ignores extra if provided JSON is invalid', () => {
document.querySelector(`[data-track-${term}="event_with_invalid_extra"]`).click();
expect(eventSpy).toHaveBeenCalledWith('_category_', 'event_with_invalid_extra', {});
});
}); });
describe.each` describe.each`
......
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