Translate Segment destination config into PostHog hog-function config
harbor run -t mimo-v2.6-rl/format-code-task-000210harbor download mimo-v2.6-rl/format-code-task-000210Fix the following issue:
We're starting to import Segment action destinations into PostHog's CDP. Segment describes what to send using two pieces of config that we need to convert into the equivalents PostHog already understands:
@path, @if).Add a small, dependency-free translation layer for these two pieces. It must live in
the plugin-server CDP code and expose two pure functions from
plugin-server/src/cdp/segment/segment-templates.ts:
translateFilters(subscribe: string): { events: HogFunctionFilterEvent[] }
translateInputs(defaultVal: any, multiple?: boolean): any
translateFiltersSegment's FQL talks about event types; PostHog talks about event names. Given a
subscription string, return a single PostHog event filter:
{
events: [
{
id: 'All events',
name: 'All events',
type: 'events',
order: 0,
properties: [{ key: <translated string>, type: 'hogql', value: null }],
},
],
}
<translated string> is the input string after applying these rewrites to every
occurrence:
| Segment FQL fragment | PostHog HogQL fragment |
|---|---|
type = "page" |
event = "$pageview" |
type = "screen" |
event = "$screen" |
type = "identify" |
event in ('$identify', '$set') |
type = "group" |
event = "$groupidentify" |
type = "track" |
event not in ('$pageview', '$screen', '$alias', '$identify', '$set', '$groupidentify') |
type = "alias" |
event = "$alias" |
After those substitutions, every remaining double quote (") in the string becomes a
single quote ('). Fragments not listed above (e.g. a name = "Order Completed"
clause joined with and/or) pass through unchanged apart from the quote conversion.
translateInputsThis resolves a single Segment field default into the string/value PostHog stores for a hog-function input.
boolean or string default is returned unchanged.@path key: take the path string, replace a leading $. with
event., normalize the field reference (see below), then:
'';{<value>}, or
{[<value>]} when multiple is true.@if key shaped like { exists, then, else }:
JSON.stringify(exists) equals JSON.stringify(then). In any other shape, return
JSON.stringify(defaultVal).then and else independently: an @path object is converted exactly
like the @path case above but without the surrounding braces; a plain string
is wrapped in single quotes ('value').then) and fallback (else):
'';{<fallback>};{<primary>};{<primary> ?? <fallback>}, and when multiple is true the
?? expression is wrapped in brackets: {[<primary> ?? <fallback>]}.JSON.stringify(defaultVal).After the $. → event. step, field references are rewritten to their PostHog
equivalents. These specific references must be remapped (matching anywhere in the
string):
| Segment reference | PostHog reference |
|---|---|
event.traits |
person.properties |
event.context.traits |
person.properties |
event.userId |
person.id |
event.anonymousId |
event.distinct_id |
event.messageId |
event.uuid |
context.os.name |
properties.$os |
context.os.version |
properties.$os_version |
context.page.url |
properties.$current_url |
context.page.path |
properties.$pathname |
context.page.referrer |
properties.$referrer |
context.campaign.source |
properties.utm_source |
context.ip |
properties.$ip |
context.locale |
properties.$locale |
context.userAgent |
properties.$raw_user_agent |
Some Segment fields have no PostHog equivalent and must be dropped — replaced with an
empty string. These include context.device.brand, context.network.wifi and
context.device.advertisingId; when one of these is the whole reference, the value
resolves to ''.
General rule applied after the specific remaps:
., the field has no usable mapping —
return ''.A reference that matches none of the above (e.g. event.properties.revenue, or an
unrecognised event.context.* path) passes through unchanged.