* Open a brand-new session from this event's payload. Called for the first * event of a device AND on boundary (gap > 30min closing the old session).
(payload: IServiceCreateEventPayload)
| 285 | * event of a device AND on boundary (gap > 30min closing the old session). |
| 286 | */ |
| 287 | private newSession(payload: IServiceCreateEventPayload): IClickhouseSession { |
| 288 | const createdAt = toClickhouseDate(payload.createdAt); |
| 289 | // For anonymous traffic the SDK doesn't send a profileId — fall back to |
| 290 | // the deviceId so each anonymous device shows up as a unique visitor in |
| 291 | // the dashboard. Matches what `createEvent` does for the events table. |
| 292 | const profileId = payload.profileId || payload.deviceId; |
| 293 | return { |
| 294 | id: payload.sessionId, |
| 295 | project_id: payload.projectId, |
| 296 | device_id: payload.deviceId, |
| 297 | profile_id: profileId, |
| 298 | is_bounce: true, |
| 299 | created_at: createdAt, |
| 300 | ended_at: createdAt, |
| 301 | event_count: payload.name === 'screen_view' ? 0 : 1, |
| 302 | screen_view_count: payload.name === 'screen_view' ? 1 : 0, |
| 303 | entry_path: payload.path ?? '', |
| 304 | entry_origin: payload.origin ?? '', |
| 305 | exit_path: payload.path ?? '', |
| 306 | exit_origin: payload.origin ?? '', |
| 307 | revenue: payload.name === 'revenue' ? (payload.revenue ?? 0) : 0, |
| 308 | referrer: payload.referrer ?? '', |
| 309 | referrer_name: payload.referrerName ?? '', |
| 310 | referrer_type: payload.referrerType ?? '', |
| 311 | os: payload.os ?? '', |
| 312 | os_version: payload.osVersion ?? '', |
| 313 | browser: payload.browser ?? '', |
| 314 | browser_version: payload.browserVersion ?? '', |
| 315 | device: payload.device ?? '', |
| 316 | brand: payload.brand ?? '', |
| 317 | model: payload.model ?? '', |
| 318 | country: payload.country ?? '', |
| 319 | region: payload.region ?? '', |
| 320 | city: payload.city ?? '', |
| 321 | longitude: payload.longitude ?? null, |
| 322 | latitude: payload.latitude ?? null, |
| 323 | duration: payload.duration ?? 0, |
| 324 | utm_medium: pickUtm(payload, 'utm_medium'), |
| 325 | utm_source: pickUtm(payload, 'utm_source'), |
| 326 | utm_campaign: pickUtm(payload, 'utm_campaign'), |
| 327 | utm_content: pickUtm(payload, 'utm_content'), |
| 328 | utm_term: pickUtm(payload, 'utm_term'), |
| 329 | groups: payload.groups ?? [], |
| 330 | sign: 1, |
| 331 | version: 1, |
| 332 | }; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Atomic write-back: session blob + wallclock ZSET + projects SET + |
no test coverage detected