Skip to content

Commit

Permalink
DOM event plugin add data-rum-id attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Ömer Aslan authored and Ömer Aslan committed May 20, 2022
1 parent 6cec9da commit f45180a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
10 changes: 9 additions & 1 deletion app/dom_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

function registerDomEvents() {
cwr('registerDomEvents', [
{ event: 'click', cssLocator: '[label="label2"]' }
{ event: 'click', cssLocator: '[label="label2"]' },
{ event: 'click', cssLocator: '[label^="button-"]' }
]);
}
</script>
Expand Down Expand Up @@ -90,6 +91,13 @@
</button>
<button id="button5" label="label2">Button Five</button>
<hr />
<button id="button6" data-rum-id="button-six" label="button-label1">
Button Six
</button>
<button id="button7" data-rum-id="button-seven" label="button-label2">
<span>Button Seven</span>
</button>
<hr />
<button id="dispatch" onclick="dispatch()">Dispatch</button>
<button id="clearRequestResponse" onclick="clearRequestResponse()">
Clear
Expand Down
10 changes: 9 additions & 1 deletion app/dom_event_mutation_observer_enabled.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

function registerDomEvents() {
cwr('registerDomEvents', [
{ event: 'click', cssLocator: '[label="label2"]' }
{ event: 'click', cssLocator: '[label="label2"]' },
{ event: 'click', cssLocator: '[label^="button-"]' }
]);
}
</script>
Expand Down Expand Up @@ -90,6 +91,13 @@
</button>
<button id="button5" label="label2">Button Five</button>
<hr />
<button id="button6" data-rum-id="button-six" label="button-label1">
Button Six
</button>
<button id="button7" data-rum-id="button-seven" label="button-label2">
<span>Button Seven</span>
</button>
<hr />
<button id="dispatch" onclick="dispatch()">Dispatch</button>
<button id="clearRequestResponse" onclick="clearRequestResponse()">
Clear
Expand Down
4 changes: 4 additions & 0 deletions src/event-schemas/dom-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"cssLocator": {
"type": "string",
"description": "CSS Locator string."
},
"elementRumId": {
"type": "string",
"description": "DOM element data-rum-id attribute."
}
},
"additionalProperties": false,
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/event-plugins/DomEventPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,30 @@ export class DomEventPlugin implements Plugin {
};
if (cssLocator !== undefined) {
eventData.cssLocator = cssLocator;
const elementRumId = this.getElementRumId(event, cssLocator);
if (elementRumId) {
eventData.elementRumId = elementRumId;
}
}
if (this.recordEvent) {
this.recordEvent(DOM_EVENT_TYPE, eventData);
}
};
}

private getElementRumId(event: Event, cssLocator: string): string {
if (event.composedPath) {
const eventPath = event.composedPath();
const firstMatchingElement = eventPath.find((element: Element) =>
element.matches(cssLocator)
) as Element;
if (firstMatchingElement.hasAttribute('data-rum-id')) {
return firstMatchingElement.getAttribute('data-rum-id');
}
}
return '';
}

private getElementId(event: Event) {
if (!event.target) {
return '';
Expand Down

0 comments on commit f45180a

Please sign in to comment.