{"id":16268,"date":"2020-06-17T15:23:54","date_gmt":"2020-06-17T13:23:54","guid":{"rendered":"https:\/\/www.dase-analytics.com\/blog\/?p=16268\/"},"modified":"2020-06-17T15:23:54","modified_gmt":"2020-06-17T13:23:54","slug":"gtm-tip-02-format-value","status":"publish","type":"post","link":"https:\/\/www.dase-analytics.com\/blog\/en\/gtm-tip-02-format-value\/","title":{"rendered":"GTM Tip #02: Format Value"},"content":{"rendered":"<p><span style=\"font-weight: 400\">Sending all values to Google Analytics in a consistent format is very important. That\u2019s why the option to format the variable value is very valuable and safes a lot of time.<\/span><\/p>\n<p><span style=\"font-weight: 400\">If you wanted to change the format of a variable before, you were forced to define the formatting logic directly in the JavaScript variable. For example, this JS variable changes the value of the dataLayer variable <\/span><b>DL &#8211; eventAction<\/b><span style=\"font-weight: 400\"> to lowercase.<\/span><\/p>\n<p><code>function(){<br \/>\nreturn {{DL - eventAction}}.toLowerCase();<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Another example is this JS variable, which returns the string &#8216;not available&#8217; if the dataLayer variable DL &#8211; eventAction is not defined (the variable is undefined or null). In other cases, it returns the original value in lowercase.<\/p>\n<p><code><span style=\"font-weight: 400\">function<\/span><span style=\"font-weight: 400\">(){<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">if<\/span><span style=\"font-weight: 400\">({{DL - eventAction}} == <\/span><span style=\"font-weight: 400\">undefined<\/span><span style=\"font-weight: 400\"> || {{DL - eventAction}} == <\/span><span style=\"font-weight: 400\">null<\/span><span style=\"font-weight: 400\">){<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">return<\/span> <span style=\"font-weight: 400\">'not available'<\/span><span style=\"font-weight: 400\">;<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">else<\/span><span style=\"font-weight: 400\">{<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">\u00a0 <\/span><span style=\"font-weight: 400\">return<\/span><span style=\"font-weight: 400\"> {{DL - eventAction}}.toLowerCase();<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> }<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">}<\/span><\/code><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400\">The big disadvantage of creating such JavaScript variables is that the entire GTM container will be full of JavaScript variables, which will make the GTM container really difficult to read. Each dataLayer variable (or really variable of any type), you have to &#8220;rewrite&#8221; as a JavaScript variable to only adjust its format to the desired format. Of course, formatting dataLayer variables is best solved directly during the implementation phase (so the values in dataLayer are already consistent), but it certainly doesn&#8217;t hurt to solve it directly in GTM. Formatting variables in GTM will also ensure that all variable values (even those that will be added in the future) will have the format, which will be consistent with previous values.<\/span><\/p>\n<h2><b>What are the options?<\/b><\/h2>\n<p><span style=\"font-weight: 400\">You can find the option to format a variable at the bottom when defining a variable of any type. There are currently 5 different options to choose from:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Change the case to lowercase or uppercase letters.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Change the null value to another value.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Change an undefined value to another value.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Change the value from true to another value.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Change false to another value.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">For the options that change the value of a variable (from null, undefined, true, or false), you can type a static value (text) in the field or refer to another variable to which the value should be changed to.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-14372\" src=\"https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image2-18.png\" alt=\"\" width=\"714\" height=\"690\" srcset=\"https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image2-18.png 714w, https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image2-18-300x290.png 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/p>\n<p><span style=\"font-weight: 400\">So, if you wanted to modify the JavaScript example from the beginning of this article and use formatting directly in the variable definition, it would look like this:<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-14373\" src=\"https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image3-16.png\" alt=\"\" width=\"712\" height=\"883\" srcset=\"https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image3-16.png 712w, https:\/\/www.dase-analytics.com\/blog\/wp-content\/uploads\/image3-16-242x300.png 242w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/p>\n<p><span style=\"font-weight: 400\">Useful, right? No custom code or creation of any additional variables is required.<\/span><\/p>\n<h2><b>Ending<\/b><\/h2>\n<p><span style=\"font-weight: 400\">Formatting variable values is a very effective way to change the format of variables, even without knowledge of JavaScript. However, the options you can currently choose from are still quite limited. I hope that in future, new, interesting ways will be added.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sending all values to Google Analytics in a consistent format is very important. That\u2019s why the option to&#8230;<\/p>\n","protected":false},"author":62,"featured_media":16275,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[770,772],"tags":[],"_links":{"self":[{"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/posts\/16268"}],"collection":[{"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/comments?post=16268"}],"version-history":[{"count":7,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/posts\/16268\/revisions"}],"predecessor-version":[{"id":16276,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/posts\/16268\/revisions\/16276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/media\/16275"}],"wp:attachment":[{"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/media?parent=16268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/categories?post=16268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dase-analytics.com\/blog\/en\/wp-json\/wp\/v2\/tags?post=16268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}