Problem/Motivation

GTM script is added to html head more than once.

Steps to reproduce

* Drupal 9.1.4
* GoogleTagManager module enabled and configured
* cookies and cookies_gtm enabled
* Open website and agree to cookies
* Inspect with console, the gtm script is displaying twice in page head

GTM script duplicated in page head

CommentFileSizeAuthor
#5 cookies-gtm-issue.jpg164.61 KBbmunslow
cookies-gtm-duplicated.png144.77 KBkirkkala

Comments

kirkkala created an issue. See original summary.

jfeltkamp’s picture

Ok, I will fix this asap.

jfeltkamp’s picture

Status: Needs work » Postponed (maintainer needs more info)

Sorry, but I can't reproduce the issue. I need more information about your GTM configuration or a full image of the header as delivered from server and re-rendered by browser.

I suppose this issue is not made by COOKiES module. Please try to disable COOKiES and check again.

bmunslow’s picture

Status: Postponed (maintainer needs more info) » Needs work

Hi, I can confirm the same issue reported by @kirkkala, with the same environment (drupal/google_tag + drupal/cookies + drupal/cookies_gtag).

Every time you click the "Accept" button, the gtm script is added to the head of the page.

To reproduce:

  • Trigger the Settings dialog
  • Accept all cookies
  • Trigger the Settings dialog again (without reloading the page)
  • Accept all cookies
  • Check inspector console and see a new GTM <script> for each click

Duplicated GTM scripts in COOKiES module

bmunslow’s picture

StatusFileSize
new164.61 KB
kirkkala’s picture

Thanks @bmunslow for confirming and with a better explanation of the issue. And @JFeltkamp indeed the script does not get duplicated if I disable cookies ´cookies_gtag modules so cannot point out the issue in google_tag module/gtm.

I believe this could be fixed in cookies_gtag/js/cookies_gtag.js with some additional checking in the attach/activate but probably should be addressed for the other submodule js's as well. I have not unfortunately had time to look into this too much since it is not that critical as Google Tag Manager can deal with this (and I also quite suck with javascript).

  • JFeltkamp committed df351c0 on 1.0.x
    Issue #3200980 by kirkkala, bmunslow: GTM script duplicating after...
jfeltkamp’s picture

Status: Needs work » Needs review

Ok, now I can see the Problem (Sorry @kirkkala). Thank you @bmunslow for the tip.

I fixed that in 1.0.x and I also reviewed the other submodules for the same issue.
You were right that the problem also exists there. Now it should work correct.

Would be nice if you could review the master 1.0.x so I will create a new release asap.

kirkkala’s picture

Brilliant, thank you @JFeltkamp! I tested with composer require drupal/cookies:1.0.x-dev#df351c0 and the issue indeed is gone for gtag which we use in our setup. That is, I did not test with other submodules but code changes look good to me regarding array vs object as that seemed to be the root cause 👍

jfeltkamp’s picture

Status: Needs review » Fixed

Great. Thank you for the feedback @kirkkala.
I tested that in my test environment, where all features are installed - works fine there too.
Created a new release 1.0.8

Status: Fixed » Closed (fixed)

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