@@ -97,6 +97,25 @@ namespace GleanMetrics {
9797 } ) ;
9898 }
9999
100+ /**
101+ * Construct click event context for a given element.
102+ *
103+ * The element should have at least one of the data-glean-{x} attributes where x corresponds
104+ * to the keys of ElementClickEventContext).
105+ *
106+ * @param element Element object.
107+ * @returns ElementClickEventContext
108+ */
109+ function constructClickEventContextForElement ( element : Element ) {
110+ const dataset = ( element as HTMLElement ) . dataset ;
111+ const elementClickEventContext : ElementClickEventContext = {
112+ ...( dataset . gleanId && { id : dataset . gleanId } ) ,
113+ ...( dataset . gleanType && { type : dataset . gleanType } ) ,
114+ ...( dataset . gleanLabel && { label : dataset . gleanLabel } )
115+ } ;
116+ return elementClickEventContext ;
117+ }
118+
100119 /**
101120 * Handler for "click" events on a document.
102121 *
@@ -106,14 +125,15 @@ namespace GleanMetrics {
106125 * @param event Event object.
107126 */
108127 export function handleClickEvent ( event : Event ) {
109- const htmlElement = event . target as HTMLElement ;
128+ const clickedElement = event . target as Element ;
129+ const closestElementWithClickAttributes : Element | null = clickedElement . closest ( "[data-glean-id], [data-glean-type], [data-glean-label]" ) ;
110130
111- const elementClickEventContext : ElementClickEventContext = { } ;
112- if ( htmlElement ?. dataset ?. gleanId ) elementClickEventContext . id = htmlElement ?. dataset ?. gleanId ;
113- if ( htmlElement ?. dataset ?. gleanType ) elementClickEventContext . type = htmlElement ?. dataset ?. gleanType ;
114- if ( htmlElement ?. dataset ?. gleanLabel ) elementClickEventContext . label = htmlElement ?. dataset ?. gleanLabel ;
131+ if ( ! closestElementWithClickAttributes ) {
132+ return ;
133+ }
115134
116- if ( Object . keys ( elementClickEventContext ) . length > 0 ) recordElementClick ( elementClickEventContext ) ;
135+ const elementClickEventContext : ElementClickEventContext = constructClickEventContextForElement ( closestElementWithClickAttributes ) ;
136+ recordElementClick ( elementClickEventContext ) ;
117137 }
118138
119139 /**
0 commit comments