: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
feat(cdp): allow custom property names in URL parameters transformation (#63196)
Christiaan Hendriksen9917ad67fa44
diff --git a/frontend/src/lib/components/CyclotronJob/CyclotronJobInputs.tsx b/frontend/src/lib/components/CyclotronJob/CyclotronJobInputs.tsxindex c0d1de573845..fafc81cd387b 100644--- a/frontend/src/lib/components/CyclotronJob/CyclotronJobInputs.tsx+++ b/frontend/src/lib/components/CyclotronJob/CyclotronJobInputs.tsx@@ -262,6 +262,7 @@ function CyclotronJobTemplateInput(props: {onChange?: (value: CyclotronJobInputType) => voidinput: CyclotronJobInputTypesampleGlobalsWithInputs: CyclotronJobInvocationGlobalsWithInputs | null+ placeholder?: string}): JSX.Element {const templating = props.input.templating ?? 'hog'@@ -269,8 +270,10 @@ function CyclotronJobTemplateInput(props: {return (<LemonInputtype="text"+ className={props.className}value={props.input.value}onChange={(val) => props.onChange?.({ ...props.input, value: val })}+ placeholder={props.placeholder}/>)}@@ -337,7 +340,7 @@ function DictionaryField({return (<div className="deprecated-space-y-2">- {!entries.some(([key]) => key === EXTEND_OBJECT_KEY) ? (+ {templating && !entries.some(([key]) => key === EXTEND_OBJECT_KEY) ? (<LemonButton icon={<IconPlus />} size="small" type="secondary" onClick={handleEnableIncludeObject}>Include properties from an entire object</LemonButton>@@ -362,6 +365,7 @@ function DictionaryField({<CyclotronJobTemplateInputclassName="overflow-hidden flex-2"+ placeholder="Value"input={{ ...input, value: val }}onChange={(val) => {if (val.templating) {diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.test.ts b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.test.tsindex 9ff3037dfeb9..7ae3caa13225 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.test.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.test.ts@@ -37,18 +37,9 @@ const pluginJSON = require('./plugin.json')function buildMockMeta(partialConfig: Partial<PluginConfig> = {}): LocalMeta {const config: PluginConfig = { ...defaultConfig, ...partialConfig }- return {- global: {- ignoreCase: config.ignoreCase === 'true',- setAsInitialUserProperties: config.setAsInitialUserProperties === 'true',- setAsUserProperties: config.setAsUserProperties === 'true',- alwaysJson: config.alwaysJson === 'true',- parameters: new Set(- config.parameters ? config.parameters.split(',').map((parameter) => parameter.trim()) : null- ),- },- config: config,- } as LocalMeta+ const meta = { global: {}, config: config } as LocalMeta+ setupPlugin(meta)+ return meta}describe('ParamsToPropertiesPlugin', () => {@@ -131,6 +122,26 @@ describe('ParamsToPropertiesPlugin', () => {expect(meta.global.parameters.size).toBe(0)})++ it('should add customNames keys to the parameters set', () => {+ const config: PluginConfig = {+ ...defaultConfig,+ parameters: 'one_item',+ customNames: { mapped_item: 'custom_name' },+ }+ const meta = {+ global: {+ parameters: new Set(),+ },+ config,+ } as LocalMeta++ setupPlugin(meta)++ expect(meta.global.parameters.size).toBe(2)+ expect(meta.global.parameters.has('one_item')).toBeTruthy()+ expect(meta.global.parameters.has('mapped_item')).toBeTruthy()+ })})describe('plugin.json', () => {@@ -149,7 +160,8 @@ describe('ParamsToPropertiesPlugin', () => {expect(fields.has('suffix')).toBeTruthy()expect(fields.has('parameters')).toBeTruthy()expect(fields.has('alwaysJson')).toBeTruthy()- expect(fields.size).toEqual(7)+ expect(fields.has('customNames')).toBeTruthy()+ expect(fields.size).toEqual(8)}})@@ -168,6 +180,7 @@ describe('ParamsToPropertiesPlugin', () => {expect(fields.get('suffix')).toEqual('string')expect(fields.get('parameters')).toEqual('string')expect(fields.get('alwaysJson')).toEqual('choice')+ expect(fields.get('customNames')).toEqual('dictionary')}})})@@ -381,6 +394,106 @@ describe('ParamsToPropertiesPlugin', () => {}})+ it.each<{+ case: string+ url: string+ config: Partial<PluginConfig>+ expectedKey: string+ expectedValue: string+ absentKey: string+ }>([+ {+ case: 'a parameter listed in parameters',+ url: 'https://posthog.com/test?plugin=1&myUrlParameter=1',+ config: { customNames: { myUrlParameter: 'custom_name' } },+ expectedKey: 'custom_name',+ expectedValue: '1',+ absentKey: 'myUrlParameter',+ },+ {+ case: 'a parameter listed only in customNames',+ url: 'https://posthog.com/test?plugin=1&fid=42',+ config: { customNames: { fid: 'follower_id' } },+ expectedKey: 'follower_id',+ expectedValue: '42',+ absentKey: 'fid',+ },+ {+ case: 'a parameter with different case when ignoreCase is true',+ url: 'https://posthog.com/test?plugin=1&MyUrlParameter=1',+ config: { ignoreCase: 'true', customNames: { myUrlParameter: 'custom_name' } },+ expectedKey: 'custom_name',+ expectedValue: '1',+ absentKey: 'MyUrlParameter',+ },+ ])('should store $case under its custom name', ({ url, config, expectedKey, expectedValue, absentKey }) => {+ const processedEvent = processEvent(buildPageViewEvent(url), buildMockMeta(config))++ if (processedEvent.properties) {+ expect(processedEvent.properties[expectedKey]).toEqual(expectedValue)+ expect(processedEvent.properties[absentKey]).not.toBeDefined()+ } else {+ expect(processedEvent.properties).toBeDefined()+ }+ })++ it("shouldn't apply prefix and suffix to custom names", () => {+ const sourceEvent = buildPageViewEvent('https://posthog.com/test?plugin=1&myUrlParameter=1&fid=42')++ const processedEvent = processEvent(+ sourceEvent,+ buildMockMeta({+ parameters: 'myUrlParameter, fid',+ prefix: 'prefix_',+ suffix: '_suffix',+ customNames: { fid: 'follower_id' },+ })+ )++ if (processedEvent.properties) {+ expect(processedEvent.properties['follower_id']).toEqual('42')+ expect(processedEvent.properties['prefix_myUrlParameter_suffix']).toEqual('1')+ } else {+ expect(processedEvent.properties).toBeDefined()+ }+ })++ it('should fall back to prefix and suffix when the custom name is empty', () => {+ const sourceEvent = buildPageViewEvent('https://posthog.com/test?plugin=1&myUrlParameter=1')++ const processedEvent = processEvent(+ sourceEvent,+ buildMockMeta({ prefix: 'prefix_', suffix: '_suffix', customNames: { myUrlParameter: '' } })+ )++ if (processedEvent.properties) {+ expect(processedEvent.properties['prefix_myUrlParameter_suffix']).toEqual('1')+ } else {+ expect(processedEvent.properties).toBeDefined()+ }+ })++ it('should use the custom name for $set and $set_once properties', () => {+ const sourceEvent = buildPageViewEvent('https://posthog.com/test?plugin=1&myUrlParameter=1')++ const processedEvent = processEvent(+ sourceEvent,+ buildMockMeta({+ setAsUserProperties: 'true',+ setAsInitialUserProperties: 'true',+ customNames: { myUrlParameter: 'custom_name' },+ })+ )++ if (processedEvent.properties) {+ expect(processedEvent.properties['custom_name']).toEqual('1')+ expect(processedEvent.properties.$set['custom_name']).toEqual('1')+ expect(processedEvent.properties.$set_once['initial_custom_name']).toEqual('1')+ } else {+ expect(processedEvent.properties).toBeDefined()+ }+ })+it("shouldn't add properties when $current_url is undefined", () => {const sourceEvent = {...buildPageViewEvent('https://posthog.com/test?plugin=1&myUrlParameter=1'),diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.ts b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.tsindex 87088d684bae..ad84bdf3fa1c 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/index.ts@@ -12,6 +12,7 @@ export type PluginConfig = {suffix: stringparameters: stringalwaysJson: 'true' | 'false'+ customNames?: Record<string, string>}export type LocalMeta = LegacyTransformationPluginMeta & {@@ -39,6 +40,11 @@ export const setupPlugin = (meta: LocalMeta): void => {global.parameters = new Set(config.parameters ? config.parameters.split(',').map((parameter) => parameter.trim()) : null)+ for (const parameter of Object.keys(config.customNames ?? {})) {+ if (parameter.trim()) {+ global.parameters.add(parameter.trim())+ }+ }}export const processEvent = (event: PluginEvent, meta: LocalMeta): PluginEvent => {@@ -62,7 +68,8 @@ export const processEvent = (event: PluginEvent, meta: LocalMeta): PluginEvent =}if (value.length > 0) {- const key = `${meta.config.prefix}${name}${meta.config.suffix}`+ const customName = meta.config.customNames?.[name]+ const key = customName || `${meta.config.prefix}${name}${meta.config.suffix}`// if we've only got one, then just store the first string as a string// unless we want them all JSON-ifieddiff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/plugin.json b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/plugin.jsonindex 6e84ab12d58a..31ac06a0a4f4 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/plugin.json+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/plugin.json@@ -11,19 +11,26 @@"default": "","hint": "Comma separated list of URL query parameters to capture. Leaving this blank will capture nothing."},+ {+ "key": "customNames",+ "name": "Custom property names",+ "type": "dictionary",+ "default": {},+ "hint": "Capture URL parameters under custom property names. The key is the URL parameter and the value is the property name to store it as, e.g. key 'fid' with value 'follower_id' stores ?fid=123 as the property follower_id. Parameters listed here are captured even if they are not in 'URL query parameters to convert', and the prefix and suffix are not applied to them."+ },{"key": "prefix","name": "Prefix","type": "string","default": "",- "hint": "Add a prefix to the property name e.g. set it to 'prefix_' to get followerId -> prefix_followerId"+ "hint": "Add a prefix to the property name e.g. set it to 'prefix_' to get followerId -> prefix_followerId. Not applied to parameters with a custom property name."},{"key": "suffix","name": "Suffix","type": "string","default": "",- "hint": "Add a suffix to the property name e.g. set it to '_suffix' to get followerId -> followerId_suffix"+ "hint": "Add a suffix to the property name e.g. set it to '_suffix' to get followerId -> followerId_suffix. Not applied to parameters with a custom property name."},{"key": "ignoreCase",diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/template.ts b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/template.tsindex 4db9f979cc32..31f303270feb 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/template.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-app-url-parameters-to-event-properties/template.ts@@ -27,6 +27,15 @@ export const posthogAppUrlParametersToEventProperties: LegacyTransformationPlugidescription:'Comma separated list of URL query parameters to capture. Leaving this blank will capture nothing.',},+ {+ key: 'customNames',+ templating: false,+ label: 'Custom property names',+ type: 'dictionary',+ default: {},+ description:+ "Capture URL parameters under custom property names. The key is the URL parameter and the value is the property name to store it as, e.g. key 'fid' with value 'follower_id' stores ?fid=123 as the property follower_id. Parameters listed here are captured even if they are not in 'URL query parameters to convert', and the prefix and suffix are not applied to them.",+ },{key: 'prefix',templating: false,@@ -34,7 +43,7 @@ export const posthogAppUrlParametersToEventProperties: LegacyTransformationPlugitype: 'string',default: '',description:- "Add a prefix to the property name e.g. set it to 'prefix_' to get followerId -> prefix_followerId",+ "Add a prefix to the property name e.g. set it to 'prefix_' to get followerId -> prefix_followerId. Not applied to parameters with a custom property name.",},{key: 'suffix',@@ -43,7 +52,7 @@ export const posthogAppUrlParametersToEventProperties: LegacyTransformationPlugitype: 'string',default: '',description:- "Add a suffix to the property name e.g. set it to '_suffix' to get followerId -> followerId_suffix",+ "Add a suffix to the property name e.g. set it to '_suffix' to get followerId -> followerId_suffix. Not applied to parameters with a custom property name.",},{key: 'ignoreCase',