Closed Ciantic closed 2 months ago
Quick input: This could be because
1) we need to know if the user changes the consent in any given moment. 2) dude.fi is using swup.js for page transtions
But I might be wrong about this. Need to check in some point.
It happened on our site too (not using swup.js), that's why I opened the issue. I just didn't want to post that site URL so I found out it happens on dude.fi which was better showcase.
Cookie Consent's onAccept is triggered on each page load.
I managed to fix this like this, add a new callback onFirstAction
:
airCookieSettings.onFirstAction = function() {
airCookierecordConsent();
}
Then commenting out airCookierecordConsent()
from onAccept like this:
<?php // Allow adding categiry specific javascript to be runned when the category is accepted.
if ( ! empty( $cookie_categories ) && is_array( $cookie_categories ) ) : ?>
airCookieSettings.onAccept = function() {
// airCookierecordConsent(); // THIS LINE IS THE PROBLEM, BECAUSE CONSENT calls onAccept on each request
<?php foreach ( $cookie_categories as $cookie_category ) {
echo do_category_js( $cookie_category ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} ?>
}
I think they have changed many things in 3.0.0, because there is now onConsent
callback.
Hmm, I see. We should upgrade to 3 in some point (#3). In the meantime you could send a PR about your fix?
I was about to send PR, but I don't think it's useful anymore because there is now this excellent PR for upgrading directly 3.0 that also fixes this: https://github.com/digitoimistodude/air-cookie/pull/4
Reproduce:
I see POST https://www.dude.fi/wp-json/air-cookie/v1/consent on each request:
https://github.com/digitoimistodude/air-cookie/assets/64731/978e9680-4ff4-4cbd-968b-bd6597f8d6c8