Problem/Motivation

Since https://www.drupal.org/project/google_tag/issues/3452712, parts of this module's javascript load in the <head> element and have started somewhat of a cascade effect for performance.

The gtag and gtm libraries have a dependency on core/drupalSettings which causes the drupal settings loader to become render-blocking as well. All in all, it's fairly bad business to load javascript synchronously in the head these days.

Steps to reproduce

Enable the module, notice first paint is blocked by a round-trip for javascript resources that don't actually influence rendering.

Proposed resolution

Revert the change that loads the javascript synchronously.

GTM based site verification is only one mechanism of ownership verification that Google offers!

Remaining tasks

  1. Review
  2. Commit
  3. Update documentation to encourage site owners to use alternative verification methods

User interface changes

None

API changes

None

Data model changes

None

CommentFileSizeAuthor
#6 new-optimize-web-vitals-setting.png110.81 KBluke.leber

Issue fork google_tag-3549374

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

luke.leber created an issue. See original summary.

dpagini’s picture

Priority: Normal » Major

I'd like to propose this issue as a major priority. If you don't want this change due to the aforementioned performance issues, you have to rollback to 2.0.3, I believe, and that version of the module has an open security threat.

nitesh624’s picture

yes we can't roll back to previous version due to security issue in older version. I believe this need to be prioritize and worked on based on suggestion given.

nitesh624’s picture

Is there any update here?

nitesh624’s picture

I did some research around this I posted my findings in https://www.drupal.org/project/google_tag/issues/3452712 issue.

I think we can rollback changes done in https://www.drupal.org/project/google_tag/issues/3452712 and address the actual root cause for that

luke.leber’s picture

Issue summary: View changes
StatusFileSize
new110.81 KB

Updated proposed resolution; merge request imminent.

luke.leber’s picture

Status: Active » Needs review
luke.leber’s picture

Issue summary: View changes
dpagini’s picture

Thanks for putting this up, @luke.leber!

I have a question for this module, and I was thinking I'm going to create a new issue for it, but I might as well start here... In my team's testing, the original PR that does this is 3452712, which states that there was a problem registering the site with "Google Search Console".

In our testing, I can't register a site with the google_tag module installed BEFORE 3452712, and I can't register a site with it AFTER. In both cases, I get the error message that the snippet is in the wrong location. I almost expected this, b/c this module doesn't add the code that Google tells you to use directly, it's wrapped in some Drupal-ism wrapper javascript so we can use drupalSettings, and included via a local JS file. So I have a lot of questions if the original change was fully tested...? Or maybe Google changed since then, I just can't figure out how to determine what happened...

Anyways, one of our team's developers figured out that, he actually COULD get Google Search Console to recognize the site by making sure the <noframe> snippet was immediately following the body tag, which it does not do by default.

So this new MR is going to make this an optional setting... but I guess I want to challenge if this code should even be left intact in the first place. If it's a) not working, and b) causing some real javascript challenges... why not just revert 3452712? I really don't see any comments on that original issue that indicate that this worked for them...

luke.leber’s picture

Hey David,

My take on the situation is that this module was "doing it wrong" from the beginning by making the Google libraries dependent on the Drupal libraries (purely speaking in terms of site verification through Tag Manager). Given we can't know how Google operates internally when it comes to such verification, I think it'd be safest to encourage site owners to use alternative verification methods wholesale.

My first pass at this (that I didn't commit) also added a textfield for the meta-tag driven verification method as well, but I opted against that in the end because it seems like something this module shouldn't be doing.

I would wager that this module has always had somewhat of a race condition with Google's GTM based verification method. If the maintainers would be up to reverting #3452712: Possibly script loading/placement issue that would be preferable to me as well.

dpagini’s picture

Yeah, I think we're on the same page.

This is slightly funny, this conversation reminded me of recently coming across this issue, https://www.drupal.org/project/drupal/issues/3542042, which you also appear to be the author of, so not much use in brining it forward, ha! But it almost seems like the google_tag module should do something like this inline approach if a user wants the "google way" of loading it as high as possible on the page...

Any suggestions on getting some maintainers attention on this...? It seems these issues with the header: true have been ongoing since this was committed in the Fall of '24, and issues like this, to bring up that concern, have seemed to be going ignored... maybe the entire module is not actively maintained...?

catch’s picture

Reverting #3452712: Possibly script loading/placement issue and trying to fix the original problem in a different way sounds good to me too.

nitesh624’s picture

yes agreed with @catch here

luke.leber’s picture

Issue summary: View changes

Update IS.

nitesh624’s picture

Status: Needs review » Reviewed & tested by the community

Latest MR worked for me

nagy.balint’s picture

MR !128 gave me an around 20 point increase in the mobile performance score on google pagespeed insights.

japerry made their first commit to this issue’s fork.

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Confirmed the bug with a test. MR !128 looks correct to me. Fixed!

Note, in our release notes:

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.