-
Recording the Visibility of a Shadow DOM Element in the Browser
Hey there! I’m trying to figure out a way to fire an event to GA4 when a user brings up an LWC div element by clicking a button (like an error text pop-up). This element is tucked away in Shadow DOM (think Salesforce greetings), and the usual GTM elementVisibility trigger won’t detect it as the querySelector method turns up nothing.
On my end, though, I can spot it using the DevTools console by running this:
`javascript
document.querySelector(‘c-cp-login’).shadowRoot.querySelector(‘.slds-form-element.cp-padding-left-right.cp-error_text’);
`
I’ve also cooked up a GTM custom JavaScript variable that can pull up the path of this element or the exact text of the element if I add in a.textContent
property to the selector:`javascript
“HTMLDivElement: div.slds-grid.slds-grid_vertical.slds-grid_vertical-” +
“stretch.cp-top-container.cp-Box > div.slds-col > div.slds-form-element.cp-padding-left-right.cp-error_text”
`
So here’s my challenge: Can I set up a dataLayer push to fire an event when this rogue element shows itself using a custom HTML tag in GTM? Any thoughts or suggestions would be really appreciated. Thanks!
Log in to reply.