Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 46c96de

Browse files
committed
Trace interactions with a name
1 parent 60e7d71 commit 46c96de

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

packages/opencensus-web-instrumentation-zone/src/interaction-tracker.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,25 @@ export class InteractionTracker {
5555
) => {
5656
console.warn('Running task');
5757
console.log(task);
58-
console.log(task.zone);
5958

6059
const interceptingElement = getTrackedElement(task);
60+
const interactionName = resolveInteractionName(
61+
interceptingElement,
62+
task.eventName
63+
);
6164
let taskZone = Zone.current;
6265
if (interceptingElement) {
63-
console.log('Click detected');
64-
65-
if (this.currentEventTracingZone === undefined) {
66-
this.startNewInteraction(interceptingElement, task.eventName);
67-
}
68-
// Change the zone task.
69-
if (this.currentEventTracingZone) {
66+
if (this.currentEventTracingZone === undefined && interactionName) {
67+
this.startNewInteraction(
68+
interceptingElement,
69+
task.eventName,
70+
interactionName
71+
);
72+
} else if (this.currentEventTracingZone) {
7073
task._zone = this.currentEventTracingZone;
7174
taskZone = this.currentEventTracingZone;
75+
} else {
76+
task._zone = Zone.root;
7277
}
7378
this.incrementTaskCount(getTraceId(task.zone));
7479
} else if (isTrackedTask(task)) {
@@ -130,11 +135,12 @@ export class InteractionTracker {
130135

131136
private startNewInteraction(
132137
interceptingElement: HTMLElement,
133-
eventName: string
138+
eventName: string,
139+
interactionName: string
134140
) {
135141
const traceId = randomTraceId();
136142
const spanOptions = {
137-
name: resolveInteractionName(interceptingElement, eventName),
143+
name: interactionName,
138144
spanContext: {
139145
traceId,
140146
// This becomes the parentSpanId field of the root span, and the actual
@@ -271,12 +277,14 @@ function isTrackedTask(task: Task): boolean {
271277
* not present, use the element ID, tag name, event that triggered the interaction.
272278
* Thus, the resulting interaction name will be: "tag_name> id:'ID' event"
273279
* (e.g. "<BUTTON> id:'save_changes' click").
280+
* In case the the name is not resolvable, return empty string (e.g. element is the document).
274281
* @param element
275282
*/
276283
function resolveInteractionName(
277-
element: HTMLElement,
284+
element: HTMLElement | null,
278285
eventName: string
279286
): string {
287+
if (!element) return '';
280288
if (!element.getAttribute) return '';
281289
if (element.hasAttribute('disabled')) {
282290
return '';

0 commit comments

Comments
 (0)