{"id":123242,"date":"2025-03-10T01:31:32","date_gmt":"2025-03-10T01:31:32","guid":{"rendered":"https:\/\/peraltafinancing.com\/analytics\/capturing-the-correct-element-in-google-tag-manager\/"},"modified":"2025-03-10T01:31:32","modified_gmt":"2025-03-10T01:31:32","slug":"capturing-the-correct-element-in-google-tag-manager","status":"publish","type":"post","link":"https:\/\/fivemor.com\/?p=123242","title":{"rendered":"Capturing The Correct Element In Google Tag Manager"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<blockquote>\n<p><strong>Last updated 24 August 2022<\/strong>: The instructions in this article are no longer that useful, as you can simply use the native JavaScript <code>Element.closest(selector)<\/code> method instead of the {{Find Closest}} trick described in this article. Browser support for <code>closest<\/code> has thankfully improved a great deal since this article was originally written.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/tagmanager.google.com\/\">Google Tag Manager<\/a> provides us with a bunch of handy triggers, designed to make capturing user interactions on the website much easier. These triggers are part of a paradigm called <a href=\"https:\/\/www.simoahava.com\/analytics\/auto-event-tracking-gtm-2-0\/\">auto-event tracking<\/a>, which comprises the <strong>Click<\/strong>, <strong>Form<\/strong>, <strong>History<\/strong>, and <strong>JavaScript Error<\/strong> trigger types.<\/p>\n<div style=\"aspect-ratio: 2832 \/ 972;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2017\/02\/auto-event-triggers.jpg\" title=\"Auto-event triggers\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"972\" width=\"2832\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2017\/02\/auto-event-triggers.jpg#ZgotmplZ\" alt=\"Auto-event triggers\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>Now, I\u2019ve covered GTM\u2019s triggers many, many times before. If you need a refresher, take a look at the following articles:<\/p>\n<p>In this article, I want to tackle a specific aspect of <strong>Click triggers<\/strong>. If you\u2019ve used them before, you\u2019ll know (at least) two things:<\/p>\n<ol>\n<li>\n<p>The <strong>Just Links<\/strong> trigger captures the link element (<code><a\/><\/code>) that is the nearest wrapping link of the clicked element.<\/p>\n<\/li>\n<li>\n<p>The <strong>All Elements<\/strong> trigger captures the clicked element itself.<\/p>\n<\/li>\n<\/ol>\n<p>In other words, the Just Links trigger actually climbs up the document until it finds a link element, and returns that for GTM to process. The All Elements acts more like a regular handler: it simply returns the element that was clicked.<\/p>\n<p>In this article, I want to show you how to leverage especially the All Elements trigger more effectively. Basically, you might want to use the All Elements trigger, but then mimic the Just Links trigger to find some nearest wrapping element, and return that for GTM to use.<\/p>\n<p>                <span class=\"simmer\"><br \/>\n  <span class=\"close\">X<\/span><\/p>\n<p>\n    <span class=\"fa fa-md fa-bell\"\/><br \/>\n    <strong>The Simmer Newsletter<\/strong>\n  <\/p>\n<p>\n    Subscribe to the <a href=\"https:\/\/www.simoahava.com\/newsletter\/\">Simmer newsletter<\/a> to get the latest news and content from Simo Ahava into your email inbox!\n  <\/p>\n<p>  <\/span><\/p>\n<h2 id=\"why-use-all-elements\">Why use All Elements?<\/h2>\n<p>Well, there\u2019s one obvious reason: <strong>There are all sorts of elements on the page you might want to track<\/strong>. Even though links are a very popular tracking item, there are many elements that have nothing to do with links.<\/p>\n<div style=\"aspect-ratio: 1776 \/ 592;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2017\/02\/all-elements.jpg\" title=\"All Elements\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"592\" width=\"1776\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2017\/02\/all-elements.jpg#ZgotmplZ\" alt=\"All Elements\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>Perhaps you want to track a button, or a form field, or an image. In these cases, you will need the All Elements trigger.<\/p>\n<p>Another reason you might not be aware of is very significant: <strong>The All Elements trigger uses the <em>capture<\/em> phase of the event path<\/strong>. The Just Links trigger, as well as pretty much all other GTM triggers, utilize the <em>bubble<\/em> phase.<\/p>\n<p>And why is this significant? Because event propagation is <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/fix-problems-with-gtm-listeners\/\">most often cancelled<\/a> in the <em>bubble<\/em> phase! Many people have come across forms and links that simply refuse to fire their respective triggers in Google Tag Manager. The reason is that other JavaScript on the site can prevent the event from climbing up the document to where Google Tag Manager\u2019s listeners are waiting.<\/p>\n<p>But when you use the All Elements trigger, you are using the <em>capture<\/em> phase, which is cancelled far less often.<\/p>\n<p>In other words, you can actually use the All Elements trigger to apply link tracking for links that do not create a GTM event when using the Just Links trigger!<\/p>\n<p>The only problem with this approach is that the All Elements trigger returns the actual element that was clicked. For example, let\u2019s say you have HTML like this on the site:<\/p>\n<p>If you used a <strong>Just Links<\/strong> trigger, and event propagation wasn\u2019t stopped, clicking the link will pass the <code><a href=\"&quot;mailto:&lt;a\" class=\"__cf_email__\" data-cfemail=\"b2c1dbdfddf2d7cad3dfc2ded79cd1dddf\">[email\u00a0protected]<\/a>\"\/&gt;<\/code> to Google Tag Manager as the target element of the event. But if you\u2019re using the <strong>All Elements<\/strong> trigger, GTM captures the <code><span\/><\/code>, because it\u2019s what the click actually landed on!<\/p>\n<p>So how do we get the benefit of All Elements with its <em>capture<\/em> phase handler, but still be able to capture any element up the DOM tree?<\/p>\n<p>With some Custom HTML Tag and Custom JavaScript variable magic, of course!<\/p>\n<h2 id=\"the-solution\">The solution<\/h2>\n<p>The solution comes in two parts. First, we need to create a Custom HTML Tag that introduces a <strong>polyfill<\/strong> (read: workaround) for older browsers which might not support the method we\u2019re going to use.<\/p>\n<p>Next, we\u2019ll use a Custom JavaScript variable to create a generic function to which we can pass a <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/10-useful-css-selectors\/\">CSS selector<\/a>. This function, in turn, climbs up the document tree and returns the element that matches the selector.<\/p>\n<p>Note that you can achieve most of this easily with <a href=\"https:\/\/jquery.com\">jQuery<\/a> or something similar. However, I want to show the native JavaScript way of doing it, since it\u2019s not actually that complex.<\/p>\n<h3 id=\"the-custom-html-tag\">The Custom HTML Tag<\/h3>\n<p>The Custom HTML Tag should fire as early as possible on the page, so you can use the All Pages trigger, if you wish. This is what it holds:<\/p>\n<div class=\"highlight\">\n<pre style=\"background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-javascript\" data-lang=\"javascript\"><script><![CDATA[\n  <span style=\"color:#00a\">if (!Element.prototype.matches) {\n    Element.prototype.matches = \n        Element.prototype.matchesSelector || \n        Element.prototype.mozMatchesSelector ||\n        Element.prototype.msMatchesSelector || \n        Element.prototype.oMatchesSelector || \n        Element.prototype.webkitMatchesSelector ||\n        <span style=\"color:#00a\">function(s) {\n            <span style=\"color:#00a\">var matches = (<span style=\"color:#00a\">this.<span style=\"color:#0aa\">document || <span style=\"color:#00a\">this.ownerDocument).querySelectorAll(s),\n                i = matches.length;\n            <span style=\"color:#00a\">while (--i >= <span style=\"color:#099\">0 && matches.item(i) !== <span style=\"color:#00a\">this) {}\n            <span style=\"color:#00a\">return i > -<span style=\"color:#099\">1;            \n        };\n  }\n<<span style=\"color:#f00;background-color:#faa\">\/script>\n]]><\/script><\/code><\/pre>\n<\/div>\n<p>This is a good addition to the site code in general, so if possible to add it to the regular JavaScript of the site, I recommend you do!<\/p>\n<p>The polyfill is gratefully copied from the <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/Element\/matches\">Mozilla Developer Network pages<\/a>.<\/p>\n<p>The script checks if the browser supports the method <code>Element.matches()<\/code> or any of its alternatives. If no match is found, then it\u2019s fixed with a custom method that scrolls through the document elements and checks if any of them matches the given CSS selector.<\/p>\n<p>Once this is running, you can start using it!<\/p>\n<h3 id=\"the-custom-javascript-variable\">The Custom JavaScript variable<\/h3>\n<p>To make things work, we need a little utility Custom JavaScript variable. So create a new Custom JavaScript Variable, and name it <strong>{{Find closest}}<\/strong>. This is the code you need to put within:<\/p>\n<div class=\"highlight\">\n<pre style=\"background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-javascript\" data-lang=\"javascript\"><span style=\"color:#00a\">function<\/span>() {\n  <span style=\"color:#00a\">return<\/span> <span style=\"color:#00a\">function<\/span>(target, selector) {\n    <span style=\"color:#00a\">while<\/span> (!target.matches(selector) &amp;&amp; !target.matches(<span style=\"color:#a50\">'body'<\/span>)) {\n      target = target.parentElement;\n    }\n    <span style=\"color:#00a\">return<\/span> target.matches(selector) ? target : <span style=\"color:#00a\">undefined<\/span>;\n  }\n}\n<\/code><\/pre>\n<\/div>\n<p>This function takes two parameters: <code>target<\/code> and <code>selector<\/code>. The first is an HTML element such as the <strong>{{Click Element}}<\/strong> <a href=\"https:\/\/www.simoahava.com\/analytics\/variable-guide-google-tag-manager\/\">built-in variable<\/a>. The latter is a <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/10-useful-css-selectors\/\">CSS selector<\/a> string. The CSS selector is what you use to tell GTM the following:<\/p>\n<blockquote>\n<p>Starting from <code>target<\/code>, start climbing up the wrapping DOM structure until you find an element that matches <code>selector<\/code>. If such an element is found, return it. If such an element is NOT found, return the original event target.<\/p>\n<\/blockquote>\n<p>The idea here is that even though the click landed on the element you wanted to target, <strong>you might actually want to access some other element relative to the original event target<\/strong>. For this, the <code>matches()<\/code> workaround is invaluable, as it lets you traverse element-relative paths.<\/p>\n<h2 id=\"how-to-use-it\">How to use it<\/h2>\n<p>Once you\u2019ve set up the polyfill and the Custom JavaScript Variable, you can leverage the variable in all your Google Tag Manager JavaScript with the following syntax:<\/p>\n<p><code>var elementFound = {{Find closest}}(someHTMLElement, 'someCSSselector');<\/code><\/p>\n<p>For example, let\u2019s say you have an HTML structure that looks like this:<\/p>\n<div class=\"highlight\">\n<pre style=\"background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-html\" data-lang=\"html\">div <span style=\"color:#1e90ff\">id<\/span>=<span style=\"color:#a50\">\"product_12345\"<\/span>&gt;\n  div <span style=\"color:#1e90ff\">class<\/span>=<span style=\"color:#a50\">\"details\"<\/span>&gt;\n    h2&gt;Product 1<span style=\"color:#1e90ff;font-weight:bold\">h2<\/span>&gt;\n  <span style=\"color:#1e90ff;font-weight:bold\">div<\/span>&gt;\n  div <span style=\"color:#1e90ff\">class<\/span>=<span style=\"color:#a50\">\"link\"<\/span>&gt;\n    a <span style=\"color:#1e90ff\">href<\/span>=<span style=\"color:#a50\">\"product1.html\"<\/span>&gt;\n      Product 1\n    <span style=\"color:#1e90ff;font-weight:bold\">a<\/span>&gt;\n  <span style=\"color:#1e90ff;font-weight:bold\">div<\/span>&gt;\n<span style=\"color:#1e90ff;font-weight:bold\">div<\/span>&gt;<\/code><\/pre>\n<\/div>\n<p>Now, you\u2019ve set up a <strong>Click &#8211; Just Links<\/strong> or <strong>Click &#8211; All Elements<\/strong> trigger to capture clicks on the <code><a\/><\/code> element. Now, however, you also want to grab the value of the <code>id<\/code> attribute from the very top-most <code>div<\/code> (\u201cproduct_12345\u201d). To get that, you could use a Custom JavaScript Variable like this:<\/p>\n<div class=\"highlight\">\n<pre style=\"background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-javascript\" data-lang=\"javascript\"><span style=\"color:#00a\">function<\/span>() {\n  <span style=\"color:#00a\">var<\/span> el = {{Find closest}}({{Click Element}}, <span style=\"color:#a50\">'div[id^=\"product\"]'<\/span>);\n  <span style=\"color:#00a\">return<\/span> <span style=\"color:#00a\">typeof<\/span> el !== <span style=\"color:#a50\">'undefined'<\/span> ? el.id : <span style=\"color:#00a\">undefined<\/span>;\n}\n<\/code><\/pre>\n<\/div>\n<p>This Custom JavaScript Variable takes {{Click Element}} (the element that was originally clicked), and then starts walking up the DOM until it finds a <code>div<\/code> whose <code>id<\/code> attribute begins with the string \u201cproduct\u201d. If such an element is found, it returns the value of the <code>id<\/code> attribute in question. If the element isn\u2019t found, it simply returns <code>undefined<\/code>.<\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>Traversing the Document Object Model with CSS selectors can be a very good friend to you, indeed. Click handlers are usually <em>too<\/em> accurate, as they return elements we didn\u2019t expect them to return, or they return elements too deep in the DOM to matter.<\/p>\n<p>That\u2019s why it\u2019s good to have a tool that lets you access the DOM structure more deliberately. Traversing the DOM with the {{Find closest}} workaround is a nice way to achieve the type of freedom using native JavaScript that is typically found only within frameworks like jQuery.<\/p>\n<\/p><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Last updated 24 August 2022: The instructions in this article are no longer that useful, as you can simply use the native JavaScript Element.closest(selector) method instead of the {{Find Closest}} trick described in this article. Browser support for closest has thankfully improved a great deal since this article was originally written. Google Tag Manager provides [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":123243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12033],"tags":[10893,23838,27300,1531,14495,1791],"dealstore":[],"offerexpiration":[],"class_list":["post-123242","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","tag-capturing","tag-correct","tag-element","tag-google","tag-manager","tag-tag"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Capturing The Correct Element In Google Tag Manager - Som2ny Network<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fivemor.com\/?p=123242\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Capturing The Correct Element In Google Tag Manager - Som2ny Network\" \/>\n<meta property=\"og:description\" content=\"Last updated 24 August 2022: The instructions in this article are no longer that useful, as you can simply use the native JavaScript Element.closest(selector) method instead of the {{Find Closest}} trick described in this article. Browser support for closest has thankfully improved a great deal since this article was originally written. Google Tag Manager provides [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fivemor.com\/?p=123242\" \/>\n<meta property=\"og:site_name\" content=\"Som2ny Network\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-10T01:31:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"879\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fivemor.com\/?p=123242#article\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371\"},\"headline\":\"Capturing The Correct Element In Google Tag Manager\",\"datePublished\":\"2025-03-10T01:31:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242\"},\"wordCount\":1268,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fivemor.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg\",\"keywords\":[\"Capturing\",\"Correct\",\"Element\",\"Google\",\"Manager\",\"TAG\"],\"articleSection\":[\"Analytics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fivemor.com\/?p=123242#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fivemor.com\/?p=123242\",\"url\":\"https:\/\/fivemor.com\/?p=123242\",\"name\":\"Capturing The Correct Element In Google Tag Manager - Som2ny Network\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg\",\"datePublished\":\"2025-03-10T01:31:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fivemor.com\/?p=123242#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fivemor.com\/?p=123242\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/?p=123242#primaryimage\",\"url\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg\",\"contentUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg\",\"width\":2560,\"height\":879},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fivemor.com\/?p=123242#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fivemor.com\/?bp_activities=1\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Capturing The Correct Element In Google Tag Manager\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fivemor.com\/#website\",\"url\":\"https:\/\/fivemor.com\/\",\"name\":\"Som2ny Network\",\"description\":\"Daily Deals\",\"publisher\":{\"@id\":\"https:\/\/fivemor.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fivemor.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/fivemor.com\/#organization\",\"name\":\"Som2ny Network\",\"url\":\"https:\/\/fivemor.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png\",\"contentUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png\",\"width\":300,\"height\":86,\"caption\":\"Som2ny Network\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/fivemor.com\"],\"url\":\"https:\/\/fivemor.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Capturing The Correct Element In Google Tag Manager - Som2ny Network","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fivemor.com\/?p=123242","og_locale":"en_US","og_type":"article","og_title":"Capturing The Correct Element In Google Tag Manager - Som2ny Network","og_description":"Last updated 24 August 2022: The instructions in this article are no longer that useful, as you can simply use the native JavaScript Element.closest(selector) method instead of the {{Find Closest}} trick described in this article. Browser support for closest has thankfully improved a great deal since this article was originally written. Google Tag Manager provides [&hellip;]","og_url":"https:\/\/fivemor.com\/?p=123242","og_site_name":"Som2ny Network","article_published_time":"2025-03-10T01:31:32+00:00","og_image":[{"width":2560,"height":879,"url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fivemor.com\/?p=123242#article","isPartOf":{"@id":"https:\/\/fivemor.com\/?p=123242"},"author":{"name":"admin","@id":"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371"},"headline":"Capturing The Correct Element In Google Tag Manager","datePublished":"2025-03-10T01:31:32+00:00","mainEntityOfPage":{"@id":"https:\/\/fivemor.com\/?p=123242"},"wordCount":1268,"commentCount":0,"publisher":{"@id":"https:\/\/fivemor.com\/#organization"},"image":{"@id":"https:\/\/fivemor.com\/?p=123242#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg","keywords":["Capturing","Correct","Element","Google","Manager","TAG"],"articleSection":["Analytics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fivemor.com\/?p=123242#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fivemor.com\/?p=123242","url":"https:\/\/fivemor.com\/?p=123242","name":"Capturing The Correct Element In Google Tag Manager - Som2ny Network","isPartOf":{"@id":"https:\/\/fivemor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fivemor.com\/?p=123242#primaryimage"},"image":{"@id":"https:\/\/fivemor.com\/?p=123242#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg","datePublished":"2025-03-10T01:31:32+00:00","breadcrumb":{"@id":"https:\/\/fivemor.com\/?p=123242#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fivemor.com\/?p=123242"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/?p=123242#primaryimage","url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg","contentUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/03\/auto-event-triggers-scaled.jpg","width":2560,"height":879},{"@type":"BreadcrumbList","@id":"https:\/\/fivemor.com\/?p=123242#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fivemor.com\/?bp_activities=1"},{"@type":"ListItem","position":2,"name":"Capturing The Correct Element In Google Tag Manager"}]},{"@type":"WebSite","@id":"https:\/\/fivemor.com\/#website","url":"https:\/\/fivemor.com\/","name":"Som2ny Network","description":"Daily Deals","publisher":{"@id":"https:\/\/fivemor.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fivemor.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fivemor.com\/#organization","name":"Som2ny Network","url":"https:\/\/fivemor.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/#\/schema\/logo\/image\/","url":"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png","contentUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png","width":300,"height":86,"caption":"Som2ny Network"},"image":{"@id":"https:\/\/fivemor.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png","caption":"admin"},"sameAs":["https:\/\/fivemor.com"],"url":"https:\/\/fivemor.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts\/123242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123242"}],"version-history":[{"count":0,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts\/123242\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/media\/123243"}],"wp:attachment":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123242"},{"taxonomy":"dealstore","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fdealstore&post=123242"},{"taxonomy":"offerexpiration","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fofferexpiration&post=123242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}