-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add keyboard input breadcrumbs. (#513)
Prototype implementation.
- Loading branch information
1 parent
41b03e3
commit 3c79e47
Showing
8 changed files
with
103 additions
and
16 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
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
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
16 changes: 1 addition & 15 deletions
16
packages/telemetry/browser-telemetry/src/collectors/dom/ClickCollector.ts
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
63 changes: 63 additions & 0 deletions
63
packages/telemetry/browser-telemetry/src/collectors/dom/KeypressCollector.ts
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,63 @@ | ||
import { Breadcrumb, UiBreadcrumb } from '../../api/Breadcrumb'; | ||
import { BrowserTelemetry } from '../../api/BrowserTelemetry'; | ||
import { Collector } from '../../api/Collector'; | ||
import getTarget from './getTarget'; | ||
import toSelector from './toSelector'; | ||
|
||
const THROTTLE_TIME_MS = 1000; | ||
|
||
const INPUT_TAG_NAMES = ['INPUT', 'TEXTAREA']; | ||
|
||
/** | ||
* Collects mouse click events and adds them as breadcrumbs. | ||
*/ | ||
export default class KeypressCollector implements Collector { | ||
private destination?: BrowserTelemetry; | ||
private lastEvent?: UiBreadcrumb; | ||
|
||
constructor() { | ||
window.addEventListener( | ||
'keypress', | ||
(event: KeyboardEvent) => { | ||
const target = getTarget(event); | ||
const htmlElement = target as HTMLElement; | ||
// An example of `isContentEditable` would be an editable <p> tag. | ||
// Input and textarea tags do not have the isContentEditable property. | ||
if ( | ||
target && | ||
(INPUT_TAG_NAMES.includes(target.tagName) || htmlElement?.isContentEditable) | ||
) { | ||
const breadcrumb: UiBreadcrumb = { | ||
class: 'ui', | ||
type: 'input', | ||
level: 'info', | ||
timestamp: Date.now(), | ||
message: toSelector(target), | ||
}; | ||
|
||
if (!this.shouldDeduplicate(breadcrumb)) { | ||
this.destination?.addBreadcrumb(breadcrumb); | ||
this.lastEvent = breadcrumb; | ||
} | ||
} | ||
}, | ||
true, | ||
); | ||
} | ||
|
||
register(telemetry: BrowserTelemetry): void { | ||
this.destination = telemetry; | ||
} | ||
unregister(): void { | ||
this.destination = undefined; | ||
} | ||
|
||
private shouldDeduplicate(crumb: Breadcrumb): boolean { | ||
// TODO: Consider de-duplication at the dom level. | ||
if (this.lastEvent) { | ||
const timeDiff = Math.abs(crumb.timestamp - this.lastEvent.timestamp); | ||
return this.lastEvent.message === crumb.message && timeDiff <= THROTTLE_TIME_MS; | ||
} | ||
return false; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/telemetry/browser-telemetry/src/collectors/dom/getTarget.ts
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,14 @@ | ||
/** | ||
* Get the event target. This is wrapped because in some situations a browser may throw when | ||
* accessing the event target. | ||
* | ||
* @param event The event to get the target from. | ||
* @returns The event target, or undefined if one is not available. | ||
*/ | ||
export default function getTarget(event: { target: any }): Element | undefined { | ||
try { | ||
return event.target as Element; | ||
} catch { | ||
return undefined; | ||
} | ||
} |
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