Developer
Developer

Reputation: 83

How to add a script at top of the <body> section of my HTML using Google Tag Manager

I need to put this code at the top of the body section of my HTML using Google Tag Manager. I am not able to understand how to put this at the top of 'body' section.

<script>
window.__crmp = {
config: {
storeData: false, 
appliesGlobally: false,
testingMode: 'never show' //change to 'normal' when testing is complete 
}
} 
</script>
<script src = 'URL' async> </script>  

Any help is greatly appreciated. Thanks!

Upvotes: 1

Views: 6114

Answers (1)

i--
i--

Reputation: 4360

You can write javascript to place your code directly at the top of the body tag via Google Tag Manager (GTM), but it would help with the order of when it's triggered. If you need to execute it before the page loads, here is what you can do:

  1. Create new trigger: GTM > Triggers > New > DOM Ready (don't forget to name your trigger).
  2. Create new tag: GTM > Tags > New (don't forget to name your tag). In the "Tag Configuration" section select "Custom HTML" and place your code into the text box that will appear. In the "Triggering" section below select the trigger you just created and named.

That will execute your code before the page loads which is equivalent to your cod being inside the body tag, but not necessarily at the top of it. You can't control that unless you actually place that javascript snippet directly in the html of your site.

Edit: I just checked and it looks like the "Page View" trigger is fired before the "DOM Ready" one, so I would probably use that instead, in your case.

Upvotes: 1

Related Questions