Skip to content

Commit

Permalink
Merge pull request #147 from uktrade/feature/ORPD-93-analytics-cookie…
Browse files Browse the repository at this point in the history
…-format
  • Loading branch information
gdbarnes authored Dec 19, 2024
2 parents 65b4adb + ea19352 commit 1efdd51
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
27 changes: 25 additions & 2 deletions app/core/cookies.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import json
import logging

from django.conf import settings
from django.http import HttpRequest, HttpResponse

logger = logging.getLogger(__name__)


def set_ga_cookie_policy(response: HttpResponse, preference: str) -> None:
"""Set Google Analytics (GA) policy.
Sets the cookies that are required for Google Tag Manager (GTM) to
function correctly.
"""

cookie_object = {
"essential": True,
"settings": True,
"usage": preference == "true",
"campaigns": False,
}

json_string_cookie_object = json.dumps(cookie_object)

response.set_cookie(
key=settings.COOKIE_ACCEPTED_GA_NAME,
value=preference,
value=json_string_cookie_object,
max_age=365 * 24 * 60 * 60,
)
response.set_cookie(
Expand All @@ -26,4 +41,12 @@ def get_ga_cookie_preference(request: HttpRequest) -> str:
Returns value of the GA cookie preference if it exists, otherwise
returns "false".
"""
return request.COOKIES.get(settings.COOKIE_ACCEPTED_GA_NAME, "false")
cookie_object = request.COOKIES.get(settings.COOKIE_ACCEPTED_GA_NAME)
if cookie_object:
try:
cookie_value = json.loads(cookie_object)
return str(cookie_value.get("usage", False)).lower()
except json.JSONDecodeError as e:
logger.error(f"Error parsing GA cookie: {e}")

return "false"
15 changes: 15 additions & 0 deletions app/core/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
import logging

from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
Expand All @@ -9,6 +12,8 @@
from .forms import CookiePreferenceForm
from .healthcheck import application_service_health

logger = logging.getLogger(__name__)


@require_http_methods(["GET"])
def home(request: HttpRequest) -> HttpResponse:
Expand Down Expand Up @@ -118,6 +123,16 @@ def set_cookie_banner_preference(request) -> HttpResponseRedirect:
query parameter. This parameter is used to display a confirmation message
banner.
"""

cookie_object = request.COOKIES.get(settings.COOKIE_ACCEPTED_GA_NAME)
preference = "false"
if cookie_object:
try:
cookie_value = json.loads(cookie_object)
preference = str(cookie_value.get("usage", False)).lower()
except json.JSONDecodeError as e:
logger.error(f"Error parsing GA cookie: {e}")

preference = request.GET.get(settings.COOKIE_ACCEPTED_GA_NAME, "false")
current_page = request.GET.get("current_page")
if not url_has_allowed_host_and_scheme(
Expand Down
2 changes: 1 addition & 1 deletion app/templates/cookie_banner.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2 class="govuk-cookie-banner__heading govuk-heading-m">
<div class="govuk-grid-column-two-thirds">
<div class="govuk-cookie-banner__content">
<p class="govuk-body">You've {% if cookie_preference == 'true' %} accepted {% else %}
rejected {% endif %} additional cookies. You can <a class="govuk-link" href="/cookies">change your cookie
rejected {% endif %} additional cookies. You can <a class="govuk-link govuk-link--no-visited-state" href="/cookies">change your cookie
settings</a> at any time.
</p>
</div>
Expand Down
9 changes: 8 additions & 1 deletion fbr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,14 @@

DATABASES: dict = {"default": {}}

if DATABASE_URL := env("DATABASE_CREDENTIALS", default=None):
if DATABASE_URL := env("DATABASE_URL", default=None):
# Use DATABASE_URL for local development if available in local.env
DATABASES["default"] = dj_database_url.parse(
DATABASE_URL,
engine="postgresql",
)
DATABASES["default"]["ENGINE"] = "django.db.backends.postgresql"
elif DATABASE_URL := env("DATABASE_CREDENTIALS", default=None):
DATABASES["default"] = dj_database_url.config(
default=database_url_from_env("DATABASE_CREDENTIALS")
)
Expand Down

0 comments on commit 1efdd51

Please sign in to comment.