:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(ingestion): stop server-side $os poisoning $initial_os (#61044)
Anna Garciad1d3f55b0c06
diff --git a/nodejs/src/utils/db/utils.ts b/nodejs/src/utils/db/utils.tsindex 35bc42d6ea58..fe13fb828162 100644--- a/nodejs/src/utils/db/utils.ts+++ b/nodejs/src/utils/db/utils.ts@@ -66,11 +66,19 @@ export function personInitialAndUTMProperties(properties: Properties): Propertielet $set: Record<string, any> | undefinedlet $set_once: Record<string, any> | undefined+ // Server-side SDKs set $is_server: true. Don't lift their host $os/$os_version onto the person+ // (it poisons the sticky $initial_os). Client events omit $is_server (or set it false).+ const skipServerHostOs = properties.$is_server === true+for (const key of eventToPersonProperties) {if (!(key in properties)) {continue}+ if (skipServerHostOs && (key === '$os' || key === '$os_version')) {+ continue+ }+const value = properties[key]if ($set === undefined) {@@ -100,7 +108,7 @@ export function personInitialAndUTMProperties(properties: Properties): Propertie// For the purposes of $initial properties, $os_name is treated as a fallback alias of $os, starting August 2024// It's a special case due to _some_ SDKs using $os_name: https://github.com/PostHog/posthog-js-lite/issues/244const osName = properties.$os_name- if (osName !== undefined) {+ if (osName !== undefined && !skipServerHostOs) {if (!('$os' in properties)) {properties.$os = osName}diff --git a/nodejs/tests/utils/db/utils.test.ts b/nodejs/tests/utils/db/utils.test.tsindex 43ad1f7c3712..9bdadfbdec8b 100644--- a/nodejs/tests/utils/db/utils.test.ts+++ b/nodejs/tests/utils/db/utils.test.ts@@ -118,4 +118,43 @@ describe('personInitialAndUTMProperties()', () => {$set: { $os: 'Windows' },})})+ it('does not lift device context from an $is_server event, but keeps campaign params', () => {+ // A server SDK (is_server: true) stamps its host $os; it must not reach $set/$set_once or+ // it permanently poisons $initial_os. utm is real attribution and still flows through.+ const properties = {+ $is_server: true,+ $os: 'Linux',+ $os_version: '5.15.0',+ utm_source: 'newsletter',+ }+ expect(personInitialAndUTMProperties(properties)).toEqual({+ $is_server: true,+ $os: 'Linux',+ $os_version: '5.15.0',+ utm_source: 'newsletter',+ $set: { utm_source: 'newsletter' },+ $set_once: { $initial_utm_source: 'newsletter' },+ })+ })+ it('does not let a server $os_name alias into $initial_os', () => {+ const result = personInitialAndUTMProperties({ $is_server: true, $os_name: 'Linux' })+ expect((result.$set_once as Record<string, any> | undefined)?.$initial_os).toBeUndefined()+ })+ it.each([+ ['client event maps $os', { $lib: 'web', $os: 'Linux' }, 'Linux'],+ ['client mobile via $os_name', { $lib: 'posthog-ios', $os_name: 'iOS' }, 'iOS'],+ ['no $is_server is treated as client', { $os: 'Linux' }, 'Linux'],+ ['$is_server false is treated as client', { $os: 'Linux', $is_server: false }, 'Linux'],+ ['$is_server true skips $os', { $os: 'Linux', $is_server: true }, undefined],+ ['$is_server true skips regardless of lib', { $lib: 'web', $os: 'Linux', $is_server: true }, undefined],+ ])('maps $os to $initial_os unless $is_server is true: %s', (_desc, properties, expected) => {+ const result = personInitialAndUTMProperties({ ...properties })+ const setOnce = result.$set_once as Record<string, any> | undefined+ const set = result.$set as Record<string, any> | undefined+ expect(setOnce?.$initial_os).toBe(expected)+ if (expected === undefined) {+ // host $os must not reach current $set either, not just $set_once+ expect(set?.$os).toBeUndefined()+ }+ })})