{"id":143,"date":"2016-05-24T13:56:25","date_gmt":"2016-05-24T13:56:25","guid":{"rendered":"http:\/\/www.digitaldatatactics.com\/?p=143"},"modified":"2017-03-27T18:05:05","modified_gmt":"2017-03-27T18:05:05","slug":"an-alternative-to-using-css-for-event-based-rules","status":"publish","type":"post","link":"https:\/\/www.digitaldatatactics.com\/index.php\/2016\/05\/24\/an-alternative-to-using-css-for-event-based-rules\/","title":{"rendered":"An alternative to using CSS for Event-Based Rules"},"content":{"rendered":"<p>A lot of the magic that happens in DTM is based on CSS (cascading style sheets) and CSS Selectors (using the styles applied to HTML elements as a map to your site). Even if you have a data layer to avoid relying on CSS for your data elements, Event-based Rules still rely on CSS Selectors to figure out which elements to listen to. For instance, if I want DTM to listen for clicks on my blog menu, which looks something like this:<\/p>\n<pre><span class=\"html-tag\">&lt;li <span class=\"html-attribute-name\">id<\/span>=\"<span class=\"html-attribute-value\">menu-item-82<\/span>\" <span class=\"html-attribute-name\">class<\/span>=\"<span class=\"html-attribute-value\">menu-item menu-item-type-custom menu-item-object-custom menu-item-82<\/span>\"&gt;\r\n<\/span><span class=\"html-tag\">     &lt;a <span class=\"html-attribute-name\">href<\/span>=\"http:\/\/digitaldatatactics.com\/beaconParser\/\"&gt;<\/span>Beacon Parser Tool<span class=\"html-tag\">&lt;\/a&gt;\r\n<\/span><span class=\"html-tag\">&lt;\/li&gt;<\/span>\u00a0\r\n<\/pre>\n<p>I might tell it to listen to link\/anchor tags (&#8220;a&#8221; tags) that are within list items (&#8220;li&#8221; tags) that have a class of &#8220;menu-item&#8221;. \u00a0The CSS Selector for this would be &#8220;li.menu-item a&#8221;, so I might create this rule in DTM:<\/p>\n<p><a href=\"http:\/\/digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrCSS.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-157\" src=\"\/\/digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrCSS.jpg\" alt=\"ebrCSS\" width=\"383\" height=\"246\" srcset=\"https:\/\/www.digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrCSS.jpg 383w, https:\/\/www.digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrCSS-300x193.jpg 300w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/a><\/p>\n<p>This should work, BUT there are a few potential problems:<\/p>\n<ul>\n<li>it relies on the CSS of my site staying the same, and on my ability to interpret CSS selectors to get one with the right level of granularity.<\/li>\n<li>Developers wouldn&#8217;t know that Analytics is relying on that &#8220;menu-item&#8221; class, and they might change it without notice.<\/li>\n<li>An implementation may have dozens of these Event-based rules, which could require a lot of time to set up and test.<\/li>\n<li>Lastly, if I want to pass a value like &#8220;nav click:beacon parser&#8221; I have to figure out a way to dynamically grab the value &#8220;beacon parser&#8221; from the surrounding HTML.<\/li>\n<\/ul>\n<p>There is an alternative: instead of relying on classes, we could work with developers to add another attribute to the elements we want to listen to. This attribute would be completely arbitrary- I&#8217;m going to use &#8220;data-track&#8221; for this post. So I might ask developers to add &#8220;data-track=&#8217;nav click'&#8221; to that element:<\/p>\n<pre><span class=\"html-tag\">&lt;li <span class=\"html-attribute-name\">id<\/span>=\"<span class=\"html-attribute-value\">menu-item-82<\/span>\" <span class=\"html-attribute-name\">class<\/span>=\"<span class=\"html-attribute-value\">menu-item menu-item-type-custom menu-item-object-custom menu-item-82<\/span>\" <strong>data-track=\"nav click:beacon parser\"<\/strong>&gt;\r\n<\/span><span class=\"html-tag\">     &lt;a <span class=\"html-attribute-name\">href<\/span>=\"http:\/\/digitaldatatactics.com\/beaconParser\/\"&gt;<\/span>Beacon Parser Tool<span class=\"html-tag\">&lt;\/a&gt;\r\n<\/span><span class=\"html-tag\">&lt;\/li&gt;<\/span><\/pre>\n<p>Then in DTM, I could set up a rule that listens not to CSS, but for clicks on any item that has &#8220;data-track&#8221; on it:<br \/>\n<a href=\"http:\/\/digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrDatatrack.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-158\" src=\"\/\/digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrDatatrack.jpg\" alt=\"ebrDatatrack\" width=\"367\" height=\"274\" srcset=\"https:\/\/www.digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrDatatrack.jpg 367w, https:\/\/www.digitaldatatactics.com\/wp\/wp-content\/uploads\/2016\/05\/ebrDatatrack-300x224.jpg 300w\" sizes=\"(max-width: 367px) 100vw, 367px\" \/><\/a><\/p>\n<p>Or if I wanted, I could have this particular rule only fire on clicks where &#8220;data-track&#8221; started with &#8220;nav click&#8221;: &#8220;[data-track^=&#8217;nav click&#8217;]&#8221;.<\/p>\n<p>This approach has some benefits:<\/p>\n<ul>\n<li>Developers aren&#8217;t likely to accidentally change the &#8216;data-track&#8217; attribute<\/li>\n<li>Setting up rules in DTM doesn&#8217;t require deep knowledge of CSS<\/li>\n<li>You don&#8217;t have to rely on the HTML around the element to provide dynamic values (like &#8220;beacon parser&#8221; in my example above.) Whatever values I want in my reporting, I can have put in friendly wording into my data-track attribute.<\/li>\n<\/ul>\n<p>As well as a few potential downsides:<\/p>\n<ul>\n<li>I&#8217;d need to get my developers to add this attribute to all elements I want to track.<\/li>\n<li>I&#8217;d need some sort of management on the values of those attributes (I may want to document that we&#8217;re using &#8220;nav click&#8221; and keep some standards and consistency across how data-track is used across the site.)<\/li>\n<\/ul>\n<p>Ultimately, for many clients, the benefits of having control and flexibility far outweigh the upfront work of adding those tags.<\/p>\n<p>Hopefully this approach can provide a solution to some folks out there, or at least can serve as a starting point for discussing the different options for setting event-based rules. I&#8217;d love to hear about folks who have used this solution or other solutions to keep CSS selector dependency to a minimum!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of the magic that happens in DTM is based on CSS (cascading style sheets) and CSS Selectors (using the styles applied to HTML elements as a map to your site). Even if you have a data layer to avoid relying on CSS for your data elements, Event-based Rules still rely on CSS Selectors &#8230; <a title=\"An alternative to using CSS for Event-Based Rules\" class=\"read-more\" href=\"https:\/\/www.digitaldatatactics.com\/index.php\/2016\/05\/24\/an-alternative-to-using-css-for-event-based-rules\/\" aria-label=\"Read more about An alternative to using CSS for Event-Based Rules\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":5,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digitaldatatactics.com\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}