{"id":250,"date":"2025-06-17T12:54:30","date_gmt":"2025-06-17T18:54:30","guid":{"rendered":"https:\/\/blog.alejandrobecerril.com\/?p=250"},"modified":"2026-03-02T18:57:46","modified_gmt":"2026-03-03T00:57:46","slug":"create-helper-functions-in-google-tag-manager","status":"publish","type":"post","link":"https:\/\/blog.alejandrobecerril.com\/es\/create-helper-functions-in-google-tag-manager\/","title":{"rendered":"Create Helper Functions in Google Tag Manager"},"content":{"rendered":"<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"385\" src=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-1024x385.png\" alt=\"\" class=\"wp-image-255\" srcset=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-1024x385.png 1024w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-300x113.png 300w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-768x289.png 768w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-18x7.png 18w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708.png 1290w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>As a frequent user of <strong>Google Tag Manager (GTM)<\/strong>, I often find myself reusing the same blocks of code across multiple tags and variables, especially when working with browser cookies, string manipulation, or custom logic. Repeating code not only clutters your workspace but also increases the chances of inconsistency and errors.<\/p>\n\n\n\n<p>To solve this, I use a <strong>modular approach<\/strong> by creating <em>helper functions<\/em> through <strong>Custom JavaScript variables<\/strong>. These helper functions can then be reused across your GTM implementation with minimal overhead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation Steps<\/h2>\n\n\n\n<ul>\n<li>Go to <strong>Variables<\/strong> in your GTM workspace.<\/li>\n\n\n\n<li>Create a new <strong>User-Defined Variable<\/strong> &gt; Type: <strong>Custom JavaScript<\/strong>.<\/li>\n\n\n\n<li>Paste your helper function inside, wrapping it as shown below.<\/li>\n\n\n\n<li>In your tags or other variables, reference the helper function using <code>{{variableName}}<\/code>.<\/li>\n\n\n\n<li>Assign it to a local variable before using it.<\/li>\n\n\n\n<li>Execute it, passing the parameters defined in the function.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1<\/strong>: setCookie<\/h3>\n\n\n\n<p>This function sets a cookie with a name, value, and expiration date (in days). Follow the implementation steps defined above and use the next code for your setCookie variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function(){\n  var setCookie = function(cname, cvalue, exdays) {\n      var d = new Date();\n      d.setTime(d.getTime() + (exdays*24*60*60*1000));\n      var expires = \"expires=\"+ d.toUTCString();\n      document.cookie = cname + \"=\" + cvalue + \";\" + expires + \";path=\/\";\n  }\n  return setCookie;\n}<\/code><\/pre>\n\n\n\n<p>Your custom JavaScript variable should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"595\" src=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1024x595.png\" alt=\"\" class=\"wp-image-251\" srcset=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1024x595.png 1024w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-300x174.png 300w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-768x446.png 768w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-18x10.png 18w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image.png 1190w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example2: <\/strong>getCookie<\/h3>\n\n\n\n<p>This function retrieves the value of a cookie by name. Follow the implementation steps defined above and use the next code for your getCookie variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function(){\n  var getCookie = function(cname) {\n      var name = cname + \"=\";\n      var decodedCookie = decodeURIComponent(document.cookie);\n      var ca = decodedCookie.split(';');\n      for(var i = 0; i &lt;ca.length; i++) {\n          var c = ca&#91;i];\n          while (c.charAt(0) == ' ') {\n              c = c.substring(1);\n          }\n          if (c.indexOf(name) == 0) {\n              return c.substring(name.length, c.length);\n          }\n      }\n      return \"\";\n  }\n  return getCookie;\n}<\/code><\/pre>\n\n\n\n<p>Your custom JavaScript variable should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"535\" src=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1-1024x535.png\" alt=\"\" class=\"wp-image-252\" srcset=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1-1024x535.png 1024w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1-300x157.png 300w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1-768x401.png 768w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1-18x9.png 18w, https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/image-1.png 1135w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use These Functions<\/h2>\n\n\n\n<p>In any tag or variable with custom JavaScript code, reference the function like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var setCookie = {{setCookie}};\nsetCookie(\"userType\", \"premium\", 30);<\/code><\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var getCookie = {{getCookie}};\nvar userType = getCookie(\"userType\");<\/code><\/pre>\n\n\n\n<p><strong>Best practice:<\/strong> Save the returned function in a local variable before calling it. This ensures the function is evaluated only once per execution context and avoids repeatedly invoking the GTM variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of GTM helper functions<\/h2>\n\n\n\n<ul>\n<li><strong>Modularity:<\/strong> Define once, use everywhere.<\/li>\n\n\n\n<li><strong>Performance:<\/strong> Saves compute cycles by avoiding repeated parsing and execution.<\/li>\n\n\n\n<li><strong>Maintainability:<\/strong> Centralizes logic, making updates faster and safer.<\/li>\n\n\n\n<li><strong>Readability:<\/strong> Keeps your tag code clean and focused.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>By using this pattern, you gain more control over your GTM container\u2019s architecture and performance. This method is particularly useful in complex implementations where modularity and efficiency are key. <\/p>","protected":false},"excerpt":{"rendered":"<p>As a frequent user of Google Tag Manager (GTM), I often find myself reusing the same blocks of code across multiple tags and variables, especially when working with browser cookies, string manipulation, or custom logic. Repeating code not only clutters your workspace but also increases the chances of inconsistency and errors. To solve this, I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.alejandrobecerril.com\/es\/create-helper-functions-in-google-tag-manager\/\" class=\"more-link\">Continuar leyendo<span class=\"screen-reader-text\"> &#8220;Create Helper Functions in Google Tag Manager&#8221;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35],"tags":[11,19],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create Helper Functions in Google Tag Manager - Alejandro Becerril<\/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:\/\/blog.alejandrobecerril.com\/es\/create-helper-functions-in-google-tag-manager\/\" \/>\n<meta property=\"og:locale\" content=\"es_MX\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create Helper Functions in Google Tag Manager - Alejandro Becerril\" \/>\n<meta property=\"og:description\" content=\"As a frequent user of Google Tag Manager (GTM), I often find myself reusing the same blocks of code across multiple tags and variables, especially when working with browser cookies, string manipulation, or custom logic. Repeating code not only clutters your workspace but also increases the chances of inconsistency and errors. To solve this, I &hellip; Continuar leyendo &quot;Create Helper Functions in Google Tag Manager&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alejandrobecerril.com\/es\/create-helper-functions-in-google-tag-manager\/\" \/>\n<meta property=\"og:site_name\" content=\"Alejandro Becerril\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-17T18:54:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-03T00:57:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-1024x385.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#website\",\"url\":\"https:\/\/blog.alejandrobecerril.com\/\",\"name\":\"Alejandro becerril - Marketing, C\\u00f3digo y Negocios\",\"description\":\"Data Solutions | Marketing Intelligence | Technology\",\"publisher\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#\/schema\/person\/3967f8b514fab90a660d896cbf407486\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/blog.alejandrobecerril.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"es-MX\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#primaryimage\",\"inLanguage\":\"es-MX\",\"url\":\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708.png\",\"contentUrl\":\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708.png\",\"width\":1290,\"height\":485},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#webpage\",\"url\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/\",\"name\":\"Create Helper Functions in Google Tag Manager - Alejandro Becerril\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#primaryimage\"},\"datePublished\":\"2025-06-17T18:54:30+00:00\",\"dateModified\":\"2026-03-03T00:57:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#breadcrumb\"},\"inLanguage\":\"es-MX\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Inicio\",\"item\":\"https:\/\/blog.alejandrobecerril.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create Helper Functions in Google Tag Manager\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#webpage\"},\"author\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#\/schema\/person\/3967f8b514fab90a660d896cbf407486\"},\"headline\":\"Create Helper Functions in Google Tag Manager\",\"datePublished\":\"2025-06-17T18:54:30+00:00\",\"dateModified\":\"2026-03-03T00:57:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#webpage\"},\"wordCount\":338,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#\/schema\/person\/3967f8b514fab90a660d896cbf407486\"},\"image\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blog.alejandrobecerril.com\/wp-content\/uploads\/2025\/06\/Screenshot-2025-06-17-125708-1024x385.png\",\"keywords\":[\"google tag manager\",\"javascript\"],\"articleSection\":[\"Google Tag Manager\"],\"inLanguage\":\"es-MX\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.alejandrobecerril.com\/create-helper-functions-in-google-tag-manager\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#\/schema\/person\/3967f8b514fab90a660d896cbf407486\",\"name\":\"Alejandro Becerril\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#personlogo\",\"inLanguage\":\"es-MX\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15ec3fc837a9f7ce7e24092347a222f4?s=96&d=retro&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15ec3fc837a9f7ce7e24092347a222f4?s=96&d=retro&r=g\",\"caption\":\"Alejandro Becerril\"},\"logo\":{\"@id\":\"https:\/\/blog.alejandrobecerril.com\/#personlogo\"},\"sameAs\":[\"https:\/\/blog.alejandrobecerril.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/posts\/250"}],"collection":[{"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/comments?post=250"}],"version-history":[{"count":3,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/posts\/250\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/media?parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/categories?post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alejandrobecerril.com\/es\/wp-json\/wp\/v2\/tags?post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}