Add event tracking to window.GOVUK.analytics

app/assets/javascripts/errorTracking.js sent
events to `window.ga`.

This extends the API of `window.GOVUK.Analytics`
to include support for sending events so all
calls to `window.ga` can use it instead of direct
access.

This use of `window.ga` was missed from the
initial work on `window.GOVUK.Anaytics`.
This commit is contained in:
Tom Byers
2020-01-10 11:02:17 +00:00
parent c8f36683ed
commit 1a97c6028c
4 changed files with 73 additions and 17 deletions

View File

@@ -18,18 +18,23 @@ describe('Error tracking', () => {
afterEach(() => {
document.body.innerHTML = '';
delete window.GOVUK.analytics;
});
test("It should send the right data to Google Analytics", () => {
test("If there is an analytics tracker set up, it should send details of the error to window.GOVUK.analytic", () => {
window.ga = jest.fn(() => {});
window.GOVUK.analytics = {
'trackEvent': jest.fn()
};
// start the module
window.GOVUK.modules.start();
expect(window.ga).toHaveBeenCalled();
expect(window.ga.mock.calls[0]).toEqual(['send', 'event', 'Error', 'validation', 'missing field']);
expect(window.GOVUK.analytics.trackEvent).toHaveBeenCalled();
expect(window.GOVUK.analytics.trackEvent.mock.calls[0]).toEqual(['Error', 'validation', {
'label': 'missing field'
}]);
});