I have a Visualforce page, used in the Sites, that I've been asked to add the Google tag manager to.

I have one block of script that comes with the instruction to "embed the following Javascript code before the close tag of the header"

I have another code block that's an iframe wrapped in a block. that is supposed to go "after the opening body tag".

How can I place these blocks when "header" and "body" are not Visualforce tags?

This answer suggests making an empty html5 container page, but I need content in my page:

How to add GTM Codes in the Visualforce Page

1 Answer 1


Visualforce pages have implicit html and body tag, so yeah, you can add those blocks at the beginning or end of your page, as specified.

Also, nothing stops you from adding your own html and body tags to the visualforce page, even if you don't use any more standard html tags. For example, you can do this

<apex:page showHeader="false" standardController="User"
extensions="ProfileTabUserController" >

<apex:outputPanel >

    <p>Your name is {!$User.FirstName} {!$User.LastName}, and 
    you're the viewer. Your UID is {!viewerID}.</p>

    <p>You are viewing the profile of {!user.name}, 
    whose UID is {!subjectID}.</p>

    <p>Are you viewing your own profile? {!viewingMyProfile}</p>


Notice how the body tag is inside the apex:page tag, and that's totally fine.

  • When I add my own body tag, I see two in the page when I view the page source. There is only one in the Visualforce page. I'm not sure where the second one comes from. Is Salesforce adding it when it generates the page, or is it added by a script somehow?
    – Jagular
    Sep 26, 2019 at 15:00
  • It's probably implicitly added by salesforce. If it's not breaking your script, don't worry about it. Sep 26, 2019 at 15:12
  • Elsewhere, I'm told that having multiple body tags is invalid html. I don't know that it's a problem, but I don't know that it isn't. stackoverflow.com/questions/2035462/…
    – Jagular
    Sep 26, 2019 at 15:16
  • I depends. Browsers can read that anyway and might automatically correct errors. If it's not breaking the page, or your script, then it's ok. Sep 26, 2019 at 15:20
  • 1
    Turns out it is possible to suppress the automatic generation of the body tag with applyBodyTag="false". That is half of what I need. Detailed here: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – Jagular
    Sep 26, 2019 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.