Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/mirror-raw-user-agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog": patch
---

Django middleware also sends the request user agent as `$raw_user_agent`, the standardized property PostHog's server-side classification (e.g. bot detection) reads
4 changes: 3 additions & 1 deletion posthog/integrations/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,12 @@ def _build_tags(self, request, user_id, user_email):
if ip_address:
tags["$ip"] = ip_address

# Extract user agent
# Extract user agent, mirrored into $raw_user_agent β€” the standardized
# property PostHog's server-side classification (e.g. bot detection) reads
user_agent = request.headers.get("User-Agent")
if user_agent:
tags["$user_agent"] = user_agent
tags["$raw_user_agent"] = user_agent

# Apply extra tags if configured
if self.extra_tags:
Expand Down
3 changes: 3 additions & 0 deletions posthog/test/integrations/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_extract_tags_basic(self):
headers={
"X-POSTHOG-SESSION-ID": "session-123",
"X-POSTHOG-DISTINCT-ID": "user-456",
"User-Agent": "TestAgent/1.0",
},
method="POST",
path="/api/test",
Expand All @@ -103,6 +104,8 @@ def test_extract_tags_basic(self):
self.assertEqual(get_context_distinct_id(), "user-456")
self.assertEqual(tags["$current_url"], "https://example.com/api/test")
self.assertEqual(tags["$request_method"], "POST")
self.assertEqual(tags["$user_agent"], "TestAgent/1.0")
self.assertEqual(tags["$raw_user_agent"], "TestAgent/1.0")

def test_extract_tags_missing_headers(self):
"""Test tag extraction when PostHog headers are missing"""
Expand Down
Loading