import { setHTMLFixture } from 'helpers/fixtures'; import { TRACKING_CONTEXT_SCHEMA } from '~/experimentation/constants'; import { getExperimentData } from '~/experimentation/utils'; import Tracking, { initUserTracking, initDefaultTrackers, STANDARD_CONTEXT } from '~/tracking'; jest.mock('~/experimentation/utils', () => ({ getExperimentData: jest.fn() })); describe('Tracking', () => { let snowplowSpy; let bindDocumentSpy; let trackLoadEventsSpy; beforeEach(() => { getExperimentData.mockReturnValue(undefined); window.snowplow = window.snowplow || (() => {}); window.snowplowOptions = { namespace: '_namespace_', hostname: 'app.gitfoo.com', cookieDomain: '.gitfoo.com', }; snowplowSpy = jest.spyOn(window, 'snowplow'); }); describe('initUserTracking', () => { it('calls through to get a new tracker with the expected options', () => { initUserTracking(); expect(snowplowSpy).toHaveBeenCalledWith('newTracker', '_namespace_', 'app.gitfoo.com', { namespace: '_namespace_', hostname: 'app.gitfoo.com', cookieDomain: '.gitfoo.com', appId: '', userFingerprint: false, respectDoNotTrack: true, forceSecureTracker: true, eventMethod: 'post', contexts: { webPage: true, performanceTiming: true }, formTracking: false, linkClickTracking: false, pageUnloadTimer: 10, }); }); }); describe('initDefaultTrackers', () => { beforeEach(() => { bindDocumentSpy = jest.spyOn(Tracking, 'bindDocument').mockImplementation(() => null); trackLoadEventsSpy = jest.spyOn(Tracking, 'trackLoadEvents').mockImplementation(() => null); }); it('should activate features based on what has been enabled', () => { initDefaultTrackers(); expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30); expect(snowplowSpy).toHaveBeenCalledWith('trackPageView', null, [STANDARD_CONTEXT]); expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking'); expect(snowplowSpy).not.toHaveBeenCalledWith('enableLinkClickTracking'); window.snowplowOptions = { ...window.snowplowOptions, formTracking: true, linkClickTracking: true, }; initDefaultTrackers(); expect(snowplowSpy).toHaveBeenCalledWith('enableFormTracking'); expect(snowplowSpy).toHaveBeenCalledWith('enableLinkClickTracking'); }); it('binds the document event handling', () => { initDefaultTrackers(); expect(bindDocumentSpy).toHaveBeenCalled(); }); it('tracks page loaded events', () => { initDefaultTrackers(); expect(trackLoadEventsSpy).toHaveBeenCalled(); }); }); describe('.event', () => { afterEach(() => { window.doNotTrack = undefined; navigator.doNotTrack = undefined; navigator.msDoNotTrack = undefined; }); describe('builds the standard context', () => { let standardContext; 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(standardContext.schema).toBe('iglu:com.gitlab/gitlab_standard'); expect(standardContext.data.environment).toBe('testing'); }); it('overrides schema source', () => { expect(standardContext.data.source).toBe('gitlab-javascript'); }); }); it('tracks to snowplow (our current tracking system)', () => { Tracking.event('_category_', '_eventName_', { label: '_label_' }); expect(snowplowSpy).toHaveBeenCalledWith( 'trackStructEvent', '_category_', '_eventName_', '_label_', undefined, undefined, [STANDARD_CONTEXT], ); }); it('skips tracking if snowplow is unavailable', () => { window.snowplow = false; Tracking.event('_category_', '_eventName_'); expect(snowplowSpy).not.toHaveBeenCalled(); }); it('skips tracking if the user does not want to be tracked (general spec)', () => { window.doNotTrack = '1'; Tracking.event('_category_', '_eventName_'); expect(snowplowSpy).not.toHaveBeenCalled(); }); it('skips tracking if the user does not want to be tracked (firefox legacy)', () => { navigator.doNotTrack = 'yes'; Tracking.event('_category_', '_eventName_'); expect(snowplowSpy).not.toHaveBeenCalled(); }); it('skips tracking if the user does not want to be tracked (IE legacy)', () => { navigator.msDoNotTrack = '1'; Tracking.event('_category_', '_eventName_'); expect(snowplowSpy).not.toHaveBeenCalled(); }); }); 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', () => { it('flushes any pending events', () => { Tracking.initialized = false; Tracking.event('_category_', '_eventName_', { label: '_label_' }); expect(snowplowSpy).not.toHaveBeenCalled(); Tracking.flushPendingEvents(); expect(snowplowSpy).toHaveBeenCalledWith( 'trackStructEvent', '_category_', '_eventName_', '_label_', undefined, undefined, [STANDARD_CONTEXT], ); }); }); describe.each` term ${'event'} ${'action'} `('tracking interface events with data-track-$term', ({ term }) => { let eventSpy; beforeEach(() => { eventSpy = jest.spyOn(Tracking, 'event'); Tracking.bindDocument('_category_'); // only happens once setHTMLFixture(`