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.

feat(cdp): add native SemVer Flattener transformation (#93937)

Meikel Ratza3c39636177a
diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/semver-flattener-plugin/template.ts b/nodejs/src/cdp/legacy-plugins/_transformations/semver-flattener-plugin/template.ts
index 832892d9b1f6..32123befac99 100644
--- a/nodejs/src/cdp/legacy-plugins/_transformations/semver-flattener-plugin/template.ts
+++ b/nodejs/src/cdp/legacy-plugins/_transformations/semver-flattener-plugin/template.ts
@@ -6,7 +6,7 @@ export const semverFlattenerPlugin: LegacyTransformationPlugin = {
processEvent,
template: {
free: true,
- status: 'stable',
+ status: 'deprecated',
type: 'transformation',
id: 'plugin-semver-flattener-plugin',
name: 'SemVer Flattener',
diff --git a/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.test.ts b/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.test.ts
new file mode 100644
index 000000000000..92573934a626
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.test.ts
@@ -0,0 +1,80 @@
+import { HogFunctionInvocationGlobals } from '../../../types'
+import { TemplateTester } from '../../test/test-helpers'
+import { template } from './semver-flattener.template'
+
+describe('semver-flattener.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('flattens each targeted version and leaves others untouched', async () => {
+ mockGlobals = tester.createGlobals({
+ event: {
+ properties: {
+ targetted_version: '1.12.20',
+ not_a_targetted_version: '1.34.53',
+ another_targetted_version: '1.23.14-pre+build.12345',
+ },
+ },
+ })
+
+ const result = await invoke({ properties: 'targetted_version, another_targetted_version' }, mockGlobals)
+
+ expect(result.properties).toEqual({
+ targetted_version: '1.12.20',
+ targetted_version__major: 1,
+ targetted_version__minor: 12,
+ targetted_version__patch: 20,
+ not_a_targetted_version: '1.34.53',
+ another_targetted_version: '1.23.14-pre+build.12345',
+ another_targetted_version__major: 1,
+ another_targetted_version__minor: 23,
+ another_targetted_version__patch: 14,
+ another_targetted_version__preRelease: 'pre',
+ another_targetted_version__build: 'build.12345',
+ })
+ })
+
+ it('handles versions with no patch, pre-release joined by dashes, and build-only', async () => {
+ mockGlobals = tester.createGlobals({
+ event: {
+ properties: {
+ a: '22.7',
+ b: '22.7-pre-release',
+ c: '1.0.0+20130313144700',
+ },
+ },
+ })
+
+ const result = await invoke({ properties: 'a, b, c' }, mockGlobals)
+
+ expect(result.properties.a__major).toBe(22)
+ expect(result.properties.a__minor).toBe(7)
+ expect(result.properties.a__patch).toBeUndefined()
+ expect(result.properties.b__preRelease).toBe('pre-release')
+ expect(result.properties.b__patch).toBeUndefined()
+ expect(result.properties.c__patch).toBe(0)
+ expect(result.properties.c__build).toBe('20130313144700')
+ expect(result.properties.c__preRelease).toBeUndefined()
+ })
+
+ it('leaves the event unchanged when the target property is absent', async () => {
+ mockGlobals = tester.createGlobals({
+ event: { properties: { unrelated: 'x' } },
+ })
+
+ const result = await invoke({ properties: 'app_version' }, mockGlobals)
+
+ expect(result.properties).toEqual({ unrelated: 'x' })
+ })
+})
diff --git a/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.ts b/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.ts
new file mode 100644
index 000000000000..93f7aa1cac3c
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/semver-flattener/semver-flattener.template.ts
@@ -0,0 +1,74 @@
+import { HogFunctionTemplate } from '~/cdp/types'
+
+export const template: HogFunctionTemplate = {
+ free: true,
+ status: 'stable',
+ type: 'transformation',
+ id: 'template-semver-flattener',
+ name: 'SemVer Flattener',
+ description:
+ 'Splits semantic version strings in the chosen properties into separate major, minor, patch, pre-release and build 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)) {
+ return event
+}
+
+let returnEvent := event
+let targets := splitByString(',', inputs.properties)
+
+for (let target in targets) {
+ let name := trim(target)
+ if (not empty(name)) {
+ let candidate := event.properties[name]
+ if (not empty(candidate) and typeof(candidate) == 'string') {
+ // Build metadata is everything after the first '+'
+ let head := candidate
+ let build := null
+ let plusPos := position(candidate, '+')
+ if (plusPos > 0) {
+ head := substring(candidate, 1, plusPos - 1)
+ build := substring(candidate, plusPos + 1, length(candidate) - plusPos)
+ }
+
+ // Pre-release is everything after the first '-'
+ let core := head
+ let preRelease := null
+ let dashPos := position(head, '-')
+ if (dashPos > 0) {
+ core := substring(head, 1, dashPos - 1)
+ preRelease := substring(head, dashPos + 1, length(head) - dashPos)
+ }
+
+ let parts := splitByString('.', core)
+ returnEvent.properties[concat(name, '__major')] := toInt(parts[1])
+ returnEvent.properties[concat(name, '__minor')] := length(parts) > 1 ? toInt(parts[2]) : null
+ if (length(parts) > 2) {
+ returnEvent.properties[concat(name, '__patch')] := toInt(parts[3])
+ }
+ if (not empty(preRelease)) {
+ returnEvent.properties[concat(name, '__preRelease')] := preRelease
+ }
+ if (not empty(build)) {
+ returnEvent.properties[concat(name, '__build')] := build
+ }
+ }
+ }
+}
+
+return returnEvent
+ `,
+ inputs_schema: [
+ {
+ key: 'properties',
+ type: 'string',
+ label: 'Properties to flatten',
+ description:
+ 'Comma-separated list of properties holding semantic version strings (e.g. app_version, sdk_version).',
+ default: '',
+ required: true,
+ },
+ ],
+}
diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.ts
index d4a238144f95..ba5e3138c328 100644
--- a/nodejs/src/cdp/templates/index.ts
+++ b/nodejs/src/cdp/templates/index.ts
@@ -57,6 +57,7 @@ import { template as hashPropertiesTemplate } from './_transformations/hash-prop
import { template as ipAnonymizationTemplate } from './_transformations/ip-anonymization/ip-anonymization.template'
import { template as piiHashingTemplate } from './_transformations/pii-hashing/pii-hashing.template'
import { template as removeNullPropertiesTemplate } from './_transformations/remove-null-properties/remove-null-properties.template'
+import { template as semverFlattenerTemplate } from './_transformations/semver-flattener/semver-flattener.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 logDefaultTemplate } from './_transformations_log/default/default.template'
@@ -122,6 +123,7 @@ export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS: HogFunctionTemplate[] = [
filterPropertiesTemplate,
hashPropertiesTemplate,
urlNormalizationTemplate,
+ semverFlattenerTemplate,
]
export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS_LOG: HogFunctionTemplate[] = [
diff --git a/nodejs/tests/fixtures/hog-bytecode/09c16a088570476ff03299e20fcb603610595edb5b0880e018896af6fa560cfa.json b/nodejs/tests/fixtures/hog-bytecode/09c16a088570476ff03299e20fcb603610595edb5b0880e018896af6fa560cfa.json
new file mode 100644
index 000000000000..633a8f2d8551
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/09c16a088570476ff03299e20fcb603610595edb5b0880e018896af6fa560cfa.json
@@ -0,0 +1 @@
+["_H",1,32,"targetted_version, another_targetted_version",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/1951362d4a79bb38573a377e146db5826af9def12f1088235e722a6b5a9ed51e.json b/nodejs/tests/fixtures/hog-bytecode/1951362d4a79bb38573a377e146db5826af9def12f1088235e722a6b5a9ed51e.json
new file mode 100644
index 000000000000..2bf60ffad396
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/1951362d4a79bb38573a377e146db5826af9def12f1088235e722a6b5a9ed51e.json
@@ -0,0 +1 @@
+["_H",1,32,"app_version",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/a09f8a6862a3adc97202f06f50b357d3183b32999cad843ba8c4ece9659275e7.json b/nodejs/tests/fixtures/hog-bytecode/a09f8a6862a3adc97202f06f50b357d3183b32999cad843ba8c4ece9659275e7.json
new file mode 100644
index 000000000000..81cc70114140
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/a09f8a6862a3adc97202f06f50b357d3183b32999cad843ba8c4ece9659275e7.json
@@ -0,0 +1 @@
+["_H",1,32,"a, b, c",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/c87f1cf2ca1d59d52bfa3b39290ea893f28b09e8816b3adc5cdad77086d2dde7.json b/nodejs/tests/fixtures/hog-bytecode/c87f1cf2ca1d59d52bfa3b39290ea893f28b09e8816b3adc5cdad77086d2dde7.json
new file mode 100644
index 000000000000..8ae0119eaefd
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/c87f1cf2ca1d59d52bfa3b39290ea893f28b09e8816b3adc5cdad77086d2dde7.json
@@ -0,0 +1 @@
+["_H",1,32,"properties",32,"event",1,2,2,"empty",1,40,5,32,"event",1,1,38,32,"event",1,1,32,",",32,"properties",32,"inputs",1,2,2,"splitByString",2,36,1,36,2,2,"values",1,33,1,36,3,2,"length",1,31,36,5,36,4,16,40,306,36,3,36,4,45,37,6,36,6,2,"trim",1,36,7,2,"empty",1,5,40,276,32,"properties",32,"event",1,2,36,7,45,36,8,2,"empty",1,5,32,"string",36,8,2,"typeof",1,11,3,2,40,248,36,8,31,36,8,32,"+",2,"position",2,33,0,36,11,13,40,34,36,8,33,1,33,1,36,11,7,2,"substring",3,37,9,36,8,33,1,36,11,6,36,11,36,8,2,"length",1,7,2,"substring",3,37,10,36,9,31,36,9,32,"-",2,"position",2,33,0,36,14,13,40,34,36,9,33,1,33,1,36,14,7,2,"substring",3,37,12,36,9,33,1,36,14,6,36,14,36,9,2,"length",1,7,2,"substring",3,37,13,32,".",36,12,2,"splitByString",2,36,0,32,"properties",45,36,7,32,"__major",2,"concat",2,36,15,33,1,45,2,"toInt",1,46,36,0,32,"properties",45,36,7,32,"__minor",2,"concat",2,33,1,36,15,2,"length",1,13,40,10,36,15,33,2,45,2,"toInt",1,39,1,31,46,33,2,36,15,2,"length",1,13,40,21,36,0,32,"properties",45,36,7,32,"__patch",2,"concat",2,36,15,33,3,45,2,"toInt",1,46,36,13,2,"empty",1,5,40,15,36,0,32,"properties",45,36,7,32,"__preRelease",2,"concat",2,36,13,46,36,10,2,"empty",1,5,40,15,36,0,32,"properties",45,36,7,32,"__build",2,"concat",2,36,10,46,35,35,35,35,35,35,35,35,35,36,4,33,1,6,37,4,39,-313,35,35,35,35,35,36,0,38,35,35]