: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): add native URL parameters to properties transformation (#93940)
Meikel Ratz69cc38a1776e
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 31f303270feb..0af83e11d45f 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@@ -7,7 +7,7 @@ export const posthogAppUrlParametersToEventProperties: LegacyTransformationPlugisetupPlugin: setupPlugin as any,template: {free: true,- status: 'stable',+ status: 'deprecated',type: 'transformation',id: 'plugin-posthog-app-url-parameters-to-event-properties',name: 'URL parameters to event properties',diff --git a/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.test.ts b/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.test.tsnew file mode 100644index 000000000000..4238f92224ea--- /dev/null+++ b/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.test.ts@@ -0,0 +1,98 @@+import { HogFunctionInvocationGlobals } from '../../../types'+import { TemplateTester } from '../../test/test-helpers'+import { template } from './url-parameters-to-properties.template'++describe('url-parameters-to-properties.template', () => {+ const tester = new TemplateTester(template)+ let mockGlobals: HogFunctionInvocationGlobals++ beforeEach(async () => {+ await tester.beforeEach()+ })++ const invoke = async (inputs: Record<string, any>, globals: HogFunctionInvocationGlobals): Promise<any> => {+ const response = await tester.invoke(inputs, globals)+ expect(response.finished).toBe(true)+ expect(response.error).toBeUndefined()+ return response.execResult as any+ }++ it('copies named parameters into event properties and decodes values', async () => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: {+ $current_url: 'https://example.com/?utm_source=google&utm_campaign=spring%20sale&ignore=me',+ },+ },+ })++ const result = await invoke({ parameters: 'utm_source, utm_campaign' }, mockGlobals)++ expect(result.properties.utm_source).toBe('google')+ expect(result.properties.utm_campaign).toBe('spring sale')+ expect(result.properties.ignore).toBeUndefined()+ })++ it('applies prefix and suffix and writes $set and $set_once', async () => {+ mockGlobals = tester.createGlobals({+ event: { properties: { $current_url: 'https://example.com/?ref=abc' } },+ })++ const result = await invoke(+ {+ parameters: 'ref',+ prefix: 'utm_',+ suffix: '_param',+ setAsUserProperties: true,+ setAsInitialUserProperties: true,+ },+ mockGlobals+ )++ expect(result.properties.utm_ref_param).toBe('abc')+ expect(result.properties.$set.utm_ref_param).toBe('abc')+ expect(result.properties.$set_once.initial_utm_ref_param).toBe('abc')+ })++ it('decodes + as a space and matches percent-encoded parameter names', async () => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: { $current_url: 'https://example.com/?utm%5Fsource=spring+sale' },+ },+ })++ const result = await invoke({ parameters: 'utm_source' }, mockGlobals)++ expect(result.properties.utm_source).toBe('spring sale')+ })++ it('stores repeated parameters as a JSON array', async () => {+ mockGlobals = tester.createGlobals({+ event: { properties: { $current_url: 'https://example.com/?tag=a&tag=b' } },+ })++ const result = await invoke({ parameters: 'tag' }, mockGlobals)++ expect(result.properties.tag).toBe('["a","b"]')+ })++ it('ignores query-like text inside a hash fragment', async () => {+ mockGlobals = tester.createGlobals({+ event: { properties: { $current_url: 'https://example.com/#/route?utm_source=fromhash' } },+ })++ const result = await invoke({ parameters: 'utm_source' }, mockGlobals)++ expect(result.properties.utm_source).toBeUndefined()+ })++ it('leaves the event unchanged when the URL has no query string', async () => {+ mockGlobals = tester.createGlobals({+ event: { properties: { $current_url: 'https://example.com/home' } },+ })++ const result = await invoke({ parameters: 'utm_source' }, mockGlobals)++ expect(result.properties).toEqual({ $current_url: 'https://example.com/home' })+ })+})diff --git a/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.ts b/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.tsnew file mode 100644index 000000000000..6ce968180163--- /dev/null+++ b/nodejs/src/cdp/templates/_transformations/url-parameters-to-properties/url-parameters-to-properties.template.ts@@ -0,0 +1,131 @@+import { HogFunctionTemplate } from '~/cdp/types'++export const template: HogFunctionTemplate = {+ free: true,+ status: 'stable',+ type: 'transformation',+ id: 'template-url-parameters-to-properties',+ name: 'URL parameters to event properties',+ description: 'Copies chosen query parameters from $current_url into event properties.',+ icon_url: 'https://res.cloudinary.com/dmukukwp6/image/upload/q_auto,f_auto/builder_hog_01_955c082cad.png',+ category: ['Custom'],+ code_language: 'hog',+ code: `+if (empty(event.properties?.$current_url) or typeof(event.properties.$current_url) != 'string') {+ return event+}++let returnEvent := event+// Strip the fragment first, so a '?' inside a '#...' fragment (as in hash routing) is not read as+// the query string.+let withoutFragment := splitByString('#', event.properties.$current_url, 2)[1]+let parts := splitByString('?', withoutFragment, 2)+if (length(parts) < 2) {+ return event+}++// Query strings encode a space as '+', so decode both names and values the way a browser does,+// and fall back to the raw text when a component is not valid percent-encoding.+fun decodeComponent(s) {+ let replaced := replaceAll(s, '+', ' ')+ return tryDecodeURLComponent(replaced) ?? replaced+}++let queryString := parts[2]+let pairs := splitByString('&', queryString)++for (let raw in splitByString(',', inputs.parameters)) {+ let name := trim(raw)+ if (not empty(name)) {+ let values := []+ for (let pair in pairs) {+ if (not empty(pair)) {+ let kv := splitByString('=', pair, 2)+ let decodedName := decodeComponent(kv[1])+ let matches := inputs.ignoreCase ? (lower(decodedName) == lower(name)) : (decodedName == name)+ if (matches) {+ values := arrayPushBack(values, length(kv) > 1 ? decodeComponent(kv[2]) : '')+ }+ }+ }+ if (length(values) > 0) {+ let key := concat(inputs.prefix, name, inputs.suffix)+ let storeValue := (length(values) == 1 and not inputs.alwaysJson) ? values[1] : jsonStringify(values)+ returnEvent.properties[key] := storeValue+ if (inputs.setAsUserProperties) {+ if (empty(returnEvent.properties.$set)) {+ returnEvent.properties.$set := {}+ }+ returnEvent.properties.$set[key] := storeValue+ }+ if (inputs.setAsInitialUserProperties) {+ if (empty(returnEvent.properties.$set_once)) {+ returnEvent.properties.$set_once := {}+ }+ returnEvent.properties.$set_once[concat('initial_', key)] := storeValue+ }+ }+ }+}++return returnEvent+ `,+ inputs_schema: [+ {+ key: 'parameters',+ type: 'string',+ label: 'Query parameters',+ description: 'Comma-separated query parameter names to copy into event properties.',+ default: '',+ required: true,+ },+ {+ key: 'prefix',+ type: 'string',+ label: 'Property name prefix',+ description: 'Text placed before each parameter name when naming the property.',+ default: '',+ required: false,+ },+ {+ key: 'suffix',+ type: 'string',+ label: 'Property name suffix',+ description: 'Text placed after each parameter name when naming the property.',+ default: '',+ required: false,+ },+ {+ key: 'ignoreCase',+ type: 'boolean',+ label: 'Ignore case',+ description: 'Match parameter names without regard to upper or lower case.',+ default: false,+ required: false,+ },+ {+ key: 'alwaysJson',+ type: 'boolean',+ label: 'Always store as JSON',+ description: 'Store every value as a JSON array, even when a parameter appears once.',+ default: false,+ required: false,+ },+ {+ key: 'setAsUserProperties',+ type: 'boolean',+ label: 'Also set as user properties',+ description: 'Copy each value into $set so it updates the person.',+ default: false,+ required: false,+ },+ {+ key: 'setAsInitialUserProperties',+ type: 'boolean',+ label: 'Also set as initial user properties',+ description: 'Copy each value into $set_once as initial_<name> so it is set only once per person.',+ default: false,+ required: false,+ },+ ],+}diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.tsindex 5ac2debd7c36..57da56d43365 100644--- a/nodejs/src/cdp/templates/index.ts+++ b/nodejs/src/cdp/templates/index.ts@@ -62,6 +62,7 @@ import { template as semverFlattenerTemplate } from './_transformations/semver-fimport { template as timestampParserTemplate } from './_transformations/timestamp-parser/timestamp-parser.template'import { template as urlMaskingTemplate } from './_transformations/url-masking/url-masking.template'import { template as urlNormalizationTemplate } from './_transformations/url-normalization/url-normalization.template'+import { template as urlParametersToPropertiesTemplate } from './_transformations/url-parameters-to-properties/url-parameters-to-properties.template'import { template as logDefaultTemplate } from './_transformations_log/default/default.template'import { template as logDropBySeverityTemplate } from './_transformations_log/drop-by-severity/drop-by-severity.template'import { template as logPiiScrubTemplate } from './_transformations_log/pii-scrub/pii-scrub.template'@@ -128,6 +129,7 @@ export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS: HogFunctionTemplate[] = [semverFlattenerTemplate,timestampParserTemplate,downsamplingTemplate,+ urlParametersToPropertiesTemplate,]export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS_LOG: HogFunctionTemplate[] = [diff --git a/nodejs/tests/fixtures/hog-bytecode/0337372fcb1374c0688c22a9502014833382180fbb8a2297304534d1d3e66935.json b/nodejs/tests/fixtures/hog-bytecode/0337372fcb1374c0688c22a9502014833382180fbb8a2297304534d1d3e66935.jsonnew file mode 100644index 000000000000..49ede01a43f1--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/0337372fcb1374c0688c22a9502014833382180fbb8a2297304534d1d3e66935.json@@ -0,0 +1 @@+["_H",1,32,"utm_source",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/1492f2b5e0853a7f82a2252da1e9bf1a52b8cef68b5bef5b0a2555902b7f37c2.json b/nodejs/tests/fixtures/hog-bytecode/1492f2b5e0853a7f82a2252da1e9bf1a52b8cef68b5bef5b0a2555902b7f37c2.jsonnew file mode 100644index 000000000000..9d1b8ce744ee--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/1492f2b5e0853a7f82a2252da1e9bf1a52b8cef68b5bef5b0a2555902b7f37c2.json@@ -0,0 +1 @@+["_H",1,32,"tag",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/60d4b0c0c64b6daeebce758c9191135e8305d88c6352de95e175f9e776a484bd.json b/nodejs/tests/fixtures/hog-bytecode/60d4b0c0c64b6daeebce758c9191135e8305d88c6352de95e175f9e776a484bd.jsonnew file mode 100644index 000000000000..84c026467176--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/60d4b0c0c64b6daeebce758c9191135e8305d88c6352de95e175f9e776a484bd.json@@ -0,0 +1 @@+["_H",1,32,"utm_source, utm_campaign",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/7145e6f17912da462a3dfa15e70318f9d17f75ae6bfaa95876315fbf903e22b4.json b/nodejs/tests/fixtures/hog-bytecode/7145e6f17912da462a3dfa15e70318f9d17f75ae6bfaa95876315fbf903e22b4.jsonnew file mode 100644index 000000000000..aa32ea7a019c--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/7145e6f17912da462a3dfa15e70318f9d17f75ae6bfaa95876315fbf903e22b4.json@@ -0,0 +1 @@+["_H",1,32,"utm_",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/75d983f6a55f018ee4138e38bc29bafc19433e6f487e2ee9fd491f45568b8d9d.json b/nodejs/tests/fixtures/hog-bytecode/75d983f6a55f018ee4138e38bc29bafc19433e6f487e2ee9fd491f45568b8d9d.jsonnew file mode 100644index 000000000000..89b543edf63e--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/75d983f6a55f018ee4138e38bc29bafc19433e6f487e2ee9fd491f45568b8d9d.json@@ -0,0 +1 @@+["_H",1,32,"properties",32,"event",1,2,32,"$current_url",48,2,"empty",1,32,"string",32,"$current_url",32,"properties",32,"event",1,3,2,"typeof",1,12,4,2,40,5,32,"event",1,1,38,32,"event",1,1,32,"?",32,"$current_url",32,"properties",32,"event",1,3,33,2,2,"splitByString",3,33,2,36,1,2,"length",1,15,40,5,32,"event",1,1,38,52,"decodeComponent",1,0,21,36,0,32,"+",32," ",2,"replaceAll",3,36,1,2,"tryDecodeURLComponent",1,47,3,35,36,1,38,35,53,0,32,"#",36,1,33,2,45,33,2,2,"splitByString",3,33,1,45,32,"&",36,3,2,"splitByString",2,32,",",32,"parameters",32,"inputs",1,2,2,"splitByString",2,36,5,2,"values",1,33,1,36,6,2,"length",1,31,36,8,36,7,16,40,328,36,6,36,7,45,37,9,36,9,2,"trim",1,36,10,2,"empty",1,5,40,298,43,0,36,4,36,12,2,"values",1,33,1,36,13,2,"length",1,31,36,15,36,14,16,40,105,36,13,36,14,45,37,16,36,16,2,"empty",1,5,40,81,32,"=",36,16,33,2,2,"splitByString",3,36,17,33,1,45,36,2,54,1,32,"ignoreCase",32,"inputs",1,2,40,13,36,10,2,"lower",1,36,18,2,"lower",1,11,39,5,36,10,36,18,11,36,19,40,30,36,11,33,1,36,17,2,"length",1,13,40,11,36,17,33,2,45,36,2,54,1,39,2,32,"",2,"arrayPushBack",2,37,11,35,35,35,36,14,33,1,6,37,14,39,-112,35,35,35,35,35,33,0,36,11,2,"length",1,13,40,153,32,"prefix",32,"inputs",1,2,36,10,32,"suffix",32,"inputs",1,2,2,"concat",3,33,1,36,11,2,"length",1,11,32,"alwaysJson",32,"inputs",1,2,5,3,2,40,7,36,11,33,1,45,39,5,36,11,2,"jsonStringify",1,36,0,32,"properties",45,36,12,36,13,46,32,"setAsUserProperties",32,"inputs",1,2,40,36,36,0,32,"properties",45,32,"$set",45,2,"empty",1,40,10,36,0,32,"properties",45,32,"$set",42,0,46,36,0,32,"properties",45,32,"$set",45,36,12,36,13,46,32,"setAsInitialUserProperties",32,"inputs",1,2,40,41,36,0,32,"properties",45,32,"$set_once",45,2,"empty",1,40,10,36,0,32,"properties",45,32,"$set_once",42,0,46,36,0,32,"properties",45,32,"$set_once",45,32,"initial_",36,12,2,"concat",2,36,13,46,35,35,35,35,36,7,33,1,6,37,7,39,-335,35,35,35,35,35,36,0,38,35,35,35,35,35]diff --git a/nodejs/tests/fixtures/hog-bytecode/7dff763b3b0a86cb6e0ae2046d109ffb50fefddc74d48120723667da5c8a4b34.json b/nodejs/tests/fixtures/hog-bytecode/7dff763b3b0a86cb6e0ae2046d109ffb50fefddc74d48120723667da5c8a4b34.jsonnew file mode 100644index 000000000000..5d5120c20a2f--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/7dff763b3b0a86cb6e0ae2046d109ffb50fefddc74d48120723667da5c8a4b34.json@@ -0,0 +1 @@+["_H",1,32,"properties",32,"event",1,2,32,"$current_url",48,2,"empty",1,32,"string",32,"$current_url",32,"properties",32,"event",1,3,2,"typeof",1,12,4,2,40,5,32,"event",1,1,38,32,"event",1,1,32,"#",32,"$current_url",32,"properties",32,"event",1,3,33,2,2,"splitByString",3,33,1,45,32,"?",36,1,33,2,2,"splitByString",3,33,2,36,2,2,"length",1,15,40,5,32,"event",1,1,38,52,"decodeComponent",1,0,21,36,0,32,"+",32," ",2,"replaceAll",3,36,1,2,"tryDecodeURLComponent",1,47,3,35,36,1,38,35,53,0,36,2,33,2,45,32,"&",36,4,2,"splitByString",2,32,",",32,"parameters",32,"inputs",1,2,2,"splitByString",2,36,6,2,"values",1,33,1,36,7,2,"length",1,31,36,9,36,8,16,40,328,36,7,36,8,45,37,10,36,10,2,"trim",1,36,11,2,"empty",1,5,40,298,43,0,36,5,36,13,2,"values",1,33,1,36,14,2,"length",1,31,36,16,36,15,16,40,105,36,14,36,15,45,37,17,36,17,2,"empty",1,5,40,81,32,"=",36,17,33,2,2,"splitByString",3,36,18,33,1,45,36,3,54,1,32,"ignoreCase",32,"inputs",1,2,40,13,36,11,2,"lower",1,36,19,2,"lower",1,11,39,5,36,11,36,19,11,36,20,40,30,36,12,33,1,36,18,2,"length",1,13,40,11,36,18,33,2,45,36,3,54,1,39,2,32,"",2,"arrayPushBack",2,37,12,35,35,35,36,15,33,1,6,37,15,39,-112,35,35,35,35,35,33,0,36,12,2,"length",1,13,40,153,32,"prefix",32,"inputs",1,2,36,11,32,"suffix",32,"inputs",1,2,2,"concat",3,33,1,36,12,2,"length",1,11,32,"alwaysJson",32,"inputs",1,2,5,3,2,40,7,36,12,33,1,45,39,5,36,12,2,"jsonStringify",1,36,0,32,"properties",45,36,13,36,14,46,32,"setAsUserProperties",32,"inputs",1,2,40,36,36,0,32,"properties",45,32,"$set",45,2,"empty",1,40,10,36,0,32,"properties",45,32,"$set",42,0,46,36,0,32,"properties",45,32,"$set",45,36,13,36,14,46,32,"setAsInitialUserProperties",32,"inputs",1,2,40,41,36,0,32,"properties",45,32,"$set_once",45,2,"empty",1,40,10,36,0,32,"properties",45,32,"$set_once",42,0,46,36,0,32,"properties",45,32,"$set_once",45,32,"initial_",36,13,2,"concat",2,36,14,46,35,35,35,35,36,8,33,1,6,37,8,39,-335,35,35,35,35,35,36,0,38,35,35,35,35,35,35]diff --git a/nodejs/tests/fixtures/hog-bytecode/991e4bbd2e2ece4d7364de6f6e23796165d5983709256df1861ba506cb02c64c.json b/nodejs/tests/fixtures/hog-bytecode/991e4bbd2e2ece4d7364de6f6e23796165d5983709256df1861ba506cb02c64c.jsonnew file mode 100644index 000000000000..ccac57be310d--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/991e4bbd2e2ece4d7364de6f6e23796165d5983709256df1861ba506cb02c64c.json@@ -0,0 +1 @@+["_H",1,32,"ref",38]diff --git a/nodejs/tests/fixtures/hog-bytecode/b5809e2564ca2754c4715ed4e97fd26b78095b3827d8387180e691c60908667f.json b/nodejs/tests/fixtures/hog-bytecode/b5809e2564ca2754c4715ed4e97fd26b78095b3827d8387180e691c60908667f.jsonnew file mode 100644index 000000000000..3a9b9bec9f17--- /dev/null+++ b/nodejs/tests/fixtures/hog-bytecode/b5809e2564ca2754c4715ed4e97fd26b78095b3827d8387180e691c60908667f.json@@ -0,0 +1 @@+["_H",1,32,"_param",38]