django-cookie-consent icon indicating copy to clipboard operation
django-cookie-consent copied to clipboard

Refresh page after clicking accept cookie bar

Open some1ataplace opened this issue 2 years ago • 1 comments

Allow users to click the accept button on the cookie bar and automatically refresh the page and load in the cookies that they did not have before on that page.

Use case:

  • Blog website that uses Google Adsense & Google Analytics cookies
  • User reads through half the blog and sees something like a cookie wall which cuts off blog text unless you accept cookies
  • User accepts cookies from the cookie bar
  • The page automatically refreshes and the user could now click ads, get analytics tracked, and read the rest of the blog page

The user does not have to remember to refresh the page themselves to load in the accepted cookies.

Maybe this should probably be a django-cookie-consent optional setting to enable/disable page refreshes after accepting the cookie bar.

We can use window.location.reload(); inside of cookiebar.js after body.classList.remove('with-cookie-bar');

But for a custom django setting, I am thinking we may need to put a django template if statement to see if the setting is True/False. Then if True, run something like this when Accept is clicked:

const elems = document.querySelectorAll('.cc-cookie-accept');

elems.forEach( el => el .addEventListener('click', fn, false) );

function fn(e) {
    e.preventDefault();
    window.location.reload();
}

Decode / unescape unicode:

<div class="cookie-bar">This site uses Social media, Optional (test) cookies cookies for better performance and user experience. Do you agree to use cookies? <a href="/cookies/accept/social,optional/" class="cc-cookie-accept">Accept</a>
  <a href="/cookies/decline/social,optional/" class="cc-cookie-decline">Decline</a>
  <a href="/cookies/">Cookies info</a>
</div>

https://www.online-toolz.com/tools/text-unicode-entities-convertor.php

I am not sure if we were to include this if it would cause rate limiting/DDoS issues if abused. Example - use selenium to accept cookies, refresh, delete cookies, repeat.

I am also not sure if doing this would effect https://support.google.com/adsense/answer/1346295?hl=en#Auto-refreshing_ads. I don't think it would because the ads are not even present on the site until the user accepts cookies and refreshes. Therefore, we are not refreshing the ads like that link says since we are not doing any programmatic refreshing when cookies are already accepted and the ads are already shown on the page.

some1ataplace avatar Jun 21 '22 15:06 some1ataplace

Figured out a way to do it with a custom django setting:

test_page.html:

    <script>const refresh_setting = "{{request|cookie_consent_refresh_enabled}}";</script>
    <script type="text/javascript" src={% static "cookie_consent/cookiebar.js" %}></script>

Cookiebar.js:

      body.classList.remove('with-cookie-bar');
      if (refresh_setting === "True") {
        window.location.reload();
      }

Conf.py:

REFRESH = True

Util.py:

def is_cookie_consent_refresh_enabled(request):
    """
    Returns if django-cookie-consent is refresh setting is enabled for given request.
    """
    enabled = settings.COOKIE_CONSENT_REFRESH
    if callable(enabled):
        return enabled(request)
    else:
        return enabled

cookie_consent_tags.py:

from cookie_consent.util import (
    is_cookie_consent_refresh_enabled
)
@register.filter
def cookie_consent_refresh_enabled(request):
    """
    Filter returns if cookie consent refresh is enabled for this request.
    """
    return is_cookie_consent_refresh_enabled(request)

some1ataplace avatar Jun 28 '22 17:06 some1ataplace

The whole point of using the Javascript bar to me seems that you don't need to do full-page refreshes, which is a horrible user experience and unexpected.

Image that a user starts filling out some data on a page, clicks the reject/accept buttons and then the page refreshes, losing all their work. This is not acceptable.

sergei-maertens avatar Sep 24 '22 16:09 sergei-maertens

@sergei-maertens - One way I was able to work around that problem so that you could use the refresh feature was that I hid the entire page form functionality before cookies were even accepted (it is a blank page with a warning saying cookies must be accepted to use the form in the first place). That way, they can click on accept cookies on the cookie bar and they will instantly be brought to the brand new form page after automatic refresh to enter data for the first time. They do not have to click on accept on the cookie bar then manually refresh themselves.

some1ataplace avatar Sep 28 '22 13:09 some1ataplace

... that I hid the entire page form functionality before cookies were even accepted ...

I'm glad that this works for you, but it is definitely not an acceptable solution in our projects :smile:

Since cookies are usually opt-in, there is no reason that a user should not be able to do their thing while leaving the banner in place. If they then halfway decide to accept/decline the cookies to get rid of the banner, that should not interfere with their earlier work.

sergei-maertens avatar Sep 24 '23 13:09 sergei-maertens