Commit 12c79809 authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch 'jejacks0n/add-form-tracking' into 'master'

Add method for enabling form event tracking

See merge request gitlab-org/gitlab!60036
parents 958c6227 af7e710d
...@@ -153,6 +153,21 @@ export default class Tracking { ...@@ -153,6 +153,21 @@ export default class Tracking {
return loadEvents; return loadEvents;
} }
static enableFormTracking(config, contexts = []) {
if (!this.enabled()) return;
if (!config?.forms?.whitelist?.length && !config?.fields?.whitelist?.length) {
// eslint-disable-next-line @gitlab/require-i18n-strings
throw new Error('Unable to enable form event tracking without whitelist rules.');
}
contexts.unshift(STANDARD_CONTEXT);
const enabler = () => window.snowplow('enableFormTracking', config, contexts);
if (document.readyState !== 'loading') enabler();
else document.addEventListener('DOMContentLoaded', enabler);
}
static mixin(opts = {}) { static mixin(opts = {}) {
return { return {
computed: { computed: {
......
...@@ -155,6 +155,32 @@ describe('Tracking', () => { ...@@ -155,6 +155,32 @@ describe('Tracking', () => {
}); });
}); });
describe('.enableFormTracking', () => {
it('tells snowplow to enable form tracking', () => {
const config = { forms: { whitelist: [''] }, fields: { whitelist: [''] } };
Tracking.enableFormTracking(config, ['_passed_context_']);
expect(snowplowSpy).toHaveBeenCalledWith('enableFormTracking', config, [
{ data: { source: 'gitlab-javascript' }, schema: undefined },
'_passed_context_',
]);
});
it('throws an error if no whitelist rules are provided', () => {
const expectedError = new Error(
'Unable to enable form event tracking without whitelist rules.',
);
expect(() => Tracking.enableFormTracking()).toThrow(expectedError);
expect(() => Tracking.enableFormTracking({ fields: { whitelist: [] } })).toThrow(
expectedError,
);
expect(() => Tracking.enableFormTracking({ fields: { whitelist: [1] } })).not.toThrow(
expectedError,
);
});
});
describe('.flushPendingEvents', () => { describe('.flushPendingEvents', () => {
it('flushes any pending events', () => { it('flushes any pending events', () => {
Tracking.initialized = false; Tracking.initialized = false;
......
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