Inserting Tags in the Head Section Using Google Tag Manager

GOOGLE TAG MANAGER (GTM)

By default, GTM injects tags either right after the opening <body> tag or just before the closing </body> tag. The exact placement typically depends on where you have inserted the GTM container code in your site's source code. This behavior occurs because most scripts and tags can operate effectively within the <body> and don't necessarily need to be placed in the <head>.

Steps to Insert Custom HTML Tags with GTM

Create a custom HTML TAG and attach a trigger

<meta name="google-site-verification" content="This was created in HTML format">

When we validate this TAG on the website, we notice that the tag got injected in the BODY section of the HTML.

I created a demo website to demonstrate this.

While GTM doesn’t directly offer a built-in functionality to position tags in the head, we can achieve this using a clever JavaScript workaround.

Steps to forcefully Insert Custom HTML Tags in HEAD with GTM

<script type="text/javascript">
var metaTag = document.createElement('meta');
metaTag.name =
"google-site-verification";
metaTag.content =
"Tag tag was created in Javascript format";
document.getElementsByTagName(
'head')[0].appendChild(metaTag);
</script>

When we validate this TAG on the website, we notice that this time tag got injected in the HEAD section of the HTML

15+ years of IT work experience as Technical delivery Lead, Analytics Architect, AEP/CJA Implementation Consultant.

Adobe certified expert in Adobe Analytics, Adobe Target, Adobe Experience Platform (AEP), Real-Time Customer Data Platform (RT-CDP), Customer Journey Analytics (CJA), Journey Optimizer (AJO). Well versed with Google Analytics Server-side, Conversion API (CAPI), Privacy & Consent Management (OneTrust).

Deputed to Canada, USA, Netherlands, Germany, UK to work closely with business clients, business analysts, solution architects, solution designers, and other key stakeholders. Passionate to decode the online consumer behaviour by using an analytics data-driven approach.