Google Tag Manager (GTM)

Ayoub Ag. asked on March 16, 2015 16:33

Hi, is there any way to be able to add GTM code directly after "<BODY>" tag via Cms Desk?

thank you

Correct Answer

Jan Hermann answered on March 17, 2015 13:37


no without a customization. The easiest solution is add it directly to the \CMSPages\PortalTemplate.aspx template.

Best regards,
Jan Hermann

0 votesVote for this answer Unmark Correct answer

Recent Answers

Brenden Kehren answered on March 17, 2015 19:03

Without modifying the base portal page template, you could use the javascript webpart as the very first item in your master page template. Or even place your code in your master page layout. No this will not get it directly after the opening body tag but it also doesn't require you to modify base code either.

As long as your scripts are the first scripts that run after the opening body tag you should be good.

2 votesVote for this answer Mark as a Correct answer

Peter de Warren answered on April 9, 2015 03:25 (last edited on April 9, 2015 04:04)

Hi, I found there was two ways to do this, not sure either is the optimal but both get the GTM tags directly after the body tags.

  1. Add the script to the "body" params text area in the master page template. I close off the body tag with a ">" then paste in the GTM script and I remove the last ">" tags from the script as it will be replaced by the existing Master page body code. Should look like this at the start ><!-- Google Tag Manager -->Your script GTM goes here and this at the end <!-- End Google Tag Manager --.

  2. I had an issue because I had sub-domains all using their own Google ID so I went with adapting the PortalTemplate.aspx page using a Literal tag plus some code behind. For the .aspx page I used:

    <body class="<%=BodyClass%>" <%=BodyParameters%>> <asp:Literal runat="server" ID="GTMTags" EnableViewState="false" /> <form id="form" runat="server">

For the code behind I used:

    `protected override void OnPreRender(EventArgs e)
        // Init the header tags
        tags.Text = HeaderTags;
        //insert Google GTM tags
        GTMTags.Text = getGTMTags(CMS.CMSHelper.CMSContext.CurrentSiteID).ToString();
        if (CMSContext.ViewMode == ViewModeEnum.Wireframe)

   protected String getGTMTags(int siteID)
        StringBuilder gtmHTML = new StringBuilder();

        int thisSite = siteID;

        String SQGTMCode = "GTM-********"; // default id

        switch (thisSite)
            case 2:
                SQGTMCode = "GTM-*******"; // main website
            case 5:
                SQGTMCode = "GTM-&&&&&&&&"; // another sub domain
            case 8:
                SQGTMCode = "GTM-@@@@@@@@"; // sub domain
                SQGTMCode = "GTM-*******";

        gtmHTML.Append("<!-- Google Tag Manager --> ");
        gtmHTML.Append("<noscript><iframe src=\"//" + SQGTMCode.ToString() + "\" height=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"></iframe></noscript>");
        gtmHTML.Append("w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});");
        gtmHTML.Append("var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';");
        gtmHTML.Append("j.async=true;j.src= '//'+i+dl;f.parentNode.insertBefore(j,f); })");
        gtmHTML.Append("(window,document,'script','dataLayer','" + SQGTMCode.ToString() + "');");
        gtmHTML.Append("<!-- End Google Tag Manager -->");
        return gtmHTML.ToString();

Like I said, it may not be the best way but it works, if anyone sees anything blatantly wrong with this code please let me know.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.