generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtm.py
39 lines (31 loc) · 1.16 KB
/
gtm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from flask import session, request, after_this_request
from app.main import bp
from datetime import datetime, timedelta, UTC
import uuid
import re
def get_gtm_anon_id():
gtm_anon_id = session.get("gtm_anon_id", "")
return {"gtm_anon_id": gtm_anon_id}
def get_gtm_anon_id_from_cookie():
uuid_pattern = re.compile(r"^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$")
anon_id_cookie = request.cookies.get("gtm_anon_id", default="")
if not uuid_pattern.match(anon_id_cookie):
return None
return anon_id_cookie
@bp.before_app_request
def detect_gtm_anon_id():
# gtm_anon_id is used to track user anonymously across services for Google Tag Manager.
if "gtm_anon_id" in session:
return
anon_id_cookie = get_gtm_anon_id_from_cookie()
if anon_id_cookie:
session["gtm_anon_id"] = anon_id_cookie
return
@after_this_request
def remember_gtm_anon_id(response):
session["gtm_anon_id"] = str(uuid.uuid4())
expiration_date = datetime.now(UTC) + timedelta(days=30)
response.set_cookie(
"gtm_anon_id", session.get("gtm_anon_id"), expires=expiration_date
)
return response