Commit dd8ee2cc authored by Doug Stull's avatar Doug Stull

Defer snowplow loading until rest of dom is loaded

- the work being done in snowplow initialization
  was slowing down dom load times and deferring
  it here should speed it up a bit.
parent 85c3aeff
...@@ -37,7 +37,7 @@ import GlFieldErrors from './gl_field_errors'; ...@@ -37,7 +37,7 @@ import GlFieldErrors from './gl_field_errors';
import initUserPopovers from './user_popovers'; import initUserPopovers from './user_popovers';
import initBroadcastNotifications from './broadcast_notification'; import initBroadcastNotifications from './broadcast_notification';
import initPersistentUserCallouts from './persistent_user_callouts'; import initPersistentUserCallouts from './persistent_user_callouts';
import { initUserTracking } from './tracking'; import { initUserTracking, initDefaultTrackers } from './tracking';
import { __ } from './locale'; import { __ } from './locale';
import 'ee_else_ce/main_ee'; import 'ee_else_ce/main_ee';
...@@ -111,6 +111,7 @@ function deferredInitialisation() { ...@@ -111,6 +111,7 @@ function deferredInitialisation() {
initBroadcastNotifications(); initBroadcastNotifications();
initFrequentItemDropdowns(); initFrequentItemDropdowns();
initPersistentUserCallouts(); initPersistentUserCallouts();
initDefaultTrackers();
if (document.querySelector('.search')) initSearchAutocomplete(); if (document.querySelector('.search')) initSearchAutocomplete();
......
...@@ -126,14 +126,18 @@ export function initUserTracking() { ...@@ -126,14 +126,18 @@ export function initUserTracking() {
const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions }; const opts = { ...DEFAULT_SNOWPLOW_OPTIONS, ...window.snowplowOptions };
window.snowplow('newTracker', opts.namespace, opts.hostname, opts); window.snowplow('newTracker', opts.namespace, opts.hostname, opts);
document.dispatchEvent(new Event('SnowplowInitialized'));
}
export function initDefaultTrackers() {
if (!Tracking.enabled()) return;
window.snowplow('enableActivityTracking', 30, 30); window.snowplow('enableActivityTracking', 30, 30);
window.snowplow('trackPageView'); // must be after enableActivityTracking window.snowplow('trackPageView'); // must be after enableActivityTracking
if (opts.formTracking) window.snowplow('enableFormTracking'); if (window.snowplowOptions.formTracking) window.snowplow('enableFormTracking');
if (opts.linkClickTracking) window.snowplow('enableLinkClickTracking'); if (window.snowplowOptions.linkClickTracking) window.snowplow('enableLinkClickTracking');
Tracking.bindDocument(); Tracking.bindDocument();
Tracking.trackLoadEvents(); Tracking.trackLoadEvents();
document.dispatchEvent(new Event('SnowplowInitialized'));
} }
---
title: Defer (certain) parts of setting up snowplow telemetry
merge_request: 40299
author:
type: performance
import { setHTMLFixture } from './helpers/fixtures'; import { setHTMLFixture } from './helpers/fixtures';
import Tracking, { initUserTracking } from '~/tracking'; import Tracking, { initUserTracking, initDefaultTrackers } from '~/tracking';
describe('Tracking', () => { describe('Tracking', () => {
let snowplowSpy; let snowplowSpy;
...@@ -17,11 +17,6 @@ describe('Tracking', () => { ...@@ -17,11 +17,6 @@ describe('Tracking', () => {
}); });
describe('initUserTracking', () => { describe('initUserTracking', () => {
beforeEach(() => {
bindDocumentSpy = jest.spyOn(Tracking, 'bindDocument').mockImplementation(() => null);
trackLoadEventsSpy = jest.spyOn(Tracking, 'trackLoadEvents').mockImplementation(() => null);
});
it('calls through to get a new tracker with the expected options', () => { it('calls through to get a new tracker with the expected options', () => {
initUserTracking(); initUserTracking();
expect(snowplowSpy).toHaveBeenCalledWith('newTracker', '_namespace_', 'app.gitfoo.com', { expect(snowplowSpy).toHaveBeenCalledWith('newTracker', '_namespace_', 'app.gitfoo.com', {
...@@ -38,9 +33,16 @@ describe('Tracking', () => { ...@@ -38,9 +33,16 @@ describe('Tracking', () => {
linkClickTracking: false, linkClickTracking: false,
}); });
}); });
});
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', () => { it('should activate features based on what has been enabled', () => {
initUserTracking(); initDefaultTrackers();
expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30); expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30);
expect(snowplowSpy).toHaveBeenCalledWith('trackPageView'); expect(snowplowSpy).toHaveBeenCalledWith('trackPageView');
expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking'); expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking');
...@@ -52,18 +54,18 @@ describe('Tracking', () => { ...@@ -52,18 +54,18 @@ describe('Tracking', () => {
linkClickTracking: true, linkClickTracking: true,
}; };
initUserTracking(); initDefaultTrackers();
expect(snowplowSpy).toHaveBeenCalledWith('enableFormTracking'); expect(snowplowSpy).toHaveBeenCalledWith('enableFormTracking');
expect(snowplowSpy).toHaveBeenCalledWith('enableLinkClickTracking'); expect(snowplowSpy).toHaveBeenCalledWith('enableLinkClickTracking');
}); });
it('binds the document event handling', () => { it('binds the document event handling', () => {
initUserTracking(); initDefaultTrackers();
expect(bindDocumentSpy).toHaveBeenCalled(); expect(bindDocumentSpy).toHaveBeenCalled();
}); });
it('tracks page loaded events', () => { it('tracks page loaded events', () => {
initUserTracking(); initDefaultTrackers();
expect(trackLoadEventsSpy).toHaveBeenCalled(); expect(trackLoadEventsSpy).toHaveBeenCalled();
}); });
}); });
......
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