consentGiven / consentDenied callbacks are (perhaps) triggered too often

Created on 1 July 2022, over 2 years ago
Updated 21 June 2023, over 1 year ago

Problem/Motivation

I'll have to investigate this further, but the consentGiven / consentDenied callbacks are triggered too often, I think. Reloading a page, a single one (e.g. cookies_ga) is triggered 4 times in this code:

 attach(context) {
      const self = this;
      alert("Register!");
      document.addEventListener("cookiesjsrUserConsent", function (event) {
        alert("Event called!");
        const service =
          typeof event.detail.services === "object"
            ? event.detail.services
            : {};
        if (typeof service.analytics !== "undefined" && service.analytics) {
          alert("Activate!");
          self.activate(context);
        }
      });
    },

leads to:

  1. Register!
  2. Event called!
  3. Activate!
  4. Event called!
  5. Activate!
  6. Event called!
  7. Activate!
  8. Event called!
  9. Activate!

As the registration only happens once, the event seems to be called too often. I think that MIGHT be due to AJAX and context. Perhaps the registration needs a once() ... or there's a different reason, I don't know yet.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇴Colombia jedihe

    In cookies_gtag.js, I'm seeing the event listener registering twice on page load; then 2 more registrations occur after each AJAX update:

    Right After Page Load:

    Right After an AJAX Refresh:

    I think it is totally possible to properly handle the various scenarios mentioned by @JFeltkamp without having this bad side-effect. Proper usage of core/once should suffice for this, maybe accompanying it with an early (outside of the behavior attach method) registration of the listener.

  • Issue was unassigned.
  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    @jedihe thanks for the investigations, I totally agree we should try to fix this. I'm not even sure, if "once" is needed or this can be done simpler. The reason behind is simply, that attach() is called on each ajax request, with the given context to load.

    While the context is == document on initial load, it's the AJAX content added on subsequent loads.

    I don't have the time to implement or check this deeper currently, could you prepare a MR? Then I'll be happy to review.

Production build 0.71.5 2024