-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track commercial events in GA
- Loading branch information
Showing
7 changed files
with
167 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
dist | ||
coverage | ||
.DS_Store | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { EventTimer } from './EventTimer'; | ||
import { trackEvent } from './GoogleAnalytics'; | ||
|
||
jest.mock('./GoogleAnalytics'); | ||
|
||
describe('EventTimer', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- delete performance to mock it as is readonly | ||
delete (window as any).performance; | ||
|
||
const performance = { | ||
now: jest.fn(), | ||
mark: jest.fn(), | ||
getEntriesByName: jest.fn().mockReturnValue([ | ||
{ | ||
duration: 1, | ||
entryType: 'mark', | ||
name: 'commercial event', | ||
startTime: 1, | ||
}, | ||
]), | ||
}; | ||
|
||
Object.defineProperty(window, 'performance', { | ||
configurable: true, | ||
enumerable: true, | ||
value: performance, | ||
writable: true, | ||
}); | ||
|
||
beforeEach(() => { | ||
window.guardian = { | ||
config: { | ||
googleAnalytics: { | ||
trackers: { | ||
editorial: 'gaTrackerTest', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
EventTimer.init(); | ||
}); | ||
|
||
it('trigger first slotReady event', () => { | ||
const eventTimer = EventTimer.get(); | ||
eventTimer.trigger('slotReady', 'inline1'); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method -- for test | ||
expect(window.performance.mark).toHaveBeenCalledWith( | ||
'gu.commercial.first-slotReady', | ||
); | ||
expect(trackEvent).toHaveBeenCalledWith( | ||
'gu.commercial.slotReady', | ||
'first-slotReady', | ||
'new', | ||
); | ||
}); | ||
|
||
it('trigger top-above-nav slotReady event', () => { | ||
const eventTimer = EventTimer.get(); | ||
eventTimer.trigger('slotReady', 'top-above-nav'); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method -- for test | ||
expect(window.performance.mark).toHaveBeenCalledWith( | ||
'gu.commercial.top-above-nav-slotReady', | ||
); | ||
expect(trackEvent).toHaveBeenCalledWith( | ||
'gu.commercial.slotReady', | ||
'top-above-nav-slotReady', | ||
'new', | ||
); | ||
}); | ||
|
||
it('not trigger a GA event if not in GA config', () => { | ||
const eventTimer = EventTimer.get(); | ||
eventTimer.trigger('adOnPage', 'inline1'); | ||
// eslint-disable-next-line @typescript-eslint/unbound-method -- for test | ||
expect(window.performance.mark).toHaveBeenCalledWith( | ||
'gu.commercial.first-adOnPage', | ||
); | ||
expect(trackEvent).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const trackEvent = ( | ||
timingCategory: string, | ||
timingVar: string, | ||
timingLabel: string, | ||
): void => { | ||
const { ga, guardian } = window; | ||
const trackerName: string | undefined = | ||
guardian.config?.googleAnalytics?.trackers.editorial; | ||
if (typeof ga === 'undefined' || typeof trackerName === 'undefined') { | ||
console.error( | ||
"Can't track GA event - GA library not loaded or no tracker found", | ||
); | ||
return; | ||
} | ||
const timeSincePageLoad: number = Math.round(window.performance.now()); | ||
|
||
const send = `${trackerName}.send`; | ||
window.ga( | ||
send, | ||
'timing', | ||
timingCategory, | ||
timingVar, | ||
timeSincePageLoad, | ||
timingLabel, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters