Create a submodule to load GTM only after consent.

Created on 8 February 2023, almost 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 almost 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 12 months 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>

Production build 0.71.5 2024