{"id":1224,"date":"2023-04-22T17:55:01","date_gmt":"2023-04-22T16:55:01","guid":{"rendered":"https:\/\/organicdigital.co\/blog\/?p=1224"},"modified":"2026-09-21T11:59:23","modified_gmt":"2026-09-21T10:59:23","slug":"how-to-use-chatgpt-ai-with-gtm","status":"publish","type":"post","link":"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/","title":{"rendered":"How To: Use ChatGPT with Google Tag Manager"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>TL;DR:<\/strong> <br>ChatGPT can be useful alongside Google Tag Manager for generating Custom JavaScript variables, writing regular expressions, debugging code and creating more bespoke tracking implementations. I use it to speed up the coding side of GTM rather than replace the knowledge needed to design the tracking. The important part is to understand and adapt the generated code, then test it properly in GTM Preview mode before publishing anything.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#How_I_use_ChatGPT_with_Google_Tag_Manager\" >How I use ChatGPT with Google Tag Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_create_Custom_JavaScript_variables_in_GTM\" >Use ChatGPT to create Custom JavaScript variables in GTM<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Extract_PDF_name_from_path\" >Extract PDF name from path<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_clean_email_and_telephone_click_data\" >Use ChatGPT to clean email and telephone click data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_reformat_a_GTM_variable\" >Use ChatGPT to reformat a GTM variable<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_fix_JavaScript_errors_in_GTM\" >Use ChatGPT to fix JavaScript errors in GTM<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_write_regular_expressions_for_GTM\" >Use ChatGPT to write regular expressions for GTM<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Use_ChatGPT_to_create_a_Custom_HTML_listener_in_GTM\" >Use ChatGPT to create a Custom HTML listener in GTM<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/daveashworth.co\/blog\/how-to-use-chatgpt-ai-with-gtm\/#Should_you_use_ChatGPT_to_write_code_for_Google_Tag_Manager\" >Should you use ChatGPT to write code for Google Tag Manager?<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_I_use_ChatGPT_with_Google_Tag_Manager\"><\/span>How I use ChatGPT with Google Tag Manager<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As a <a href=\"https:\/\/daveashworth.co\/\">website optimisation consultant<\/a>, I regularly use <a href=\"https:\/\/daveashworth.co\/skills\/google-tag-manager-consultant\/\">Google Tag Manager<\/a> for analytics, SEO and more bespoke tracking implementations. Some of those tasks require JavaScript, regular expressions or custom HTML, which is where I&#8217;ve found ChatGPT particularly useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have a background in coding, but that doesn&#8217;t mean I want to spend time writing every function from scratch. I can use ChatGPT to generate a starting point, then use my own knowledge of GTM and JavaScript to adapt the code, test it and make sure it actually does what I need.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide I&#8217;ll show you some real examples of how I&#8217;ve used <strong>ChatGPT with Google Tag Manager<\/strong>, including creating Custom JavaScript variables, fixing JavaScript errors, writing regular expressions and building a Custom HTML listener for <a href=\"https:\/\/daveashworth.co\/skills\/analytics-and-ad-tracking-consultant\/\">bespoke tracking<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The important bit is that I don&#8217;t simply copy AI-generated code into a live GTM container. I test it in Preview mode and check the resulting data before publishing it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_create_Custom_JavaScript_variables_in_GTM\"><\/span>Use ChatGPT to create Custom JavaScript variables in GTM<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the main ways I use ChatGPT with GTM is to help create <strong>Custom JavaScript variables<\/strong>. These are useful when the data available in GTM isn&#8217;t quite in the format I want to send to GA4, so I can use JavaScript to extract, clean or reformat the value first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are a few examples I&#8217;ve actually used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Extract_PDF_name_from_path\"><\/span><strong>Extract PDF name from path<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, the <code>file_download<\/code> event can leave me with a full file path when what I actually want for reporting is a clean filename, for example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/directory\/uploads\/folder\/filename.pdf<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I asked ChatGPT to write a JavaScript function that extracts just the filename from the Click URL:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"602\" height=\"581\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chatgpt_prompt_pdf.png\" alt=\"chatgpt prompt pdf\" class=\"wp-image-1226\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chatgpt_prompt_pdf.png 602w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chatgpt_prompt_pdf-300x290.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chatgpt_prompt_pdf-150x145.png 150w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">and just like that I have code that can be adapted for use within a Custom JS Variable which takes the Click URL as the input:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function()\n{\n  var this_url = {{Click URL}};\n  var url_parts = this_url.split(\"\/\");\n  return url_parts&#91;url_parts.length - 1];\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I adapted the output for a GTM Custom JavaScript variable using <code>{{Click URL}}<\/code> as the input and called the variable <strong>cJS \u2013 PDF<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can then use the resulting value as a parameter in my GA4 event:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"488\" height=\"344\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf.png\" alt=\"click_pdf event\" class=\"wp-image-1227\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf.png 488w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf-300x211.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf-150x106.png 150w\" sizes=\"(max-width: 488px) 100vw, 488px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">which makes for a much cleaner looking report:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"335\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report-1024x335.png\" alt=\"click_pdf report\" class=\"wp-image-1228\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report-1024x335.png 1024w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report-300x98.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report-150x49.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report-768x251.png 768w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_pdf_report.png 1191w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_clean_email_and_telephone_click_data\"><\/span>Use ChatGPT to clean email and telephone click data<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The same approach is useful for cleaning up other values before sending them to GA4. For example, if I&#8217;m tracking email or telephone link clicks, I might want the actual email address or telephone number in my reports rather than values beginning with <code>mailto:<\/code> or <code>tel:<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an email click, I asked ChatGPT to remove the <code>mailto:<\/code> prefix:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"614\" height=\"326\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_email.png\" alt=\"click_emil prompt\" class=\"wp-image-1229\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_email.png 614w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_email-300x159.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_email-150x80.png 150w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">which is amended to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function()\n{\n  var email_address = \"{{Click URL}}\";\n  if (email_address.startsWith(\"mailto:\")) {\n    return email_address.slice(7);\n  } else {\n    return email_address;\n  } \n}\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The same principle works for telephone links. This time I want to remove <code>tel:<\/code> from the Click URL:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"622\" height=\"334\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_tel-1.png\" alt=\"click_tel  prompt\" class=\"wp-image-1231\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_tel-1.png 622w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_tel-1-300x161.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_tel-1-150x81.png 150w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">which is amended to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function()\n{\n  var phone_number = \"{{Click URL}}\";\n  if (phone_number.startsWith(\"tel:\")) {\n    return phone_number.slice(4);\n  } else {\n    return phone_number;\n  } \n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_reformat_a_GTM_variable\"><\/span><strong>Use ChatGPT to reformat a GTM variable<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Another example came from tracking social share clicks. As well as the social platform and page path, I wanted to send the page title with the event.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On one site the H1 text was entirely uppercase, which wasn&#8217;t how I wanted it to appear in GA4. Rather than manually writing the JavaScript to reformat it, I asked ChatGPT to create the function:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"625\" height=\"297\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/titleCase_prompt.png\" alt=\"titleCase prompt\" class=\"wp-image-1232\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/titleCase_prompt.png 625w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/titleCase_prompt-300x143.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/titleCase_prompt-150x71.png 150w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">which is amended to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function ()\n{\n  var this_h1 = {{h1.title}};\n  return this_h1.toLowerCase().replace(\/(^|\\s)\\S\/g, function(firstLetter) {\n    return firstLetter.toUpperCase();\n  });\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">None of these are particularly complex JavaScript functions, and I could have written them myself with enough time. That&#8217;s not really the point.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ChatGPT gives me a quick starting point, while my knowledge of JavaScript, GTM and the tracking implementation lets me check the output, adapt it to use GTM variables and test that it works correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_fix_JavaScript_errors_in_GTM\"><\/span><strong>Use ChatGPT to fix JavaScript errors in GTM<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ChatGPT-generated JavaScript doesn&#8217;t always work first time, particularly when the code needs to run within Google Tag Manager.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If GTM returns an error, I can give ChatGPT the error message along with the code it generated and ask it to identify the problem and provide a corrected version. I still need to test the revised code rather than assume the second answer is correct.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, when I first asked ChatGPT to write the code for reformatting the uppercase and lowercase characters in the page title, it gave me the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"602\" height=\"658\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v1.jpg\" alt=\"capitalise_v1\" class=\"wp-image-1233\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v1.jpg 602w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v1-274x300.jpg 274w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v1-137x150.jpg 137w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When I tried to preview the code in GTM I got the following error:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"496\" height=\"260\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_error.png\" alt=\"GTM error\" class=\"wp-image-1234\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_error.png 496w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_error-300x157.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_error-150x79.png 150w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So I informed ChatGPT of this, it apologised, then provided updated code which worked:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"602\" height=\"577\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v2.jpg\" alt=\"capitalise_v2\" class=\"wp-image-1235\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v2.jpg 602w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v2-300x288.jpg 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/capitalise_v2-150x144.jpg 150w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_write_regular_expressions_for_GTM\"><\/span>Use ChatGPT to write regular expressions for GTM<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve worked with various programming languages over the years, including HTML, CSS, JavaScript, SQL and PHP.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, I have NEVER got my head around RegEx. I can&#8217;t do it and tend to shy away as soon as my first attempt doesn&#8217;t work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not anymore though.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One of the custom events I create as part of my GA4 configurations is to <a href=\"https:\/\/daveashworth.co\/blog\/how-to-track-internal-link-clicks-in-google-analytics-4-via-gtm\/\">track internal link clicks<\/a> and identify whether the link was clicked in the header, main content or footer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I do this my setting the click URL trigger to match the site domain so as not to confuse it for an external link click.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recently, I was working on a site that had internal linking between the subdomains www, shop, member and tickets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So I needed a trigger that would match the Click URL to any of those subdomains:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"554\" height=\"686\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/subdomains_prompt.png\" alt=\"subdomains prompt\" class=\"wp-image-1236\" style=\"width:554px;height:686px\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/subdomains_prompt.png 554w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/subdomains_prompt-242x300.png 242w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/subdomains_prompt-121x150.png 121w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And it works a treat:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"789\" height=\"468\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_page_content.png\" alt=\"click page content\" class=\"wp-image-1237\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_page_content.png 789w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_page_content-300x178.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_page_content-150x89.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/click_page_content-768x456.png 768w\" sizes=\"(max-width: 789px) 100vw, 789px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_ChatGPT_to_create_a_Custom_HTML_listener_in_GTM\"><\/span><strong>Use ChatGPT to create a Custom HTML listener in GTM<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A more complex example came from a client who publishes a lot of useful content that they&#8217;re happy for visitors to copy and use elsewhere. They wanted to know <strong>how often content was being copied, which pages it was copied from and what text users were copying<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;d never previously built this particular type of tracking in GTM, so rather than starting from scratch, I gave ChatGPT a detailed description of what I wanted the implementation to do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is where being specific with the prompt becomes particularly important. I needed the code to listen for copy events, capture the selected text and page information, and push the resulting data into the data layer so I could use it in GTM.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"627\" height=\"765\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/listener_prompt.png\" alt=\"Write A Custom HTML Listener\" class=\"wp-image-1238\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/listener_prompt.png 627w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/listener_prompt-246x300.png 246w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/listener_prompt-123x150.png 123w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">ChatGPT generated a listener that I could adapt and add to a Custom HTML tag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n  document.addEventListener('copy', function(event) {\n    var copiedText = window.getSelection().toString();\n    var pageUrl = window.location.href;\n    dataLayer.push({\n      'event': 'copy_text',\n      'copiedText': copiedText,\n      'pageUrl': {{Page Path}}\n    });\n  });\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the listener pushes the <code>copy_text<\/code> event and associated values into the data layer, I can use those values within the rest of the GTM configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I created the required Data Layer Variables, a Custom Event trigger for <code>copy_text<\/code>, and a GA4 event to send the data to Analytics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"833\" height=\"680\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chtml.png\" alt=\"chtml\" class=\"wp-image-1239\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chtml.png 833w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chtml-300x245.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chtml-150x122.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/chtml-768x627.png 768w\" sizes=\"(max-width: 833px) 100vw, 833px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Then create variables to pull in the text from the data layer, and a custom event trigger that calls the GA4 Event:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"801\" height=\"357\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/dlv.png\" alt=\"variable\" class=\"wp-image-1240\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/dlv.png 801w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/dlv-300x134.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/dlv-150x67.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/dlv-768x342.png 768w\" sizes=\"(max-width: 801px) 100vw, 801px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"791\" height=\"371\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/custom_event.png\" alt=\"custom event\" class=\"wp-image-1241\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/custom_event.png 791w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/custom_event-300x141.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/custom_event-150x70.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/custom_event-768x360.png 768w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"846\" height=\"613\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/ga4_event.png\" alt=\"event ga4 cxopy text\" class=\"wp-image-1242\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/ga4_event.png 846w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/ga4_event-300x217.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/ga4_event-150x109.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/ga4_event-768x556.png 768w\" sizes=\"(max-width: 846px) 100vw, 846px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Which then fires as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"377\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text-1024x377.png\" alt=\"copying text\" class=\"wp-image-1243\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text-1024x377.png 1024w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text-300x110.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text-150x55.png 150w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text-768x283.png 768w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/copy_text.png 1078w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"439\" height=\"337\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_tab.png\" alt=\"gtm debugger \" class=\"wp-image-1244\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_tab.png 439w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_tab-300x230.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/gtm_tab-150x115.png 150w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"530\" height=\"493\" src=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/event_fired.png\" alt=\"tag details\" class=\"wp-image-1245\" srcset=\"https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/event_fired.png 530w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/event_fired-300x279.png 300w, https:\/\/daveashworth.co\/blog\/wp-content\/uploads\/2023\/04\/event_fired-150x140.png 150w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This is a good example of where I find AI particularly useful with GTM. I knew what I wanted to track and how I wanted the resulting data to work, but I didn&#8217;t already have the JavaScript listener to make it happen. ChatGPT helped bridge that gap, while GTM Preview mode allowed me to verify that the event and data were actually being captured correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Should_you_use_ChatGPT_to_write_code_for_Google_Tag_Manager\"><\/span>Should you use ChatGPT to write code for Google Tag Manager?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I&#8217;ve found ChatGPT particularly useful for Google Tag Manager tasks where I know <strong>what I want to achieve<\/strong>, but don&#8217;t necessarily want to spend time writing the JavaScript or regular expression from scratch.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The examples above cover several ways I&#8217;ve used it in real implementations: creating Custom JavaScript variables, fixing code that doesn&#8217;t work, writing regex and building a Custom HTML listener for more bespoke tracking requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key is not to blindly copy and paste whatever ChatGPT gives you into a live GTM container. Be specific about what you want, understand what the generated code is doing, adapt it where necessary, and test everything in GTM Preview mode before publishing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For me, that&#8217;s where AI is most useful with GTM: <strong>it speeds up the coding part of the job without replacing the knowledge needed to design, implement and validate the tracking itself.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have fun.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An exploration into how to use ChatFPT AI in conjunction with Google Tag Manager (GTM).  Find out how to automate workflow, save time and simplify tasks.<\/p>\n","protected":false},"author":1,"featured_media":1225,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tag-manager"],"_links":{"self":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/comments?post=1224"}],"version-history":[{"count":0,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/posts\/1224\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media\/1225"}],"wp:attachment":[{"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/media?parent=1224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/categories?post=1224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daveashworth.co\/blog\/wp-json\/wp\/v2\/tags?post=1224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}