- Issue created by @goz
- Status changed to Needs review
about 1 year ago 8:34am 20 September 2023 - 🇫🇷France goz
I finally find out how to make it works.
I think it works "as designed", but documentation should be updated.
Event should be defined out of behaviors. Thinking about it, this should never be in behaviors, because it has to be called only once at page load, no need to reattach something with behaviors.
var _paq = _paq || []; (function($, Drupal, drupalSettings, _paq) { Drupal.eu_cookie_compliance('postPreferencesLoad', function(){ if (Drupal.eu_cookie_compliance.hasAgreed('audience')) { _paq.push(['setConsentGiven']); _paq.push(['setCookieConsentGiven']); } }); })(jQuery, Drupal, drupalSettings, _paq);
And we need to call this javascript with defer option so it will be called after eu_cookie_compliance.js script.
custom_module.matomo: js: js/matomo.js: { attributes: { defer: true } } dependencies: - matomo/matomo - eu_cookie_compliance/eu_cookie_compliance
- Status changed to Closed: works as designed
about 1 year ago 11:17am 15 October 2023 - 🇳🇴Norway svenryen
I'm closing this one, as it seems you got it sorted out.
Let me know if you'd like to reopen the issue.
- Status changed to Needs work
about 1 year ago 2:24pm 16 October 2023 - 🇫🇷France goz
I'd like to reopen for documentation improvements.
I'll work on it this week during the Drupal Con Lille.
- 🇫🇷France goz
In Readme, in the "Main file" section, we can read :
Drupal.eu_cookie_compliance = Drupal.eu_cookie_compliance || function () { (Drupal.eu_cookie_compliance.queue = Drupal.eu_cookie_compliance.queue || []) .push(arguments) }; <code> This ensures that a script from another module (which either should not use `defer`, or should be placed AFTER the main script in the HTML) will be able to access the Events from the main script and perform actions. </cite> Except that adding the code of the "Second file" section : <code> var postPreferencesLoadHandler = function(response) { console.log(response); window.cookieResponse = response; }; Drupal.eu_cookie_compliance('postPreferencesLoad', postPreferencesLoadHandler);
Will display the error :
Uncaught TypeError: Drupal.eu_cookie_compliance is not a function
After adding "defer" attribute to script, error no more display and code is called as expected.
But i don't think putting the script in defer is the better solution.Looking at https://git.drupalcode.org/project/eu_cookie_compliance_gtm/-/blob/2.x/j..., i can see Drupal.eu_cookie_compliance is defined before using it in our custom script. Adding same piece of code make it works without having to definer script as defered.
// Sep up the namespace as a function to store list of arguments in a queue. Drupal.eu_cookie_compliance = Drupal.eu_cookie_compliance || function() { (Drupal.eu_cookie_compliance.queue = Drupal.eu_cookie_compliance.queue || []).push(arguments) };
In "Second file" section, i think we should add the following informations :
To use Drupal.eu_cookie_compliance in you custom code, don't forget to instanciate it putting this code in top of your script :
// Sep up the namespace as a function to store list of arguments in a queue. Drupal.eu_cookie_compliance = Drupal.eu_cookie_compliance || function() { (Drupal.eu_cookie_compliance.queue = Drupal.eu_cookie_compliance.queue || []).push(arguments) }; <code> And we can take advantage to add the recommended <code>(function(Drupal) {})(Drupal)
syntax on the example.
Example: reading the cookie preferences after submission + save it
somewhere else for later use
```
(function(Drupal) {
// Sep up the namespace as a function to store list of arguments in a queue.
Drupal.eu_cookie_compliance = Drupal.eu_cookie_compliance || function() {
(Drupal.eu_cookie_compliance.queue = Drupal.eu_cookie_compliance.queue || []).push(arguments)
};var postPreferencesLoadHandler = function(response) {
console.log(response);
window.cookieResponse = response;
};
Drupal.eu_cookie_compliance('postPreferencesLoad', postPreferencesLoadHandler);
})(Drupal);
```Warning: As this type of code only needs to be called once, it shouldn't be necessary to add it as a Drupal.behavior.
- @goz opened merge request.
- Status changed to Needs review
about 1 year ago 3:45pm 16 October 2023 - 🇳🇴Norway svenryen
Thanks. I'll take a look. Are you done with your task, or do you plan to further work on this during sprints at DrupalCon Lille?
- 🇳🇴Norway svenryen
All right. I'll merge this later this week. Thanks for the contribution!
-
svenryen →
committed d2786628 on 8.x-1.x authored by
GoZ →
Issue #3387537 by GoZ, svenryen: Improve documentation about Drupal....
-
svenryen →
committed d2786628 on 8.x-1.x authored by
GoZ →
- Status changed to Fixed
about 1 year ago 12:34pm 22 October 2023 Automatically closed - issue fixed for 2 weeks with no activity.