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 Taxonomy transformation (#93941)

Meikel Ratzf470f0382696
diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/taxonomy-plugin/template.ts b/nodejs/src/cdp/legacy-plugins/_transformations/taxonomy-plugin/template.ts
index 8f3b29c09190..8e7ecd99c5c4 100644
--- a/nodejs/src/cdp/legacy-plugins/_transformations/taxonomy-plugin/template.ts
+++ b/nodejs/src/cdp/legacy-plugins/_transformations/taxonomy-plugin/template.ts
@@ -6,7 +6,7 @@ export const taxonomyPlugin: LegacyTransformationPlugin = {
processEvent,
template: {
free: true,
- status: 'stable',
+ status: 'deprecated',
type: 'transformation',
id: 'plugin-taxonomy-plugin',
name: 'Taxonomy',
diff --git a/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.test.ts b/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.test.ts
new file mode 100644
index 000000000000..82e2bb0ac002
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.test.ts
@@ -0,0 +1,81 @@
+import { HogFunctionInvocationGlobals } from '../../../types'
+import { TemplateTester } from '../../test/test-helpers'
+import { template } from './taxonomy.template'
+
+describe('taxonomy.template', () => {
+ const tester = new TemplateTester(template)
+
+ beforeEach(async () => {
+ await tester.beforeEach()
+ })
+
+ const rename = async (namingConvention: string, eventName: string): Promise<string> => {
+ const globals: HogFunctionInvocationGlobals = tester.createGlobals({
+ event: { event: eventName, properties: {} },
+ })
+ const response = await tester.invoke({ namingConvention }, globals)
+ expect(response.finished).toBe(true)
+ expect(response.error).toBeUndefined()
+ return (response.execResult as any).event
+ }
+
+ it.each([
+ ['user_signed_up', 'userSignedUp'],
+ ['User Logged In', 'userLoggedIn'],
+ ['checkout-completed', 'checkoutCompleted'],
+ ['userSignedUp', 'userSignedUp'],
+ ['APIResponse', 'apiResponse'],
+ ['api_response', 'apiResponse'],
+ ])('converts %s to camelCase', async (input, expected) => {
+ expect(await rename('camelCase', input)).toBe(expected)
+ })
+
+ it.each([
+ ['user_signed_up', 'UserSignedUp'],
+ ['user logged in', 'UserLoggedIn'],
+ ['checkout-completed', 'CheckoutCompleted'],
+ ['HTTPError', 'HttpError'],
+ ])('converts %s to PascalCase', async (input, expected) => {
+ expect(await rename('PascalCase', input)).toBe(expected)
+ })
+
+ it.each([
+ ['userSignedUp', 'user_signed_up'],
+ ['User Logged In', 'user_logged_in'],
+ ['checkout-completed', 'checkout_completed'],
+ ['URLChanged', 'url_changed'],
+ ['viewedFAQ', 'viewed_faq'],
+ ['caféOrder', 'café_order'],
+ ])('converts %s to snake_case', async (input, expected) => {
+ expect(await rename('snake_case', input)).toBe(expected)
+ })
+
+ it.each([
+ ['userSignedUp', 'user-signed-up'],
+ ['User Logged In', 'user-logged-in'],
+ ['checkout_completed', 'checkout-completed'],
+ ])('converts %s to kebab-case', async (input, expected) => {
+ expect(await rename('kebab-case', input)).toBe(expected)
+ })
+
+ it.each([
+ ['userSignedUp', 'user signed up'],
+ ['checkout_completed', 'checkout completed'],
+ ])('converts %s to spaces', async (input, expected) => {
+ expect(await rename('spaces', input)).toBe(expected)
+ })
+
+ it('leaves the name alone for a convention outside the list', async () => {
+ expect(await rename('spaces in between', 'user_signed_up')).toBe('user_signed_up')
+ })
+
+ it('leaves PostHog events and survey events untouched', async () => {
+ expect(await rename('snake_case', '$pageview')).toBe('$pageview')
+ expect(await rename('snake_case', 'survey sent')).toBe('survey sent')
+ })
+
+ it('leaves an oversized event name unchanged', async () => {
+ const oversized = 'aVeryLongEventName'.repeat(20)
+ expect(await rename('snake_case', oversized)).toBe(oversized)
+ })
+})
diff --git a/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.ts b/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.ts
new file mode 100644
index 000000000000..83c710ca3c2f
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/taxonomy/taxonomy.template.ts
@@ -0,0 +1,133 @@
+import { HogFunctionTemplate } from '~/cdp/types'
+
+export const template: HogFunctionTemplate = {
+ free: true,
+ status: 'stable',
+ type: 'transformation',
+ id: 'template-taxonomy',
+ name: 'Taxonomy',
+ description: 'Standardizes event names into one naming convention, such as camelCase or snake_case.',
+ icon_url: 'https://res.cloudinary.com/dmukukwp6/image/upload/q_auto,f_auto/builder_hog_01_955c082cad.png',
+ category: ['Custom'],
+ code_language: 'hog',
+ code: `
+// PostHog's own events (names starting with $) and survey events keep their names.
+if (empty(event.event) or startsWith(event.event, '$')) {
+ return event
+}
+if (event.event == 'survey shown' or event.event == 'survey sent' or event.event == 'survey dismissed') {
+ return event
+}
+// Real event names are short. Leave an oversized one unchanged so a crafted name cannot make the
+// character-by-character tokenizer run long enough to eat the transformation time budget.
+if (length(event.event) > 200) {
+ return event
+}
+
+fun isUpper(ch) {
+ return upper(ch) == ch and lower(ch) != ch
+}
+
+fun isLower(ch) {
+ return lower(ch) == ch and upper(ch) != ch
+}
+
+fun capitalize(word) {
+ if (empty(word)) {
+ return word
+ }
+ return concat(upper(substring(word, 1, 1)), substring(word, 2, length(word) - 1))
+}
+
+// Split a name into lowercased words on separators and on case boundaries,
+// so 'userSignedUp', 'user_signed_up' and 'User Signed Up' all become ['user', 'signed', 'up'].
+// Keep the last character in its own variable. Reading it back out of the word instead would
+// pair length() with substring(), and those two count differently on one of the supported
+// virtual machines, which drops the boundary after a word that holds a non-ASCII character.
+fun tokenize(name) {
+ let words := []
+ let current := ''
+ let previous := ''
+ let i := 1
+ while (i <= length(name)) {
+ let ch := substring(name, i, 1)
+ if (ch == '_' or ch == '-' or ch == ' ' or ch == '.') {
+ if (not empty(current)) {
+ words := arrayPushBack(words, lower(current))
+ current := ''
+ }
+ previous := ''
+ } else {
+ // 'userSigned' breaks at the capital. 'APIResponse' breaks at the last capital of a
+ // run, so the character after this one decides.
+ let following := substring(name, i + 1, 1)
+ if (
+ isUpper(ch)
+ and not empty(current)
+ and (isLower(previous) or (isUpper(previous) and isLower(following)))
+ ) {
+ words := arrayPushBack(words, lower(current))
+ current := ''
+ }
+ current := concat(current, ch)
+ previous := ch
+ }
+ i := i + 1
+ }
+ if (not empty(current)) {
+ words := arrayPushBack(words, lower(current))
+ }
+ return words
+}
+
+let words := tokenize(event.event)
+if (empty(words)) {
+ return event
+}
+
+let convention := inputs.namingConvention
+let result := ''
+
+if (convention == 'snake_case') {
+ result := arrayStringConcat(words, '_')
+} else if (convention == 'kebab-case') {
+ result := arrayStringConcat(words, '-')
+} else if (convention == 'spaces') {
+ result := arrayStringConcat(words, ' ')
+} else if (convention == 'camelCase' or convention == 'PascalCase') {
+ let i := 1
+ while (i <= length(words)) {
+ if (i == 1 and convention == 'camelCase') {
+ result := concat(result, words[i])
+ } else {
+ result := concat(result, capitalize(words[i]))
+ }
+ i := i + 1
+ }
+} else {
+ // A convention outside the list keeps the name, because a renamed event cannot be undone.
+ return event
+}
+
+let returnEvent := event
+returnEvent.event := result
+return returnEvent
+ `,
+ inputs_schema: [
+ {
+ key: 'namingConvention',
+ type: 'choice',
+ label: 'Naming convention',
+ description: 'The convention every event name is converted to.',
+ default: 'camelCase',
+ required: true,
+ choices: [
+ { label: 'camelCase', value: 'camelCase' },
+ { label: 'PascalCase', value: 'PascalCase' },
+ { label: 'snake_case', value: 'snake_case' },
+ { label: 'kebab-case', value: 'kebab-case' },
+ { label: 'Spaces', value: 'spaces' },
+ ],
+ },
+ ],
+}
diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.ts
index 57da56d43365..6dbdd4d03363 100644
--- a/nodejs/src/cdp/templates/index.ts
+++ b/nodejs/src/cdp/templates/index.ts
@@ -59,6 +59,7 @@ import { template as ipAnonymizationTemplate } from './_transformations/ip-anony
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 taxonomyTemplate } from './_transformations/taxonomy/taxonomy.template'
import { 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'
@@ -130,6 +131,7 @@ export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS: HogFunctionTemplate[] = [
timestampParserTemplate,
downsamplingTemplate,
urlParametersToPropertiesTemplate,
+ taxonomyTemplate,
]
export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS_LOG: HogFunctionTemplate[] = [
diff --git a/nodejs/tests/fixtures/hog-bytecode/350ee91a65671e4610e3c3f3cce7b9518a860ef033940ecdfd02289889d93c2e.json b/nodejs/tests/fixtures/hog-bytecode/350ee91a65671e4610e3c3f3cce7b9518a860ef033940ecdfd02289889d93c2e.json
new file mode 100644
index 000000000000..467474ed258b
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/350ee91a65671e4610e3c3f3cce7b9518a860ef033940ecdfd02289889d93c2e.json
@@ -0,0 +1 @@
+["_H",1,32,"event",32,"event",1,2,2,"empty",1,32,"event",32,"event",1,2,32,"$",2,"startsWith",2,4,2,40,5,32,"event",1,1,38,32,"survey shown",32,"event",32,"event",1,2,11,32,"survey sent",32,"event",32,"event",1,2,11,32,"survey dismissed",32,"event",32,"event",1,2,11,4,3,40,5,32,"event",1,1,38,52,"isUpper",1,0,19,36,0,36,0,2,"upper",1,11,36,0,36,0,2,"lower",1,12,3,2,38,53,0,52,"isLower",1,0,19,36,0,36,0,2,"lower",1,11,36,0,36,0,2,"upper",1,12,3,2,38,53,0,52,"capitalize",1,0,41,36,0,2,"empty",1,40,3,36,0,38,36,0,33,1,33,1,2,"substring",3,2,"upper",1,36,0,33,2,33,1,36,0,2,"length",1,7,2,"substring",3,2,"concat",2,38,53,0,52,"tokenize",1,2,198,43,0,32,"",32,"",33,1,36,0,2,"length",1,36,4,16,40,153,36,0,36,4,33,1,2,"substring",3,32,"_",36,5,11,32,"-",36,5,11,32," ",36,5,11,32,".",36,5,11,4,4,40,30,36,2,2,"empty",1,5,40,16,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,32,"",37,2,32,"",37,3,39,80,36,0,33,1,36,4,6,33,1,2,"substring",3,36,5,55,0,54,1,36,2,2,"empty",1,5,36,3,55,1,54,1,36,3,55,0,54,1,36,6,55,1,54,1,3,2,4,2,3,3,40,16,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,32,"",37,2,36,2,36,5,2,"concat",2,37,2,36,5,37,3,35,33,1,36,4,6,37,4,35,39,-163,36,2,2,"empty",1,5,40,12,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,36,1,38,35,35,35,35,53,2,true,0,true,1,32,"event",32,"event",1,2,36,3,54,1,36,4,2,"empty",1,40,5,32,"event",1,1,38,32,"namingConvention",32,"inputs",1,2,32,"",32,"snake_case",36,5,11,40,11,36,4,32,"_",2,"arrayStringConcat",2,37,6,39,123,32,"kebab-case",36,5,11,40,11,36,4,32,"-",2,"arrayStringConcat",2,37,6,39,105,32,"spaces",36,5,11,40,11,36,4,32," ",2,"arrayStringConcat",2,37,6,39,87,32,"camelCase",36,5,11,32,"PascalCase",36,5,11,4,2,40,68,33,1,36,4,2,"length",1,36,7,16,40,53,33,1,36,7,11,32,"camelCase",36,5,11,3,2,40,14,36,6,36,4,36,7,45,2,"concat",2,37,6,39,16,36,6,36,4,36,7,45,36,2,54,1,2,"concat",2,37,6,33,1,36,7,6,37,7,39,-63,35,39,5,32,"event",1,1,38,32,"event",1,1,36,7,32,"event",36,6,46,36,7,38,35,35,35,35,35,35,57,57]
diff --git a/nodejs/tests/fixtures/hog-bytecode/522412cb51db678c4853daaba363646d2dcd69a3602c6b9a3f6e0133bdbd999e.json b/nodejs/tests/fixtures/hog-bytecode/522412cb51db678c4853daaba363646d2dcd69a3602c6b9a3f6e0133bdbd999e.json
new file mode 100644
index 000000000000..0f20e4a0b249
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/522412cb51db678c4853daaba363646d2dcd69a3602c6b9a3f6e0133bdbd999e.json
@@ -0,0 +1 @@
+["_H",1,32,"snake_case",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/62dc10c5aa713670af2dd360533892ba25eaf3fc4416103bbe0868a3c14d2e47.json b/nodejs/tests/fixtures/hog-bytecode/62dc10c5aa713670af2dd360533892ba25eaf3fc4416103bbe0868a3c14d2e47.json
new file mode 100644
index 000000000000..6e72ed726c24
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/62dc10c5aa713670af2dd360533892ba25eaf3fc4416103bbe0868a3c14d2e47.json
@@ -0,0 +1 @@
+["_H",1,32,"spaces in between",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/755b198652eda66f81491d6c4feca1f3b7afe12a0d036b083f0c5be349597cbb.json b/nodejs/tests/fixtures/hog-bytecode/755b198652eda66f81491d6c4feca1f3b7afe12a0d036b083f0c5be349597cbb.json
new file mode 100644
index 000000000000..01fda262fdf9
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/755b198652eda66f81491d6c4feca1f3b7afe12a0d036b083f0c5be349597cbb.json
@@ -0,0 +1 @@
+["_H",1,32,"camelCase",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/93ae9277f6ca3151b05b245db2b344e4f6e8f87d99dd859330650769b2211b01.json b/nodejs/tests/fixtures/hog-bytecode/93ae9277f6ca3151b05b245db2b344e4f6e8f87d99dd859330650769b2211b01.json
new file mode 100644
index 000000000000..5eea07f7558b
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/93ae9277f6ca3151b05b245db2b344e4f6e8f87d99dd859330650769b2211b01.json
@@ -0,0 +1 @@
+["_H",1,32,"kebab-case",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/991ae53fe308070d8d6a3ea679eeaa954df43ef872cf4072349e0e5b30c4467d.json b/nodejs/tests/fixtures/hog-bytecode/991ae53fe308070d8d6a3ea679eeaa954df43ef872cf4072349e0e5b30c4467d.json
new file mode 100644
index 000000000000..e8151bf7ca0a
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/991ae53fe308070d8d6a3ea679eeaa954df43ef872cf4072349e0e5b30c4467d.json
@@ -0,0 +1 @@
+["_H",1,32,"spaces",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/c68e1af8f93a55d4692a4d88e11c1a0076894981d3172baebed402cfe97a7703.json b/nodejs/tests/fixtures/hog-bytecode/c68e1af8f93a55d4692a4d88e11c1a0076894981d3172baebed402cfe97a7703.json
new file mode 100644
index 000000000000..4ad47cc0bee3
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/c68e1af8f93a55d4692a4d88e11c1a0076894981d3172baebed402cfe97a7703.json
@@ -0,0 +1 @@
+["_H",1,32,"PascalCase",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/fc520235b0c7248eb9492f57a31a658a2673e60eea6c971628ac9cd2ca483d92.json b/nodejs/tests/fixtures/hog-bytecode/fc520235b0c7248eb9492f57a31a658a2673e60eea6c971628ac9cd2ca483d92.json
new file mode 100644
index 000000000000..76d39952c242
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/fc520235b0c7248eb9492f57a31a658a2673e60eea6c971628ac9cd2ca483d92.json
@@ -0,0 +1 @@
+["_H",1,32,"event",32,"event",1,2,2,"empty",1,32,"event",32,"event",1,2,32,"$",2,"startsWith",2,4,2,40,5,32,"event",1,1,38,32,"survey shown",32,"event",32,"event",1,2,11,32,"survey sent",32,"event",32,"event",1,2,11,32,"survey dismissed",32,"event",32,"event",1,2,11,4,3,40,5,32,"event",1,1,38,33,200,32,"event",32,"event",1,2,2,"length",1,13,40,5,32,"event",1,1,38,52,"isUpper",1,0,19,36,0,36,0,2,"upper",1,11,36,0,36,0,2,"lower",1,12,3,2,38,53,0,52,"isLower",1,0,19,36,0,36,0,2,"lower",1,11,36,0,36,0,2,"upper",1,12,3,2,38,53,0,52,"capitalize",1,0,41,36,0,2,"empty",1,40,3,36,0,38,36,0,33,1,33,1,2,"substring",3,2,"upper",1,36,0,33,2,33,1,36,0,2,"length",1,7,2,"substring",3,2,"concat",2,38,53,0,52,"tokenize",1,2,198,43,0,32,"",32,"",33,1,36,0,2,"length",1,36,4,16,40,153,36,0,36,4,33,1,2,"substring",3,32,"_",36,5,11,32,"-",36,5,11,32," ",36,5,11,32,".",36,5,11,4,4,40,30,36,2,2,"empty",1,5,40,16,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,32,"",37,2,32,"",37,3,39,80,36,0,33,1,36,4,6,33,1,2,"substring",3,36,5,55,0,54,1,36,2,2,"empty",1,5,36,3,55,1,54,1,36,3,55,0,54,1,36,6,55,1,54,1,3,2,4,2,3,3,40,16,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,32,"",37,2,36,2,36,5,2,"concat",2,37,2,36,5,37,3,35,33,1,36,4,6,37,4,35,39,-163,36,2,2,"empty",1,5,40,12,36,1,36,2,2,"lower",1,2,"arrayPushBack",2,37,1,36,1,38,35,35,35,35,53,2,true,0,true,1,32,"event",32,"event",1,2,36,3,54,1,36,4,2,"empty",1,40,5,32,"event",1,1,38,32,"namingConvention",32,"inputs",1,2,32,"",32,"snake_case",36,5,11,40,11,36,4,32,"_",2,"arrayStringConcat",2,37,6,39,123,32,"kebab-case",36,5,11,40,11,36,4,32,"-",2,"arrayStringConcat",2,37,6,39,105,32,"spaces",36,5,11,40,11,36,4,32," ",2,"arrayStringConcat",2,37,6,39,87,32,"camelCase",36,5,11,32,"PascalCase",36,5,11,4,2,40,68,33,1,36,4,2,"length",1,36,7,16,40,53,33,1,36,7,11,32,"camelCase",36,5,11,3,2,40,14,36,6,36,4,36,7,45,2,"concat",2,37,6,39,16,36,6,36,4,36,7,45,36,2,54,1,2,"concat",2,37,6,33,1,36,7,6,37,7,39,-63,35,39,5,32,"event",1,1,38,32,"event",1,1,36,7,32,"event",36,6,46,36,7,38,35,35,35,35,35,35,57,57]