mbuzz 0.1.0__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mbuzz/__init__.py +1 -1
- mbuzz/client/track.py +25 -12
- mbuzz/context.py +2 -0
- mbuzz/middleware/flask.py +24 -2
- mbuzz/utils/session_id.py +39 -0
- {mbuzz-0.1.0.dist-info → mbuzz-0.2.0.dist-info}/METADATA +1 -1
- {mbuzz-0.1.0.dist-info → mbuzz-0.2.0.dist-info}/RECORD +8 -7
- {mbuzz-0.1.0.dist-info → mbuzz-0.2.0.dist-info}/WHEEL +0 -0
mbuzz/__init__.py
CHANGED
mbuzz/client/track.py
CHANGED
|
@@ -28,10 +28,12 @@ class TrackOptions:
|
|
|
28
28
|
session_id: Optional[str] = None
|
|
29
29
|
user_id: Optional[str] = None
|
|
30
30
|
properties: Optional[Dict[str, Any]] = None
|
|
31
|
+
ip: Optional[str] = None
|
|
32
|
+
user_agent: Optional[str] = None
|
|
31
33
|
|
|
32
34
|
|
|
33
35
|
def _resolve_ids(options: TrackOptions) -> TrackOptions:
|
|
34
|
-
"""Resolve visitor/session/user IDs from context if not provided."""
|
|
36
|
+
"""Resolve visitor/session/user IDs and ip/user_agent from context if not provided."""
|
|
35
37
|
ctx = get_context()
|
|
36
38
|
if not ctx:
|
|
37
39
|
return options
|
|
@@ -42,6 +44,8 @@ def _resolve_ids(options: TrackOptions) -> TrackOptions:
|
|
|
42
44
|
session_id=options.session_id or ctx.session_id,
|
|
43
45
|
user_id=options.user_id or ctx.user_id,
|
|
44
46
|
properties=options.properties,
|
|
47
|
+
ip=options.ip or ctx.ip,
|
|
48
|
+
user_agent=options.user_agent or ctx.user_agent,
|
|
45
49
|
)
|
|
46
50
|
|
|
47
51
|
|
|
@@ -62,18 +66,21 @@ def _validate(options: TrackOptions) -> bool:
|
|
|
62
66
|
|
|
63
67
|
def _build_payload(options: TrackOptions, properties: Dict[str, Any]) -> Dict[str, Any]:
|
|
64
68
|
"""Build API payload from options."""
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"properties": properties,
|
|
73
|
-
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
74
|
-
}
|
|
75
|
-
]
|
|
69
|
+
event = {
|
|
70
|
+
"event_type": options.event_type,
|
|
71
|
+
"visitor_id": options.visitor_id,
|
|
72
|
+
"session_id": options.session_id,
|
|
73
|
+
"user_id": options.user_id,
|
|
74
|
+
"properties": properties,
|
|
75
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
76
76
|
}
|
|
77
|
+
# Add ip and user_agent only if provided (for server-side session resolution)
|
|
78
|
+
if options.ip:
|
|
79
|
+
event["ip"] = options.ip
|
|
80
|
+
if options.user_agent:
|
|
81
|
+
event["user_agent"] = options.user_agent
|
|
82
|
+
|
|
83
|
+
return {"events": [event]}
|
|
77
84
|
|
|
78
85
|
|
|
79
86
|
def _parse_response(response: Optional[Dict[str, Any]], options: TrackOptions) -> TrackResult:
|
|
@@ -97,6 +104,8 @@ def track(
|
|
|
97
104
|
session_id: Optional[str] = None,
|
|
98
105
|
user_id: Optional[str] = None,
|
|
99
106
|
properties: Optional[Dict[str, Any]] = None,
|
|
107
|
+
ip: Optional[str] = None,
|
|
108
|
+
user_agent: Optional[str] = None,
|
|
100
109
|
) -> TrackResult:
|
|
101
110
|
"""Track an event.
|
|
102
111
|
|
|
@@ -106,6 +115,8 @@ def track(
|
|
|
106
115
|
session_id: Session ID (uses context if not provided)
|
|
107
116
|
user_id: User ID (uses context if not provided)
|
|
108
117
|
properties: Additional event properties
|
|
118
|
+
ip: Client IP address for server-side session resolution
|
|
119
|
+
user_agent: Client user agent for server-side session resolution
|
|
109
120
|
|
|
110
121
|
Returns:
|
|
111
122
|
TrackResult with success status and event details
|
|
@@ -116,6 +127,8 @@ def track(
|
|
|
116
127
|
session_id=session_id,
|
|
117
128
|
user_id=user_id,
|
|
118
129
|
properties=properties,
|
|
130
|
+
ip=ip,
|
|
131
|
+
user_agent=user_agent,
|
|
119
132
|
)
|
|
120
133
|
|
|
121
134
|
options = _resolve_ids(options)
|
mbuzz/context.py
CHANGED
|
@@ -14,6 +14,8 @@ class RequestContext:
|
|
|
14
14
|
user_id: Optional[str] = None
|
|
15
15
|
url: Optional[str] = None
|
|
16
16
|
referrer: Optional[str] = None
|
|
17
|
+
ip: Optional[str] = None
|
|
18
|
+
user_agent: Optional[str] = None
|
|
17
19
|
|
|
18
20
|
def enrich_properties(self, properties: Dict[str, Any]) -> Dict[str, Any]:
|
|
19
21
|
"""Add url and referrer to properties if not already present."""
|
mbuzz/middleware/flask.py
CHANGED
|
@@ -8,6 +8,7 @@ from ..config import config
|
|
|
8
8
|
from ..context import RequestContext, set_context, clear_context
|
|
9
9
|
from ..cookies import VISITOR_COOKIE, SESSION_COOKIE, VISITOR_MAX_AGE, SESSION_MAX_AGE
|
|
10
10
|
from ..utils.identifier import generate_id
|
|
11
|
+
from ..utils.session_id import generate_deterministic, generate_from_fingerprint
|
|
11
12
|
from ..client.session import create_session
|
|
12
13
|
|
|
13
14
|
|
|
@@ -55,8 +56,29 @@ def _get_or_create_visitor_id() -> str:
|
|
|
55
56
|
|
|
56
57
|
|
|
57
58
|
def _get_or_create_session_id() -> str:
|
|
58
|
-
"""Get session ID from cookie or generate
|
|
59
|
-
|
|
59
|
+
"""Get session ID from cookie or generate deterministic one."""
|
|
60
|
+
existing = request.cookies.get(SESSION_COOKIE)
|
|
61
|
+
if existing:
|
|
62
|
+
return existing
|
|
63
|
+
|
|
64
|
+
existing_visitor_id = request.cookies.get(VISITOR_COOKIE)
|
|
65
|
+
if existing_visitor_id:
|
|
66
|
+
return generate_deterministic(existing_visitor_id)
|
|
67
|
+
else:
|
|
68
|
+
return generate_from_fingerprint(_get_client_ip(), _get_user_agent())
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _get_client_ip() -> str:
|
|
72
|
+
"""Get client IP from request headers."""
|
|
73
|
+
forwarded = request.headers.get("X-Forwarded-For", "")
|
|
74
|
+
if forwarded:
|
|
75
|
+
return forwarded.split(",")[0].strip()
|
|
76
|
+
return request.remote_addr or "unknown"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _get_user_agent() -> str:
|
|
80
|
+
"""Get user agent from request."""
|
|
81
|
+
return request.headers.get("User-Agent", "unknown")
|
|
60
82
|
|
|
61
83
|
|
|
62
84
|
def _set_request_context(visitor_id: str, session_id: str) -> None:
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Deterministic session ID generation."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import secrets
|
|
5
|
+
import time
|
|
6
|
+
|
|
7
|
+
SESSION_TIMEOUT_SECONDS = 1800
|
|
8
|
+
SESSION_ID_LENGTH = 64
|
|
9
|
+
FINGERPRINT_LENGTH = 32
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def generate_deterministic(visitor_id: str, timestamp: int | None = None) -> str:
|
|
13
|
+
"""Generate session ID for returning visitors."""
|
|
14
|
+
if timestamp is None:
|
|
15
|
+
timestamp = int(time.time())
|
|
16
|
+
time_bucket = timestamp // SESSION_TIMEOUT_SECONDS
|
|
17
|
+
raw = f"{visitor_id}_{time_bucket}"
|
|
18
|
+
return hashlib.sha256(raw.encode()).hexdigest()[:SESSION_ID_LENGTH]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def generate_from_fingerprint(
|
|
22
|
+
client_ip: str,
|
|
23
|
+
user_agent: str,
|
|
24
|
+
timestamp: int | None = None
|
|
25
|
+
) -> str:
|
|
26
|
+
"""Generate session ID for new visitors using IP+UA fingerprint."""
|
|
27
|
+
if timestamp is None:
|
|
28
|
+
timestamp = int(time.time())
|
|
29
|
+
fingerprint = hashlib.sha256(
|
|
30
|
+
f"{client_ip}|{user_agent}".encode()
|
|
31
|
+
).hexdigest()[:FINGERPRINT_LENGTH]
|
|
32
|
+
time_bucket = timestamp // SESSION_TIMEOUT_SECONDS
|
|
33
|
+
raw = f"{fingerprint}_{time_bucket}"
|
|
34
|
+
return hashlib.sha256(raw.encode()).hexdigest()[:SESSION_ID_LENGTH]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def generate_random() -> str:
|
|
38
|
+
"""Generate random session ID (fallback)."""
|
|
39
|
+
return secrets.token_hex(32)
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
mbuzz/__init__.py,sha256=
|
|
1
|
+
mbuzz/__init__.py,sha256=ip9lSCsNe7T5k1qOfGs4pttoTNGx5Ws-Zf9fHoWXFPg,2247
|
|
2
2
|
mbuzz/api.py,sha256=BITkW6TQEGmPA0OmYFFC611X6JWiY6C1dY-_wsKDT2U,2107
|
|
3
3
|
mbuzz/config.py,sha256=u6v1WnMTgiqyDXHszlXw5hyNENgYRy7-z-IWKuYp0-o,2399
|
|
4
|
-
mbuzz/context.py,sha256=
|
|
4
|
+
mbuzz/context.py,sha256=mM9Hi8GnqTkAVCqB_VKGCXl7a5AFME4WTM-2DCIji_w,1290
|
|
5
5
|
mbuzz/cookies.py,sha256=MCpMljQXaqJ2bhEtTv1aKR2fuqLfGBFofjPRayAQGpw,197
|
|
6
6
|
mbuzz/client/__init__.py,sha256=oNOTpPKdt-b_VH_f848OQTbEEDFhlpbEjZ-WjKvh3JI,36
|
|
7
7
|
mbuzz/client/conversion.py,sha256=Ua3ngFWmH6ekm3ZanNf5up_jV8t5nf788ipM1usvZoM,2346
|
|
8
8
|
mbuzz/client/identify.py,sha256=duyd-OWdBo4w7-7Nv5X-GeG2IpMI-1G_kYwnDXup0FE,859
|
|
9
9
|
mbuzz/client/session.py,sha256=mk8v892bvpeW-VNKFQYDsW442w881yfXBiM4sr9a0SU,835
|
|
10
|
-
mbuzz/client/track.py,sha256
|
|
10
|
+
mbuzz/client/track.py,sha256=HWRYWDWJ_WW_k5IHtkAn4s_MmVwSVFxsxR76KG2h_Ps,4288
|
|
11
11
|
mbuzz/middleware/__init__.py,sha256=gIjwTArToaQNB2NC0iPE_RcmzuHjH7-7jjd7_Dyq4Pw,37
|
|
12
|
-
mbuzz/middleware/flask.py,sha256=
|
|
12
|
+
mbuzz/middleware/flask.py,sha256=s3U77A2986L-KvWUH9MmqSwJwQWzQTYlZPZXH-wzE9o,4182
|
|
13
13
|
mbuzz/utils/__init__.py,sha256=-ejtRN6CTkV3D8uujDtAPDoMT0DAG-ULpWOF-Ae55dY,103
|
|
14
14
|
mbuzz/utils/identifier.py,sha256=iAYmd4he2RTtTNlmszb6KQmD9McZbSMgZZdkMuoGOCE,174
|
|
15
|
-
mbuzz
|
|
16
|
-
mbuzz-0.
|
|
17
|
-
mbuzz-0.
|
|
15
|
+
mbuzz/utils/session_id.py,sha256=3XJkzPg5iUIbRYzz5LThfS8WTAmlmtn60WZTA_TlVhk,1185
|
|
16
|
+
mbuzz-0.2.0.dist-info/METADATA,sha256=3Z8snpq6fv0PmZUvSuAjsH1KaiZIpE9pRLVHTaw8cPE,1849
|
|
17
|
+
mbuzz-0.2.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
18
|
+
mbuzz-0.2.0.dist-info/RECORD,,
|
|
File without changes
|