Script placement differences between 1.x and 2.x

Created on 30 May 2024, 6 months ago
Updated 27 August 2024, 3 months ago

Problem/Motivation

We've been using the module for our Drupal 10 upgrades and we've noticed some interesting differences that are somwhat confusing.

On the 1.x version, the script is added using $attachments['#attached']['html_head'][]. On the 2.x version it is added at the end of the page as a standard library which then runs a separate script to add on the html head.

So technically, version 1 runs as soon as it reads the tag, and version 2 loads everything and then it will load the tags.

However, on both versions the frame is added on hook_page_top.

So I'm just a little confused about this change. It is my understanding (and I'm not an expert but would be interested in being corrected) is that GTM script should always be put on the html head from the markup and not as part of a script.Maybe this decision was made in order to reduce blocking time?

Anyway, I'd appreciate if you could shed some light on this.

We are experiencing issues on one site and I want to rule out this change being the cause, as all the GTM stuff was working (based on the client feedback) before we did the upgrade.

Thanks!

πŸ’¬ Support request
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡·Costa Rica alemadlei

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @alemadlei
  • πŸ‡¨πŸ‡¦Canada gapple

    In 1.x a dynamically created inline script is added to the document head that:
    1. Initializes a placeholder dataLayer array
    2. Adds the external tag manager script to the page with the defer attribute. The browser will download the script, but not execute it until the DOM has finished parsing.

    In 2.x a local script file is added to the bottom of the page that:
    1. Initializes a placeholder dataLayer array.
    2. Uses configuration passed with drupalSettings to add the external tag manager script to the page with the async attribute (since the script is at the bottom of the page, it is technically already defered). The browser will download the script and execute it when it is available (and the JS thread is not otherwise occupied).

    The only functional difference is when the dataLayer variable is available to other scripts on the page. With 2.x, other libraries which use dataLayer will need to specify the google_tag library as a dependency to ensure they are correctly ordered to execute later when dataLayer is available. Any inline scripts inserted earlier in the page will not have access to the dataLayer variable. (You should get errors in the browser console if this were causing issues).
    You can alter the google_tag library to place it in the head of the page so that dataLayer is available earlier in the page parsing.

  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States adanbouzoua

    adanbouzoua β†’ changed the visibility of the branch 3451427-script-placement-differences to hidden.

  • Status changed to Closed: works as designed 3 months ago
Production build 0.71.5 2024