Skip to main content
Known Participant
March 14, 2024
Answered

Question about placing a google analytics script on your html-page

  • March 14, 2024
  • 5 replies
  • 2650 views

Dear all,

 

I have a google analytics script I need to place in the code of my website www.dickensfellowship.nl.

The instructions tell that I must place in under the <head> tag on each html-page. Since my website has many pages, this will take a lot of work. As most pages are connected to a template I thought it was best to put this code in there instead and update the connected pages so that I could save time. However, I'm not sure if this code will work when it's placed in a non-editable region within a page. What's your experience? Your advice?

 

Silly question, perhaps.

 

    This topic has been closed for replies.
    Correct answer L e n a

    You have no problem placing this code fragment within a TEMPLATE.

    If you're using several different templates, depending on the sections of your site, and since you're already using a proprietary aspect of DW, I'd advise you to use a LIBRARY element instead, which you'll then insert into each TEMPLATE. This way, if you need to modify GA's snipet, you'll only have to make one modification, and DW will pass it on to all the templates, and then all the pages.

    For my part, I prefer to place this fragment just before the closing BODY, as this allows me to intercept only real user interaction, not to interfere with the loading time of real content and thus improve usability. Placing it in the HEADER benefits GA more, as it analyzes various behaviors that are favorable to it, and not really to you.

    By the way, and just for your information, have you heard of Piwik, now Matomo https://matomo.org/?

    5 replies

    Nancy OShea
    Community Expert
    March 15, 2024

    You have a place for Google Tag Manager in your current template.  Analytics can go in the same place.

     

    <!-- Include all compiled plugins (below), or include individual files as needed -->  
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap-4.4.1.js"></script>
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-2B2L2HYSVP"></script>
    <script>window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date()); gtag('config', 'G-2B2L2HYSVP');
    </script>
    </body>
    </html>

     

    Nancy O'Shea— Product User, Community Expert & Moderator
    Community Expert
    March 15, 2024
    quote

    You have a place for Google Tag Manager in your current template.  Analytics can go in the same place.

     

    <!-- Include all compiled plugins (below), or include individual files as needed -->  
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap-4.4.1.js"></script>
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-2B2L2HYSVP"></script>
    <script>window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date()); gtag('config', 'G-2B2L2HYSVP');
    </script>
    </body>
    </html>
    By @Nancy OShea

     

    I share your reasoning 100%, but going further if @edinav24550678 uses two distinct templates in the website, a LIBRARY item will provide more flexible encapsulation of your code (see previous comment above)... than copying this code directly into the TEMPLATE. 

    Known Participant
    March 15, 2024

    Thank you all for your replies. I've added the google-script on each page via the templates just above the "body"-closing tag and uploaded all pages. I've also added the code to my Library. Just wait a week to see what results Google Analytics will show.

    Nancy OShea
    Community Expert
    March 14, 2024

    1. I don't recommend using Library Items. They're too limited.  And besides, you're already using a sitewide Template.  You don't need both.

     

    2. Inasmuch as analytics scripts are not mission critical and can actually decrease performance, you want the imporant parts of your page to take priority & load first. 

     

    Please do what @BenPleysier suggested and add the Google Analytics 4 code just above the closing </body> tag of your main Template.dwt file. 

     

    <script>

    <!--Google Analytics 4 Code here-->

    </script>

    </body>

     

    3. Save Template & populate changes to child pages.

    4. Republish your entire site.

     

    You should start seeing results in your Google Console with 48 hours.

     

    Nancy O'Shea— Product User, Community Expert & Moderator
    Brainiac
    March 14, 2024
    quote

     

    Please do what @BenPleysier suggested and add the Google Analytics 4 code just above the closing </body> tag of your main Template.dwt file. 

     


    By @Nancy OShea

     

    Ben didn't suggest it, Lena did. The bloody big 'thumbs up' emoji is rather eye catching though!

    Nancy OShea
    Community Expert
    March 14, 2024
    quoteBen didn't suggest it, Lena did.
    By @osgood_

    =========

    I stand corrected.

    The mention of Library Items chased me away.  🙂

     

     

     

     

    Nancy O'Shea— Product User, Community Expert & Moderator
    Community Expert
    March 14, 2024

    More than likely Google Analytics may end up being the first in a long line of scripts you will need to add to your pages. I would typically recommend looking to install Google Tag Manager ( https://tagmanager.google.com/#/home ) and embed the analytics script on your page and embed all of your other scripts like it as well. This will give you better management to avoid DW templates or adding code manually.  In addition you will also be able to preview your changes before you publish them allowing you to test before integrating new scripts that could potentially break your site.

    L e n aCorrect answer
    Community Expert
    March 14, 2024

    You have no problem placing this code fragment within a TEMPLATE.

    If you're using several different templates, depending on the sections of your site, and since you're already using a proprietary aspect of DW, I'd advise you to use a LIBRARY element instead, which you'll then insert into each TEMPLATE. This way, if you need to modify GA's snipet, you'll only have to make one modification, and DW will pass it on to all the templates, and then all the pages.

    For my part, I prefer to place this fragment just before the closing BODY, as this allows me to intercept only real user interaction, not to interfere with the loading time of real content and thus improve usability. Placing it in the HEADER benefits GA more, as it analyzes various behaviors that are favorable to it, and not really to you.

    By the way, and just for your information, have you heard of Piwik, now Matomo https://matomo.org/?

    Brainiac
    March 14, 2024
    quoteIf you're using several different templates, depending on the sections of your site, and since you're already using a proprietary aspect of DW, I'd advise you to use a LIBRARY element instead, which you'll then insert into each TEMPLATE. 
     
    By @L e n a

     

    I'm probably missing something here but why not ditch the proprietary DW library item and just use a script tag:

     

    <script src="google_ analytics.js"></script>

     

    If you link that to every page in which you want the GA  code snippet to appear you then only have to update the google_analytics.js file, if needed. However I can't see it would need updating too frequently.....do Google regularly update it? I can't remember ever needing to update the scripts on any website on which I included the GA code. For sure they had a recent big update, I think, but prior to that I don't think anything changed, that I know of anyway, not that I paid too much attention to that side of things as none of my clients could ever be bothered to analyse their data.

     

    I dont like proprietary solutions myself when not strictly necessary as they lock you into a workflow which can't be shared with other editors very easily. Given that DW is not currently being actively developed  I would not be advising anyone to lock themselves into a padlocked box just in case they need a quick escape route.

    Brainiac
    March 15, 2024
    quote

    I get your point about additional http requests but that's the modern way isn't it? According to gen Z web developers the more links to various scripts, cdns/local included on every page, even if they are not required for that page, the better is what lm currently witnessing.......so yes, whilst this should not be, it's obviously not a huge concern.


    By @osgood_

     

    Not just Gen Z. It is what it is when the HTTP server is version 2 or higher. Please read up on it so that you can convey the correct message yourself.


    quote
    quote

    I get your point about additional http requests but that's the modern way isn't it? According to gen Z web developers the more links to various scripts, cdns/local included on every page, even if they are not required for that page, the better is what lm currently witnessing.......so yes, whilst this should not be, it's obviously not a huge concern.


    By @osgood_

     

    Not just Gen Z. It is what it is when the HTTP server is version 2 or higher. Please read up on it so that you can convey the correct message yourself.


    By @BenPleysier

     

    I was referring to gen z as its those mostly who seem to enjoy over complicating web development by following ridiculous workflows used by multi billion dollar companies. For sure there are going to be a percentage of "grandad" web developers whose ability to think for themselves has long since faded who will follow trends.

    Jon Fritz
    Community Expert
    March 14, 2024

    Put it in the Template, outside the editable region, as instructed. Then allow DW to update all of your Child pages connected to your .dwt file.

    It will work as expected.

    Known Participant
    March 14, 2024

    Thanks for your reply.