Create a submodule to load GTM only after consent.

Created on 8 February 2023, over 2 years ago

Problem/Motivation

When using the module https://www.drupal.org/project/google_tag β†’ , it loads the GTM without the user's consent.

Proposed resolution

Change how the GTM scripts are loaded and replace the "src" with "data-src" and add "data-name" as it's required for klaro to load the resource only after consent.

πŸ“Œ Task
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡·πŸ‡΄Romania szloredan

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

Merge Requests

Comments & Activities

  • Issue created by @szloredan
  • Status changed to Needs review over 2 years ago
  • πŸ‡©πŸ‡ͺGermany yannickoo Berlin

    Patch works fine, the only thing I'm wondering about is the `data-src` attribute of the `

    ` inside the `` tag since there is no chance to execute that at all. Shouldn't we rather remove it completely?
  • πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

    This breaks the loading of GTM when both usercentrics and simpleklaro are installed, but simple klaro is not enabled. Could we please add a check to ensure that Klaro is enabled and only then run the hooks?

  • First commit to issue fork.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update almost 2 years ago
    7 pass
  • πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

    The current version is not loading GTM on urls that are consisting of multiple slashes, e.g. example.com/x/y will try to load the script from https://www.example.com/x/modules/contrib/google_tag/js/gtm.js which is clearly the wrong path. The issue relies in how the data-src is created and is missing the leading slash so the links look like this in the DOM before being processed by klaro:

    <script src="/?snv6li" type="text/plain" data-type="application/javascript" data-name="google-tag-manager" data-src="modules/contrib/google_tag/js/gtm.js"></script>
    

    Next to not having the leading slash one can also see that the version query string is added to the src and not to the data-src attribute which is also wrong.

  • πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

    The updated MR results into the following script on the page:

    <script type="text/plain" data-type="application/javascript" data-name="google-tag-manager" data-src="/modules/contrib/google_tag/js/gtm.js?snvjrf"></script>

  • First commit to issue fork.
  • πŸ‡ͺπŸ‡ΈSpain Nodles

    I updated the submodule to version ^11.
    I tested compatibility, and everything works without requiring any changes. Gtag loads correctly as soon as it’s selected in Klaro.

  • πŸ‡©πŸ‡ͺGermany hchonov πŸ‡ͺπŸ‡ΊπŸ‡©πŸ‡ͺπŸ‡§πŸ‡¬

    I just pushed a commit to respect the consent mode and if enabled always to load the gtag.js, as otherwise the gtag() function is not available for the Klaro callbacks and breaks the banner if used there.

Production build 0.71.5 2024