{"id":109625,"date":"2025-02-25T18:38:25","date_gmt":"2025-02-25T18:38:25","guid":{"rendered":"https:\/\/peraltafinancing.com\/analytics\/gtmtips-check-for-undefined-value\/"},"modified":"2025-02-25T18:38:25","modified_gmt":"2025-02-25T18:38:25","slug":"gtmtips-check-for-undefined-value","status":"publish","type":"post","link":"https:\/\/fivemor.com\/?p=109625","title":{"rendered":"#GTMTips: Check For Undefined Value"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p>This is one of those <a href=\"https:\/\/www.simoahava.com\/tags\/gtmtips\/\">#GTMTips<\/a> posts that I was certain I\u2019d already written. So it came as a mild surprise when I realized I\u2019d never tackled this aspect of <a href=\"https:\/\/tagmanager.google.com\/\">Google Tag Manager<\/a>.<\/p>\n<p>It\u2019s a short and sweet tip again. Today we\u2019ll learn <strong>how to check if a variable is undefined using Google Tag Manager<\/strong>.<\/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=\"tip-73-check-for-undefined-variable-values\">Tip 73: Check for undefined variable values<\/h2>\n<div style=\"aspect-ratio: 960 \/ 540;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2018\/02\/check-for-undefined-value.jpg\" title=\"Check for undefined value\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"540\" width=\"960\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2018\/02\/check-for-undefined-value.jpg#ZgotmplZ\" alt=\"Check for undefined value\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>If a variable is <code>undefined<\/code>, it means that a variable with the given name does not currently have any valid value in memory. In JavaScript, the typical check for <code>undefined<\/code> is done 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> checkIfUndefined(variable) {\n  <span style=\"color:#00a\">return<\/span> <span style=\"color:#00a\">typeof<\/span> variable === <span style=\"color:#a50\">'undefined'<\/span>;\n}\n<\/code><\/pre>\n<\/div>\n<p>With Google Tag Manager, you can\u2019t run JavaScript evaluations in triggers. Instead, if you wanted to do the check above, you\u2019d need to create a Custom JavaScript variable that returns <code>true<\/code> if the variable whose status you want to check is, indeed, <code>undefined<\/code>.<\/p>\n<p>However, there\u2019s an easier way. Google Tag Manager does some magic for you and lets you check for <code>undefined<\/code> type using a simple string check. This might sound a bit unorthodox, and it is, but currently it\u2019s the fastest way to check for <code>undefined<\/code> type without having to resort to extra trips through a Custom JavaScript variable.<\/p>\n<p>The method itself is really simple. In the trigger where you want to run the check, simply create a new condition which checks if the given variable <strong>does not equal<\/strong> <code>undefined<\/code>, like so:<\/p>\n<div style=\"aspect-ratio: 929 \/ 419;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2018\/02\/is-variable-undefined.jpg\" title=\"Check if variable is undefined\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"419\" width=\"929\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2018\/02\/is-variable-undefined.jpg#ZgotmplZ\" alt=\"Check if variable is undefined\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>This trigger fires if the value in {{Page Type}} is not <code>undefined<\/code>.<\/p>\n<p>Note that this only checks for <code>undefined<\/code> variables. It doesn\u2019t work for other <em>falsy<\/em> values, such as <code>false<\/code>, <code>null<\/code>, <code>0<\/code>, or <code>NaN<\/code>. For these, you would have to run additional checks, and you can even use a regular expression:<\/p>\n<p><strong>{{Variable}}<\/strong> does not match RegEx (ignore case) <strong>^(undefined|null|0|false|NaN|)$<\/strong><\/p>\n<p>All of these falsy values are resolved to strings in GTM for checking against in triggers. The regular expression above matches all falsy values in JavaScript, and is thus useful as a generic check for potentially invalid values in variables.<\/p>\n<p>Certain problems with GTM\u2019s approach can surface if, for example, you actually have the literal string <code>\"undefined\"<\/code> as the value of a variable. It\u2019s not an impossible scenario to conjure, since \u201cundefined\u201d is a word that has functional depth in the English language. There\u2019s no way for GTM to distinguish between this valid string value and the <code>undefined<\/code> value the variable might have, at least not in triggers. So to make the check as robust and unambiguous as possible, you would have to resort to a Custom JavaScript variable after all.<\/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> variableToValidate = {{Some Variable}};\n  <span style=\"color:#00a\">return<\/span> !!variableToCheckForFalsy;\n}\n<\/code><\/pre>\n<\/div>\n<p>The Custom JavaScript variable above returns <code>true<\/code> if the variable has a valid, non-falsy (i.e. <em>truthy<\/em>) value, and <code>false<\/code> otherwise.<\/p>\n<p>By the way, I did not make up these ridiculous <em>truthy<\/em> and <em>falsy<\/em> terms. They are accepted jargon in programming.<\/p>\n<\/p><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>This is one of those #GTMTips posts that I was certain I\u2019d already written. So it came as a mild surprise when I realized I\u2019d never tackled this aspect of Google Tag Manager. It\u2019s a short and sweet tip again. Today we\u2019ll learn how to check if a variable is undefined using Google Tag Manager. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":109627,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12033],"tags":[855,14492,45445],"dealstore":[],"offerexpiration":[],"class_list":["post-109625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","tag-check","tag-gtmtips","tag-undefined"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>#GTMTips: Check For Undefined Value - 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=109625\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"#GTMTips: Check For Undefined Value - Som2ny Network\" \/>\n<meta property=\"og:description\" content=\"This is one of those #GTMTips posts that I was certain I\u2019d already written. So it came as a mild surprise when I realized I\u2019d never tackled this aspect of Google Tag Manager. It\u2019s a short and sweet tip again. Today we\u2019ll learn how to check if a variable is undefined using Google Tag Manager. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fivemor.com\/?p=109625\" \/>\n<meta property=\"og:site_name\" content=\"Som2ny Network\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-25T18:38:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fivemor.com\/?p=109625#article\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371\"},\"headline\":\"#GTMTips: Check For Undefined Value\",\"datePublished\":\"2025-02-25T18:38:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625\"},\"wordCount\":487,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fivemor.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg\",\"keywords\":[\"Check\",\"GTMTips\",\"Undefined\"],\"articleSection\":[\"Analytics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fivemor.com\/?p=109625#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fivemor.com\/?p=109625\",\"url\":\"https:\/\/fivemor.com\/?p=109625\",\"name\":\"#GTMTips: Check For Undefined Value - Som2ny Network\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg\",\"datePublished\":\"2025-02-25T18:38:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fivemor.com\/?p=109625#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fivemor.com\/?p=109625\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/?p=109625#primaryimage\",\"url\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg\",\"contentUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fivemor.com\/?p=109625#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fivemor.com\/?bp_activities=1\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"#GTMTips: Check For Undefined Value\"}]},{\"@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":"#GTMTips: Check For Undefined Value - 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=109625","og_locale":"en_US","og_type":"article","og_title":"#GTMTips: Check For Undefined Value - Som2ny Network","og_description":"This is one of those #GTMTips posts that I was certain I\u2019d already written. So it came as a mild surprise when I realized I\u2019d never tackled this aspect of Google Tag Manager. It\u2019s a short and sweet tip again. Today we\u2019ll learn how to check if a variable is undefined using Google Tag Manager. [&hellip;]","og_url":"https:\/\/fivemor.com\/?p=109625","og_site_name":"Som2ny Network","article_published_time":"2025-02-25T18:38:25+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fivemor.com\/?p=109625#article","isPartOf":{"@id":"https:\/\/fivemor.com\/?p=109625"},"author":{"name":"admin","@id":"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371"},"headline":"#GTMTips: Check For Undefined Value","datePublished":"2025-02-25T18:38:25+00:00","mainEntityOfPage":{"@id":"https:\/\/fivemor.com\/?p=109625"},"wordCount":487,"commentCount":0,"publisher":{"@id":"https:\/\/fivemor.com\/#organization"},"image":{"@id":"https:\/\/fivemor.com\/?p=109625#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg","keywords":["Check","GTMTips","Undefined"],"articleSection":["Analytics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fivemor.com\/?p=109625#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fivemor.com\/?p=109625","url":"https:\/\/fivemor.com\/?p=109625","name":"#GTMTips: Check For Undefined Value - Som2ny Network","isPartOf":{"@id":"https:\/\/fivemor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fivemor.com\/?p=109625#primaryimage"},"image":{"@id":"https:\/\/fivemor.com\/?p=109625#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg","datePublished":"2025-02-25T18:38:25+00:00","breadcrumb":{"@id":"https:\/\/fivemor.com\/?p=109625#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fivemor.com\/?p=109625"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/?p=109625#primaryimage","url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg","contentUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/check-for-undefined-value.jpg","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/fivemor.com\/?p=109625#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fivemor.com\/?bp_activities=1"},{"@type":"ListItem","position":2,"name":"#GTMTips: Check For Undefined Value"}]},{"@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\/109625","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=109625"}],"version-history":[{"count":0,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts\/109625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/media\/109627"}],"wp:attachment":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109625"},{"taxonomy":"dealstore","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fdealstore&post=109625"},{"taxonomy":"offerexpiration","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fofferexpiration&post=109625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}