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 Timestamp Parser transformation (#93938)

Meikel Ratzf3b56944cd29
diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/timestamp-parser-plugin/template.ts b/nodejs/src/cdp/legacy-plugins/_transformations/timestamp-parser-plugin/template.ts
index 95504a1db56a..5f33f9a2e2a5 100644
--- a/nodejs/src/cdp/legacy-plugins/_transformations/timestamp-parser-plugin/template.ts
+++ b/nodejs/src/cdp/legacy-plugins/_transformations/timestamp-parser-plugin/template.ts
@@ -6,7 +6,7 @@ export const timestampParserPlugin: LegacyTransformationPlugin = {
processEvent,
template: {
free: true,
- status: 'stable',
+ status: 'deprecated',
type: 'transformation',
id: 'plugin-timestamp-parser-plugin',
name: 'Timestamp Parser',
diff --git a/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.test.ts b/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.test.ts
new file mode 100644
index 000000000000..46277c298360
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.test.ts
@@ -0,0 +1,82 @@
+import { HogFunctionInvocationGlobals } from '../../../types'
+import { TemplateTester } from '../../test/test-helpers'
+import { template } from './timestamp-parser.template'
+
+describe('timestamp-parser.template', () => {
+ const tester = new TemplateTester(template)
+ let mockGlobals: HogFunctionInvocationGlobals
+
+ beforeEach(async () => {
+ await tester.beforeEach()
+ })
+
+ const invoke = async (globals: HogFunctionInvocationGlobals): Promise<any> => {
+ const response = await tester.invoke({}, globals)
+ expect(response.finished).toBe(true)
+ expect(response.error).toBeUndefined()
+ return response.execResult as any
+ }
+
+ it('parses an ISO timestamp into date parts', async () => {
+ mockGlobals = tester.createGlobals({
+ event: { timestamp: '2024-01-01T13:37:00Z', properties: {} },
+ })
+
+ const result = await invoke(mockGlobals)
+
+ expect(result.properties.day_of_the_week).toBe('Monday')
+ expect(result.properties.day).toBe(1)
+ expect(result.properties.month).toBe(1)
+ expect(result.properties.year).toBe(2024)
+ expect(result.properties.hour).toBe(13)
+ expect(result.properties.minute).toBe(37)
+ })
+
+ it('parses a numeric unix timestamp in seconds', async () => {
+ // 2024-01-01T13:37:00Z as unix seconds. Cast because the globals type declares a string
+ // timestamp, but SDKs can send a numeric one, which this template handles.
+ mockGlobals = tester.createGlobals({
+ event: { timestamp: 1704116220 as unknown as string, properties: {} },
+ })
+
+ const result = await invoke(mockGlobals)
+
+ expect(result.properties.year).toBe(2024)
+ expect(result.properties.hour).toBe(13)
+ expect(result.properties.minute).toBe(37)
+ })
+
+ it('reads a pre-2001 millisecond timestamp as milliseconds, not seconds', async () => {
+ // 2000-01-01T00:00:00Z in milliseconds. Below the old threshold, this was misread as
+ // seconds and produced a year around 31970.
+ mockGlobals = tester.createGlobals({
+ event: { timestamp: 946684800000 as unknown as string, properties: {} },
+ })
+
+ const result = await invoke(mockGlobals)
+
+ expect(result.properties.year).toBe(2000)
+ })
+
+ it('writes no date properties for an unparseable timestamp', async () => {
+ mockGlobals = tester.createGlobals({
+ event: { timestamp: 'not a real timestamp', properties: { keep: 'me' } },
+ })
+
+ const result = await invoke(mockGlobals)
+
+ expect(result.properties.year).toBeUndefined()
+ expect(result.properties.day_of_the_week).toBeUndefined()
+ expect(result.properties.keep).toBe('me')
+ })
+
+ it('leaves the event unchanged when there is no timestamp', async () => {
+ mockGlobals = tester.createGlobals({
+ event: { timestamp: '', properties: { keep: 'me' } },
+ })
+
+ const result = await invoke(mockGlobals)
+
+ expect(result.properties).toEqual({ keep: 'me' })
+ })
+})
diff --git a/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.ts b/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.ts
new file mode 100644
index 000000000000..442e5d4592a6
--- /dev/null
+++ b/nodejs/src/cdp/templates/_transformations/timestamp-parser/timestamp-parser.template.ts
@@ -0,0 +1,47 @@
+import { HogFunctionTemplate } from '~/cdp/types'
+
+export const template: HogFunctionTemplate = {
+ free: true,
+ status: 'stable',
+ type: 'transformation',
+ id: 'template-timestamp-parser',
+ name: 'Timestamp Parser',
+ description:
+ 'Parses the event timestamp into separate day of the week, day, month, year, hour and minute 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.timestamp)) {
+ return event
+}
+
+let returnEvent := event
+let ts := event.timestamp
+let dt := null
+
+if (typeof(ts) == 'string') {
+ dt := toDateTime(ts)
+} else if (typeof(ts) == 'integer' or typeof(ts) == 'float') {
+ // 9999999999 seconds is year 2286, so any larger value is milliseconds. This keeps
+ // millisecond timestamps from before 2001 from being read as far-future second timestamps.
+ dt := ts > 9999999999 ? fromUnixTimestampMilli(ts) : fromUnixTimestamp(ts)
+}
+
+// A string that is not a real timestamp still yields a DateTime, so check that a date part comes
+// back as a real number before writing anything. An unparseable timestamp gives null or NaN, and
+// NaN fails self-equality, so both are rejected here and no junk properties are written.
+let year := dt != null ? extract('year', dt) : null
+if (year != null and year == year) {
+ returnEvent.properties['day_of_the_week'] := formatDateTime(dt, '%W')
+ returnEvent.properties['day'] := extract('day', dt)
+ returnEvent.properties['month'] := extract('month', dt)
+ returnEvent.properties['year'] := year
+ returnEvent.properties['hour'] := extract('hour', dt)
+ returnEvent.properties['minute'] := extract('minute', dt)
+}
+
+return returnEvent
+ `,
+ inputs_schema: [],
+}
diff --git a/nodejs/src/cdp/templates/index.ts b/nodejs/src/cdp/templates/index.ts
index ba5e3138c328..d36c5eec580e 100644
--- a/nodejs/src/cdp/templates/index.ts
+++ b/nodejs/src/cdp/templates/index.ts
@@ -58,6 +58,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 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 logDefaultTemplate } from './_transformations_log/default/default.template'
@@ -124,6 +125,7 @@ export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS: HogFunctionTemplate[] = [
hashPropertiesTemplate,
urlNormalizationTemplate,
semverFlattenerTemplate,
+ timestampParserTemplate,
]
export const HOG_FUNCTION_TEMPLATES_TRANSFORMATIONS_LOG: HogFunctionTemplate[] = [
diff --git a/nodejs/tests/fixtures/hog-bytecode/40c7689e7cd6e4d3784a8efb1601f83c32cfa08d0b502c80ff3b7f5f63cdc5a7.json b/nodejs/tests/fixtures/hog-bytecode/40c7689e7cd6e4d3784a8efb1601f83c32cfa08d0b502c80ff3b7f5f63cdc5a7.json
new file mode 100644
index 000000000000..afa2a3428bfb
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/40c7689e7cd6e4d3784a8efb1601f83c32cfa08d0b502c80ff3b7f5f63cdc5a7.json
@@ -0,0 +1 @@
+["_H",1,32,"timestamp",32,"event",1,2,2,"empty",1,40,5,32,"event",1,1,38,32,"event",1,1,32,"timestamp",32,"event",1,2,31,32,"string",36,1,2,"typeof",1,11,40,9,36,1,2,"toDateTime",1,37,2,39,41,32,"integer",36,1,2,"typeof",1,11,32,"float",36,1,2,"typeof",1,11,4,2,40,21,33,999999999999,36,1,13,40,7,36,1,2,"fromUnixTimestampMilli",1,39,5,36,1,2,"fromUnixTimestamp",1,37,2,31,36,2,12,40,90,36,0,32,"properties",45,32,"day_of_the_week",36,2,32,"%W",2,"formatDateTime",2,46,36,0,32,"properties",45,32,"day",32,"day",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"month",32,"month",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"year",32,"year",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"hour",32,"hour",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"minute",32,"minute",36,2,2,"extract",2,46,36,0,38,35,35,35]
diff --git a/nodejs/tests/fixtures/hog-bytecode/5602f677c5f058aee463383eb3e95f9a5397767ea474851bfef9fcb9896485f8.json b/nodejs/tests/fixtures/hog-bytecode/5602f677c5f058aee463383eb3e95f9a5397767ea474851bfef9fcb9896485f8.json
new file mode 100644
index 000000000000..2587bbc6f739
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/5602f677c5f058aee463383eb3e95f9a5397767ea474851bfef9fcb9896485f8.json
@@ -0,0 +1 @@
+["_H",1,32,"year",32,"2024-05-01T00:00:00Z",2,"toDateTime",1,2,"extract",2,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/59c36ffc5850360765c85225390bc11143e47b245f282957c38cf32c16593788.json b/nodejs/tests/fixtures/hog-bytecode/59c36ffc5850360765c85225390bc11143e47b245f282957c38cf32c16593788.json
new file mode 100644
index 000000000000..b435516f80e2
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/59c36ffc5850360765c85225390bc11143e47b245f282957c38cf32c16593788.json
@@ -0,0 +1 @@
+["_H",1,32,"timestamp",32,"event",1,2,2,"empty",1,40,5,32,"event",1,1,38,32,"event",1,1,32,"timestamp",32,"event",1,2,31,32,"string",36,1,2,"typeof",1,11,40,9,36,1,2,"toDateTime",1,37,2,39,41,32,"integer",36,1,2,"typeof",1,11,32,"float",36,1,2,"typeof",1,11,4,2,40,21,33,999999999999,36,1,13,40,7,36,1,2,"fromUnixTimestampMilli",1,39,5,36,1,2,"fromUnixTimestamp",1,37,2,31,36,2,12,40,9,32,"year",36,2,2,"extract",2,39,1,31,31,36,3,12,40,85,36,0,32,"properties",45,32,"day_of_the_week",36,2,32,"%W",2,"formatDateTime",2,46,36,0,32,"properties",45,32,"day",32,"day",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"month",32,"month",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"year",36,3,46,36,0,32,"properties",45,32,"hour",32,"hour",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"minute",32,"minute",36,2,2,"extract",2,46,36,0,38,35,35,35,35]
diff --git a/nodejs/tests/fixtures/hog-bytecode/641cc072945d5f640d6e442d557181c5cc08b02ef2d083e36cf128fbe2fb077c.json b/nodejs/tests/fixtures/hog-bytecode/641cc072945d5f640d6e442d557181c5cc08b02ef2d083e36cf128fbe2fb077c.json
new file mode 100644
index 000000000000..5b7c1386c3d3
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/641cc072945d5f640d6e442d557181c5cc08b02ef2d083e36cf128fbe2fb077c.json
@@ -0,0 +1 @@
+["_H",1,32,"timestamp",32,"event",1,2,2,"empty",1,40,5,32,"event",1,1,38,32,"event",1,1,32,"timestamp",32,"event",1,2,31,32,"string",36,1,2,"typeof",1,11,40,9,36,1,2,"toDateTime",1,37,2,39,41,32,"integer",36,1,2,"typeof",1,11,32,"float",36,1,2,"typeof",1,11,4,2,40,21,33,999999999999,36,1,13,40,7,36,1,2,"fromUnixTimestampMilli",1,39,5,36,1,2,"fromUnixTimestamp",1,37,2,31,36,2,12,40,9,32,"year",36,2,2,"extract",2,39,1,31,31,36,3,12,36,3,36,3,11,3,2,40,85,36,0,32,"properties",45,32,"day_of_the_week",36,2,32,"%W",2,"formatDateTime",2,46,36,0,32,"properties",45,32,"day",32,"day",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"month",32,"month",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"year",36,3,46,36,0,32,"properties",45,32,"hour",32,"hour",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"minute",32,"minute",36,2,2,"extract",2,46,36,0,38,35,35,35,35]
diff --git a/nodejs/tests/fixtures/hog-bytecode/a491dc5f2c9b7125a8c5e945773fa4f19b7f24e8266348ccc41c1cb330ff63c3.json b/nodejs/tests/fixtures/hog-bytecode/a491dc5f2c9b7125a8c5e945773fa4f19b7f24e8266348ccc41c1cb330ff63c3.json
new file mode 100644
index 000000000000..09d7ca806ca8
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/a491dc5f2c9b7125a8c5e945773fa4f19b7f24e8266348ccc41c1cb330ff63c3.json
@@ -0,0 +1 @@
+["_H",1,32,"year",32,"not a date",2,"toDateTime",1,2,"extract",2,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/f7f8fac4b5563386010f5e51dd40775d900a6b13d32f32dabc970fb59c2b002e.json b/nodejs/tests/fixtures/hog-bytecode/f7f8fac4b5563386010f5e51dd40775d900a6b13d32f32dabc970fb59c2b002e.json
new file mode 100644
index 000000000000..e91f9b895b9b
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/f7f8fac4b5563386010f5e51dd40775d900a6b13d32f32dabc970fb59c2b002e.json
@@ -0,0 +1 @@
+["_H",1,32,"timestamp",32,"event",1,2,2,"empty",1,40,5,32,"event",1,1,38,32,"event",1,1,32,"timestamp",32,"event",1,2,31,32,"string",36,1,2,"typeof",1,11,40,9,36,1,2,"toDateTime",1,37,2,39,41,32,"integer",36,1,2,"typeof",1,11,32,"float",36,1,2,"typeof",1,11,4,2,40,21,33,9999999999,36,1,13,40,7,36,1,2,"fromUnixTimestampMilli",1,39,5,36,1,2,"fromUnixTimestamp",1,37,2,31,36,2,12,40,9,32,"year",36,2,2,"extract",2,39,1,31,31,36,3,12,36,3,36,3,11,3,2,40,85,36,0,32,"properties",45,32,"day_of_the_week",36,2,32,"%W",2,"formatDateTime",2,46,36,0,32,"properties",45,32,"day",32,"day",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"month",32,"month",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"year",36,3,46,36,0,32,"properties",45,32,"hour",32,"hour",36,2,2,"extract",2,46,36,0,32,"properties",45,32,"minute",32,"minute",36,2,2,"extract",2,46,36,0,38,35,35,35,35]