From a224df43af1abd506898befee7be52eca7600cb1 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 23 Jul 2018 15:03:34 +0100 Subject: [PATCH] feat(docs-infra): support sending Google Analytics events (#25042) PR Close #25042 --- aio/src/app/shared/ga.service.spec.ts | 7 +++++++ aio/src/app/shared/ga.service.ts | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/aio/src/app/shared/ga.service.spec.ts b/aio/src/app/shared/ga.service.spec.ts index be3c80cc59..ffd95fbc45 100644 --- a/aio/src/app/shared/ga.service.spec.ts +++ b/aio/src/app/shared/ga.service.spec.ts @@ -74,6 +74,13 @@ describe('GaService', () => { }); }); + describe('sendEvent', () => { + it('should send "event" with associated data', () => { + gaService.sendEvent('some source', 'some campaign', 'a label', 45); + expect(gaSpy).toHaveBeenCalledWith('send', 'event', 'some source', 'some campaign', 'a label', 45); + }); + }); + it('should support replacing the `window.ga` function', () => { const gaSpy2 = jasmine.createSpy('new ga'); mockWindow.ga = gaSpy2; diff --git a/aio/src/app/shared/ga.service.ts b/aio/src/app/shared/ga.service.ts index 122bfa4a93..65318f2a20 100644 --- a/aio/src/app/shared/ga.service.ts +++ b/aio/src/app/shared/ga.service.ts @@ -29,6 +29,10 @@ export class GaService { this.ga('send', 'pageview'); } + sendEvent(source: string, action: string, label?: string, value?: number) { + this.ga('send', 'event', source, action, label, value); + } + ga(...args: any[]) { const gaFn = (this.window as any)['ga']; if (gaFn) {