Skip to content

: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.

fix(ingestion): don't write null first-touch values into $set_once (#74742)

Christiaan Hendriksen094fc03aed25
diff --git a/nodejs/src/common/utils/db/utils.ts b/nodejs/src/common/utils/db/utils.ts
index 0a93689765c1..eccec2b8895f 100644
--- a/nodejs/src/common/utils/db/utils.ts
+++ b/nodejs/src/common/utils/db/utils.ts
@@ -95,7 +95,11 @@ export function personInitialAndUTMProperties(properties: Properties): Propertie
// Use pre-computed initial key instead of string manipulation
const initialKey = INITIAL_KEY_MAP.get(key)!
- if (!(initialKey in $set_once!)) {
+ // Never write a null into $set_once: browser SDKs send every absent campaign param as an
+ // explicit null, and $set_once only applies while the person property is missing, so
+ // `$initial_gclid: null` would permanently block the real first-touch value. $set keeps
+ // its nulls because clearing the latest-touch value on a param-less visit is intentional.
+ if (value != null && !(initialKey in $set_once!)) {
$set_once![initialKey] = value
}
}
@@ -115,7 +119,8 @@ export function personInitialAndUTMProperties(properties: Properties): Propertie
if (!('$os' in $set)) {
$set.$os = osName
}
- if (!('$initial_os' in $set_once!)) {
+ // Same null rule as above: a null $os_name must not claim $initial_os for good.
+ if (osName !== null && !('$initial_os' in $set_once!)) {
$set_once!.$initial_os = osName
}
// $os_name is normalized to $os, so remove it from person properties
diff --git a/nodejs/tests/utils/db/utils.test.ts b/nodejs/tests/utils/db/utils.test.ts
index f0acbbd7cc37..cc8630ddd937 100644
--- a/nodejs/tests/utils/db/utils.test.ts
+++ b/nodejs/tests/utils/db/utils.test.ts
@@ -140,6 +140,29 @@ describe('personInitialAndUTMProperties()', () => {
const result = personInitialAndUTMProperties({ $is_server: true, $os_name: 'Linux' })
expect((result.$set_once as Record<string, any> | undefined)?.$initial_os).toBeUndefined()
})
+ // Browser SDKs send every absent campaign param as an explicit null whenever at least one is
+ // present. $set_once only applies while the person property is missing, so a null there would
+ // permanently block the real first-touch value. $set intentionally keeps the nulls (last-touch).
+ it.each([
+ [
+ 'null campaign params stay in $set but never claim $set_once',
+ { utm_source: 'google', gclid: null },
+ { utm_source: 'google', gclid: null },
+ { $initial_utm_source: 'google' },
+ ],
+ [
+ 'all-null campaign params leave $set_once empty',
+ { gclid: null, msclkid: null },
+ { gclid: null, msclkid: null },
+ {},
+ ],
+ ['a null $os_name does not claim $initial_os', { $os_name: null }, { $os: null }, {}],
+ ])('never writes null first-touch values: %s', (_desc, input, expectedSet, expectedSetOnce) => {
+ const result = personInitialAndUTMProperties({ ...input })
+ expect(result.$set).toEqual(expectedSet)
+ expect(result.$set_once).toEqual(expectedSetOnce)
+ })
+
it.each([
['client event maps $os', { $lib: 'web', $os: 'Linux' }, 'Linux'],
['client mobile via $os_name', { $lib: 'posthog-ios', $os_name: 'iOS' }, 'iOS'],