Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { LightPlugin, LightClient, Transaction, Span, SpanStatus, LightConfig } from '../types';
import { now, isObject } from '../utils/helper';
interface InteractionPluginConfig {
enable?: boolean;
elementAttribute?: string;
maxTransactionDurationMs?: number;
idleTimeoutMs?: number;
captureScroll?: boolean;
trackClick?: boolean;
trackSubmit?: boolean;
trackKeypress?: boolean;
}
const DEFAULT_TRACKING_TYPES = ['click', 'submit'];
class InteractionPlugin implements LightPlugin {
name = 'interaction';
version = '1.0.0';
private client!: LightClient;
private config: InteractionPluginConfig = {
enable: true,
elementAttribute: 'data-sentry-component',
maxTransactionDurationMs: 30000,
idleTimeoutMs: 1000,
trackClick: true,
trackSubmit: true,
trackKeypress: false,
};
private currentTransaction: Transaction | null = null;
private idleTimer: ReturnType<typeof setTimeout> | null = null;
private maxDurationTimer: ReturnType<typeof setTimeout> | null = null;
private debounceTimer: ReturnType<typeof setTimeout> | null = null;
setup(client: LightClient): void {
this.client = client;
this.loadConfig();
if (typeof window === 'undefined') return;
if (!this.config.enable) return;
if (this.config.trackClick) {
document.addEventListener('click', this.handleClick, true);
}
if (this.config.trackSubmit) {
document.addEventListener('submit', this.handleSubmit, true);
}
}
private loadConfig(): void {
const clientConfig = this.client.config as LightConfig;
if (clientConfig && isObject((clientConfig as Record<string, unknown>).interaction)) {
this.config = { ...this.config, ...((clientConfig as Record<string, unknown>).interaction as object) };
}
}
private handleClick = (event: Event): void => {
if (this.isEventIgnored(event)) return;
const target = event.target as HTMLElement;
const elementName = this.getElementName(target, 'click');
this.scheduleInteractionTransaction(elementName, 'click', target);
};
private handleSubmit = (event: Event): void => {
if (this.isEventIgnored(event)) return;
const target = event.target as HTMLElement;
const elementName = this.getElementName(target, 'submit');
this.scheduleInteractionTransaction(elementName, 'submit', target);
};
private isEventIgnored(event: Event): boolean {
const target = event.target as HTMLElement;
if (!target) return true;
if (target.hasAttribute && target.hasAttribute('data-sentry-ignore')) {
return true;
}
return false;
}
private scheduleInteractionTransaction(name: string, op: string, element: HTMLElement): void {
if (this.debounceTimer) {
clearTimeout(this.debounceTimer);
}
this.debounceTimer = setTimeout(() => {
this.startInteractionTransaction(name, op, element);
}, 0);
}
private getElementName(element: HTMLElement | null, eventType: string): string {
if (!element) return eventType;
const attr = this.config.elementAttribute || 'data-sentry-component';
const componentName = element.getAttribute(attr);
if (componentName) return componentName;
const tag = element.tagName?.toLowerCase() || 'element';
const id = element.id ? `#${element.id}` : '';
const classes = element.className && typeof element.className === 'string'
? `.${element.className.split(' ').slice(0, 3).join('.')}`
: '';
return `${eventType}@${tag}${id}${classes}`;
}
private startInteractionTransaction(name: string, op: string, element: HTMLElement): void {
if (!this.client) return;
if (this.currentTransaction) {
this.finishCurrentTransaction('ok');
}
const transaction = this.client.startTransaction({
name,
op: `ui.${op}`,
description: `User ${op} on ${name}`,
type: 'user-interaction',
tags: {
'interaction.type': op,
'interaction.target': name,
},
data: {
tag: element.tagName?.toLowerCase(),
id: element.id || undefined,
text: element.textContent?.substring(0, 100) || undefined,
},
});
if (transaction) {
this.currentTransaction = transaction;
this.scheduleIdleFinish();
this.scheduleMaxDuration();
}
}
private scheduleIdleFinish(): void {
if (this.idleTimer) {
clearTimeout(this.idleTimer);
}
const idleTimeout = this.config.idleTimeoutMs ?? 1000;
this.idleTimer = setTimeout(() => {
this.finishCurrentTransaction('ok');
}, idleTimeout);
}
private scheduleMaxDuration(): void {
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
}
const maxDuration = this.config.maxTransactionDurationMs ?? 30000;
this.maxDurationTimer = setTimeout(() => {
this.finishCurrentTransaction('deadline_exceeded');
}, maxDuration);
}
private finishCurrentTransaction(status: SpanStatus = 'ok'): void {
if (this.idleTimer) {
clearTimeout(this.idleTimer);
this.idleTimer = null;
}
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
this.maxDurationTimer = null;
}
if (this.debounceTimer) {
clearTimeout(this.debounceTimer);
this.debounceTimer = null;
}
if (this.currentTransaction) {
this.currentTransaction.setStatus(status);
this.currentTransaction.finish();
this.currentTransaction = null;
}
}
destroy(): void {
if (typeof document !== 'undefined') {
document.removeEventListener('click', this.handleClick, true);
document.removeEventListener('submit', this.handleSubmit, true);
}
if (this.idleTimer) {
clearTimeout(this.idleTimer);
this.idleTimer = null;
}
if (this.maxDurationTimer) {
clearTimeout(this.maxDurationTimer);
this.maxDurationTimer = null;
}
if (this.debounceTimer) {
clearTimeout(this.debounceTimer);
this.debounceTimer = null;
}
this.finishCurrentTransaction('aborted');
}
}
export default InteractionPlugin;
|