I think it would be useful execute JavaScript code when a visitor gives consent for the site to use cookies. Example use can be to trigger analytics code to record the initial page visit. Without having to create a custom module.

Attached two patches, one with this issue only and one with these two #2082303: Add support for Auto-confirmation and Auto-hide agreed, #2082387: Remove duplicate code in js file and necessary modifications would need.

Regards

Comments

rroblik’s picture

Issue summary: View changes

Hum ... first step may be more simple and lightweight :)
Why to not simply trigger a custom jQuery at the end of Drupal.eu_cookie_compliance.setStatus function ?

Could look like

Drupal.eu_cookie_compliance.setStatus = function(status) {
    ...
    document.cookie = cookie;
    $(document).trigger('hasAgreed.eu_cookie_compliance');
}

And in template javascript (or custom module) we simply add listener to this event :

$(document).one("hasAgreed.eu_cookie_compliance", function() {
  console.info('User has agreed cookies');
  // Re-enable google analytics
  window['ga-disable-UA-XXXXX'] = false;
  ga("send", "pageview");.
});

Working nice for me. What do you think about it ?

Note :
I saw during test that Drupal.eu_cookie_compliance.setStatus is called twice when user click on agree button when "thanks popup" is disabled in config.
I think it's a bug isn't it ?

pbuyle’s picture

Storing JavaScript code in database and executing it via eval() is dangerous and error prone. facine's solution encourage bad and dangerous practices, rroblik's solution is far better and cleaner. But I would rather have a single eu_cookie_compliance with the set status as an extra argument. Something like $(document).trigger('eu_cookie_compliance', status) and in custom JavaScript $(document).on('eu_cookie_compliance', function(event, status) { ... }).

rroblik’s picture

Status: Needs review » Needs work

Even better !

Could you write a patch ?

(eval is not my way !)

gmario’s picture

Hi,

I have write a patch. in attachment.

gmario

svenryen’s picture

Hi @gmario, thanks for your patch on such an old issue.

I reversed the two parts of your event/action name, so that it reads 'eu_cookie_compliance.changeStatus', which to me just sounds better (don't really know why). I also passed the current status which I guess will be useful to know for the function that executes when triggered.

Can you do me the favor of reviewing this patch and give thumbs up if it matches your requirements, and doesn't introduce any bugs, then we can commit this to the dev branch.

svenryen’s picture

Status: Needs work » Needs review
svenryen’s picture

Status: Needs review » Fixed

This was added in commit 430e3009432c77c436efe3a329725b110282a909 on 19 Feb (not referencing this issue).

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.