-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Event Timer module #154
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f08421d
initial commit for eventTimer
d80933c
turns out the slot is called 'top-above-nav'
b8f15c1
added init function
d701f4b
rename because osx didn't rename it last time!
cf1328c
logical OR is more concise
ashishpuliyel 6cd12e6
prettier didn't like the last one
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
class Event { | ||
name: string; | ||
ts: DOMHighResTimeStamp; | ||
|
||
constructor(name: string, mark: PerformanceEntry) { | ||
this.name = name; | ||
this.ts = mark.startTime; | ||
} | ||
} | ||
|
||
interface SlotEventStatus { | ||
prebidStart: boolean; | ||
prebidEnd: boolean; | ||
slotInitalised: boolean; | ||
adOnPage: boolean; | ||
} | ||
|
||
export class EventTimer { | ||
events: Event[]; | ||
startTS: DOMHighResTimeStamp; | ||
triggers: { | ||
first: SlotEventStatus; | ||
'top-above-nav': SlotEventStatus; | ||
}; | ||
|
||
/** | ||
* Initalise the EventTimer class on page. | ||
* Returns a singleton instance of the EventTimer class and binds | ||
* to window.guardian.commercialTimer. If it's been previously | ||
* initalised and bound it returns the original instance | ||
* | ||
* @returns {EventTimer} Instance of EventTimer | ||
*/ | ||
static init(): EventTimer { | ||
return (window.guardian.commercialTimer ||= new EventTimer()); | ||
} | ||
|
||
constructor() { | ||
this.events = []; | ||
this.startTS = performance.now(); | ||
this.triggers = { | ||
first: { | ||
prebidStart: false, | ||
prebidEnd: false, | ||
slotInitalised: false, | ||
adOnPage: false, | ||
}, | ||
'top-above-nav': { | ||
prebidStart: false, | ||
prebidEnd: false, | ||
slotInitalised: false, | ||
adOnPage: false, | ||
}, | ||
}; | ||
} | ||
|
||
mark(name: string): PerformanceEntry { | ||
const longName = `gu.commercial.${name}`; | ||
performance.mark(longName); | ||
|
||
// Most recent mark with this name is the event we just created. | ||
const mark = performance | ||
.getEntriesByName(longName, 'mark') | ||
.slice(-1)[0]; | ||
this.events.push(new Event(name, mark)); | ||
return mark; | ||
} | ||
|
||
/** | ||
* Creates a new performance mark | ||
* For slot events also ensures each TYPE of event event is marked only once for 'first' | ||
* (the first time that event is triggered for ANY slot) and once for topAboveNav | ||
* | ||
* @param {string} eventName - The short name applied to the mark | ||
* @param {origin} [origin=page] - Either 'page' (default) or the name of the slot | ||
*/ | ||
trigger(eventName: string, origin = 'page'): void { | ||
const TRACKEDSLOTNAME = 'top-above-nav'; | ||
if (origin === 'page') { | ||
this.mark(eventName); | ||
return; | ||
} | ||
|
||
if (!this.triggers.first[eventName as keyof SlotEventStatus]) { | ||
this.mark(`first-{eventName}`); | ||
this.triggers.first[eventName as keyof SlotEventStatus] = true; | ||
} | ||
|
||
if (origin === TRACKEDSLOTNAME) { | ||
if ( | ||
!this.triggers[TRACKEDSLOTNAME][ | ||
eventName as keyof SlotEventStatus | ||
] | ||
) { | ||
this.mark(`${TRACKEDSLOTNAME}-{eventName}`); | ||
this.triggers[TRACKEDSLOTNAME][ | ||
eventName as keyof SlotEventStatus | ||
] = true; | ||
} | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be extracted in a higher scope? It could be used in the triggers on lines 23 and 51.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can it be used on line 23?