Skip to content

Commit a03762d

Browse files
authored
Add IP address and user_agent to analytics events (#58064)
1 parent e8be837 commit a03762d

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/events/components/events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function sendEvent<T extends EventType>({
122122
screen_width: window.screen.width,
123123
screen_height: window.screen.height,
124124
pixel_ratio: window.devicePixelRatio || 1,
125+
user_agent: navigator.userAgent,
125126

126127
// Location information
127128
timezone: new Date().getTimezoneOffset() / -60,

src/events/lib/schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ const context = {
168168
description: 'The device pixel ratio.',
169169
minimum: 0,
170170
},
171+
ip: {
172+
type: 'string',
173+
description: 'The IP address of the user.',
174+
},
175+
user_agent: {
176+
type: 'string',
177+
description: 'The raw user agent string from the browser.',
178+
},
171179

172180
// Location information
173181
timezone: {

src/events/middleware.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ router.post(
7878
// JSON.stringify removes `undefined` values but not `null`, and we don't want to send `null` to Hydro
7979
body.context.dotcom_user = req.cookies?.dotcom_user ? req.cookies.dotcom_user : undefined
8080
body.context.is_staff = Boolean(req.cookies?.staffonly)
81+
// Add IP address and user agent from request
82+
// Moda forwards the client's IP using the `fastly-client-ip` header
83+
body.context.ip = req.headers['fastly-client-ip'] as string | undefined
84+
body.context.user_agent ??= req.headers['user-agent']
8185
}
8286
const validate = validators[type]
8387
if (!validate(body)) {

src/events/tests/middleware.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ describe('POST /events', () => {
5252
// Location information
5353
timezone: -7,
5454
user_language: 'en-US',
55+
ip: '192.0.2.1',
56+
user_agent:
57+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
5558
},
5659
}
5760

@@ -88,6 +91,9 @@ describe('POST /events', () => {
8891
// Location information
8992
timezone: -7,
9093
user_language: 'en-US',
94+
ip: '192.0.2.1',
95+
user_agent:
96+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
9197
},
9298
}
9399

src/events/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export type EventProps = {
5050
screen_width?: number
5151
screen_height?: number
5252
pixel_ratio?: number
53+
ip?: string
54+
user_agent?: string
5355
timezone: number
5456
user_language: string
5557
os_preference: string

0 commit comments

Comments
 (0)