Duplicate event listeners across tags
Multiple custom HTML tags add listeners for the same DOM events, which can cause race conditions and double-firing.
Summary
- rule_id —
duplicate-event-listeners - purpose — Detect multiple Custom HTML tags registering listeners for the same DOM event
- effect — Duplicate event firing, inflated dataLayer pushes, and unnecessary JS execution on every interaction
- layer — container
- reversibility — non-destructive
What fires this
The rule reads codeAnalysis.eventListeners from each active, non-paused Custom HTML tag. It builds a map from event name to the list of tags that register that listener. Within a single tag, each event type is counted once — a tag registering the same event twice in its own code does not self-flag. The rule fires when any event type appears in two or more distinct tags.
Why it matters
Each registered listener fires independently on every matching event. Two tags both listening for submit each push to the dataLayer on the same form submission — downstream tags receive two pushes and fire twice. This inflates event counts, distorts conversion reports, and adds JavaScript execution cost on every interaction. GTM's built-in trigger system exists specifically to centralize event detection: one listener, many tags.
Before / after
// Tag A — GA4 form tracking
document.addEventListener('click', function(e) {
if (e.target.matches('button[type="submit"]')) {
dataLayer.push({ event: 'form_submit_ga4' })
}
})
// Tag B — HubSpot form tracking (separate tag)
document.addEventListener('click', function(e) {
if (e.target.matches('button[type="submit"]')) {
dataLayer.push({ event: 'form_submit_hs' })
}
})
// Tag A — central listener only
document.addEventListener('click', function(e) {
if (e.target.matches('button[type="submit"]')) {
dataLayer.push({ event: 'form_submit' })
}
})
// Tag B fires on GTM Custom Event trigger "form_submit"
// No addEventListener in Tag B
Remediation
- Open the rule finding and note which DOM events are registered across multiple tags.
- For each duplicated event type, designate one tag as the listener. Its sole job is to detect the event and push to the dataLayer.
- Replace that tag's handler with a single
addEventListenercall that firesdataLayer.push({ event: 'your_event_name' }). - Remove the
addEventListenercalls from all other tags that duplicated the listener. - Convert those tags to fire on a GTM Custom Event trigger matching the
eventvalue pushed in step 3. - Test in Preview mode that the event fires exactly once per user interaction and that all dependent tags receive it.
Metadata
- Severity
- medium
- Tier
- optimization
- Category
- quality
- Layer
- container
- Version
- 1
- Effort
2hr(about 2 hours)- Reversibility
- non-destructive
See also
- Quality rules — other rules in this category
- Health score — how this rule contributes to the score
- Rules catalog — browse all rules
Examples
curl https://developer.tagmanifest.com/v1/evaluate-rule \
-H "Authorization: Bearer $TM_API_KEY" \
-H "Content-Type: application/json" \
-d '{"rule_id":"duplicate-event-listeners","url":"https://example.com"}'
User: Run duplicate-event-listeners on https://example.com
# Tool call
{
"tool": "evaluate_rule",
"arguments": {
"rule_id": "duplicate-event-listeners",
"url": "https://example.com"
}
}