Problem/Motivation

With "Consent method: opt-in" if I put some script with syntax "path/to/the/script.js" in "Disable JavaScripts" with "Enable cookie(s) automatic-removal when consent isn't given" everything seems to work as expected. Untill I agree the script is not working, after i agree the script is working and cookies setted up.

If i use che same script with the "Opt-in with categories" , and I put inside "Disable JavaScripts" the line with syntax category:path/to/the/script.js with "Enable cookie(s) automatic-removal the script seems to never load, even if i agree for the specific category.
If i check the browser cookies are not setted up.

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

davidebarbato created an issue. See original summary.

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

svenryen’s picture

Status: Active » Closed (cannot reproduce)

I'm afraid I can't reproduce.

Does your script in any way utilize drupal behaviors?

I set up a sample module "sven_test_1" and added a sample script using drush generate javascript.

This produced the following script:

(function (Drupal) {

  'use strict';

  Drupal.behaviors.svenTest1SvenTest1 = {
    attach: function (context, settings) {

      console.log('It works!');

    }
  };

} (Drupal));

I then set up a single category "test", and configured my "Disable JavaScripts" settings like this:

test:modules/custom/sven_test_1/js/sven-test-1.js|svenTest1SvenTest1

This resulted in the following script being generated and included in the page source:

      window.euCookieComplianceLoadScripts = function(category) {
        if (category === "test") {
          var scriptTag = document.createElement("script");
          scriptTag.src = "\/modules\/custom\/sven_test_1\/js\/sven-test-1.js";
          document.body.appendChild(scriptTag);
          var EUCookieIntervalsvenTest1SvenTest1 = setInterval(function() {
            if (Drupal.behaviors.svenTest1SvenTest1 !== undefined) {
              Drupal.behaviors.svenTest1SvenTest1.attach(document, drupalSettings);
              clearInterval(EUCookieIntervalsvenTest1SvenTest1)
            }
            ;
          }, 100);
        }
      }

After consenting to the category "test" I was able to see "It works!" in my console, so from my experience, this feature works, also with categories.

Youcanlearnit’s picture

I have the exact same problem, with version 1.24.
If I put in the "disabled javascripts" box for example:
stat_cookies:https://www.googletagmanager.com/gtag/js
It will never load that library, even the user gives consent.

I have selected:
Opt-in with categories.
and a category with machine name: stat_cookies.

svenryen’s picture

Status: Closed (cannot reproduce) » Active

Which module provides that script, @Youcanlearnit?

Youcanlearnit’s picture

phma’s picture

I've come across a similar problem just now and it could be related.

When you operate the drupal site from a sub path, path prefix gets added to the category in the JavaScript snipped that should load the scripts after accepting cookies.

The reason for this is that _eu_cookie_compliance_explode_multiple_lines is processing entries and tries to convert relative paths while not taking into consideration that some of them are prefixed with a category. It seems to work if Drupal is ran from root, but not when it's in a sub path.

So the category needs to be split either before or inside _eu_cookie_compliance_convert_relative_uri. Else the result it produces is pretty useless.

<script>window.euCookieComplianceLoadScripts = function(category) {if (category === "l/analytics") {var scriptTag = document.createElement("script");scriptTag.src = "https:\/\/www.googletagmanager.com\/gtag\/js";document.body.appendChild(scriptTag);}
...
phma’s picture

The latest release writes the JavaScript into a separate file (I hadn't updated yet). This means if I now save the config while Drupal is served from the root path, it mitigates the problem. Still, it could break again if config is imported or if config is accidentally saved and a path prefix gets re-added.

The easiest solution I found is to filter out the path from the category name with basename, but that means categories containing slashes (or backslash on Windows) will not work. In general it would be better to extract the path before processing relative URIs. But that would require more refactoring and testing.

brandonlira’s picture

StatusFileSize
new126.55 KB

Does anyone have any updates on this, I'm also facing this issue even trying to add to DISABLE THE FOLLOWING JAVASCRIPTS WHEN CONSENT ISN'T GIVEN they are still loading as per this example analytics:sites/default/files/google_tag/google_tag.script.js, I've updated for the last version but without success and the available patch was also applied.

Does anyone have any ideas or suggestions?

svenryen’s picture

Status: Active » Needs review
svenryen’s picture

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

I'm not the most experienced GTM/GA user, but I did set up an account at https://tagmanager.google.com/#/home and https://analytics.google.com/analytics/web/.

Best practice nowadays seems to be using the google_tag module, so I installed, enabled and configured that module to load my GTM tag.

Once that's set up and confirmed working, I set up my site to Opt-in users to tracking and listed the two scripts of the google_tag module in "Disable JavaScripts".

The value of the field is:

modules/contrib/google_tag/js/gtag.js
modules/contrib/google_tag/js/gtm.js

I also tried the same setup with a category added to the mix:

analytics:modules/contrib/google_tag/js/gtag.js
analytics:modules/contrib/google_tag/js/gtm.js

Loading that page in a few incognito windows demonstrates that there's no GA traffic recorded until the consent was given, and after it was given, the googletagmanager script loads and records my activity in GA4.

It seems this module doesn't create a sites/default/files/google_tag/google_tag.script.js file. Could you add some information as to what module provides that file? Is it possible to attempt using the google_tag module to achieve your desired outcome, or is there some feature google_tag lacks?

csmdgl’s picture

Version: 8.x-1.19 » 8.x-1.26
StatusFileSize
new481 bytes

Re-roll for 1.26.

atowl’s picture

Hi All,

Could i get an update for this with someone using 1.28? and if it's still a potential issue?

atowl’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Hi,
Since there is no new information, i'm closing this. If its still an issue, please feel free to reopen

Thanks

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.