Skip to content

Commit bf99662

Browse files
committed
fix(amplitude): validate retention brackets, require formula param, add segmentation 2nd group-by
- Retention now validates retentionBrackets as a JSON array and requires it when retentionMode is "bracket", matching the block UI's requirement - Event Segmentation now throws if metric is "formula" but no formula is provided, matching the block UI's requirement - Event Segmentation now supports a documented second group-by property via groupBy2/g2 - Corrected Get Active Users' group-by copy — Amplitude's docs don't document a second-property syntax for /api/2/users the way they do for segmentation's g2, so the field no longer overpromises "max two"
1 parent 15e7c14 commit bf99662

5 files changed

Lines changed: 51 additions & 4 deletions

File tree

apps/sim/blocks/blocks/amplitude.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,14 @@ export const AmplitudeBlock: BlockConfig = {
498498
condition: { field: 'operation', value: 'event_segmentation' },
499499
mode: 'advanced',
500500
},
501+
{
502+
id: 'segmentationGroupBy2',
503+
title: 'Group By (2nd Property)',
504+
type: 'short-input',
505+
placeholder: 'Second property name (prefix custom with "gp:")',
506+
condition: { field: 'operation', value: 'event_segmentation' },
507+
mode: 'advanced',
508+
},
501509
{
502510
id: 'segmentationLimit',
503511
title: 'Limit',
@@ -605,7 +613,7 @@ export const AmplitudeBlock: BlockConfig = {
605613
id: 'activeUsersGroupBy',
606614
title: 'Group By',
607615
type: 'short-input',
608-
placeholder: 'Property name (max two)',
616+
placeholder: 'Property name',
609617
condition: { field: 'operation', value: 'get_active_users' },
610618
mode: 'advanced',
611619
},
@@ -966,6 +974,7 @@ export const AmplitudeBlock: BlockConfig = {
966974
if (params.segmentationMetric) result.metric = params.segmentationMetric
967975
if (params.segmentationInterval) result.interval = params.segmentationInterval
968976
if (params.segmentationGroupBy) result.groupBy = params.segmentationGroupBy
977+
if (params.segmentationGroupBy2) result.groupBy2 = params.segmentationGroupBy2
969978
if (params.segmentationLimit) result.limit = params.segmentationLimit
970979
if (params.segmentationFilters) result.filters = params.segmentationFilters
971980
if (params.segmentationFormula) result.formula = params.segmentationFormula
@@ -1049,6 +1058,10 @@ export const AmplitudeBlock: BlockConfig = {
10491058
dataResidency: { type: 'string', description: 'Data residency region: "us" or "eu"' },
10501059
segmentationFilters: { type: 'string', description: 'Event segmentation filters JSON' },
10511060
segmentationFormula: { type: 'string', description: 'Event segmentation formula expression' },
1061+
segmentationGroupBy2: {
1062+
type: 'string',
1063+
description: 'Event segmentation second group-by property',
1064+
},
10521065
segmentationSegment: {
10531066
type: 'string',
10541067
description: 'Event segmentation segment definition JSON',

apps/sim/tools/amplitude/event_segmentation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export const eventSegmentationTool: ToolConfig<
6565
visibility: 'user-or-llm',
6666
description: 'Property name to group by (prefix custom user properties with "gp:")',
6767
},
68+
groupBy2: {
69+
type: 'string',
70+
required: false,
71+
visibility: 'user-or-llm',
72+
description: 'Second property name to group by (prefix custom user properties with "gp:")',
73+
},
6874
limit: {
6975
type: 'string',
7076
required: false,
@@ -118,12 +124,19 @@ export const eventSegmentationTool: ToolConfig<
118124
event.filters = parsedFilters
119125
}
120126

127+
if (params.metric === 'formula' && !params.formula) {
128+
throw new Error(
129+
'Amplitude Event Segmentation: "formula" is required when metric is "formula"'
130+
)
131+
}
132+
121133
url.searchParams.set('e', JSON.stringify(event))
122134
url.searchParams.set('start', params.start)
123135
url.searchParams.set('end', params.end)
124136
if (params.metric) url.searchParams.set('m', params.metric)
125137
if (params.interval) url.searchParams.set('i', params.interval)
126138
if (params.groupBy) url.searchParams.set('g', params.groupBy)
139+
if (params.groupBy2) url.searchParams.set('g2', params.groupBy2)
127140
if (params.limit) url.searchParams.set('limit', params.limit)
128141
if (params.formula) url.searchParams.set('formula', params.formula)
129142
if (params.segment) url.searchParams.set('s', params.segment)

apps/sim/tools/amplitude/get_active_users.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const getActiveUsersTool: ToolConfig<
5555
type: 'string',
5656
required: false,
5757
visibility: 'user-or-llm',
58-
description: 'Property name to group by (max two)',
58+
description: 'Property name to group by',
5959
},
6060
segment: {
6161
type: 'string',

apps/sim/tools/amplitude/retention.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,27 @@ export const retentionTool: ToolConfig<AmplitudeRetentionParams, AmplitudeRetent
110110
url.searchParams.set('start', params.start)
111111
url.searchParams.set('end', params.end)
112112
if (params.retentionMode) url.searchParams.set('rm', params.retentionMode)
113-
if (params.retentionBrackets) url.searchParams.set('rb', params.retentionBrackets)
113+
114+
if (params.retentionMode === 'bracket') {
115+
if (!params.retentionBrackets) {
116+
throw new Error(
117+
'Amplitude Retention: "retentionBrackets" is required when Retention Mode is "bracket"'
118+
)
119+
}
120+
let parsedBrackets: unknown
121+
try {
122+
parsedBrackets = JSON.parse(params.retentionBrackets)
123+
} catch {
124+
parsedBrackets = undefined
125+
}
126+
if (!Array.isArray(parsedBrackets)) {
127+
throw new Error(
128+
'Amplitude Retention: "retentionBrackets" must be a valid JSON array of day ranges, e.g. [[0,4]]'
129+
)
130+
}
131+
url.searchParams.set('rb', JSON.stringify(parsedBrackets))
132+
}
133+
114134
if (params.interval) url.searchParams.set('i', params.interval)
115135
if (params.groupBy) url.searchParams.set('g', params.groupBy)
116136
if (params.segment) url.searchParams.set('s', params.segment)

apps/sim/tools/amplitude/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export interface AmplitudeEventSegmentationParams extends AmplitudeBasicAuthPara
168168
metric?: string
169169
interval?: string
170170
groupBy?: string
171+
groupBy2?: string
171172
limit?: string
172173
/** JSON array of filter objects applied to the event (subprop_type, subprop_key, subprop_op, subprop_value). */
173174
filters?: string
@@ -194,7 +195,7 @@ export interface AmplitudeGetActiveUsersParams extends AmplitudeBasicAuthParams
194195
end: string
195196
metric?: string
196197
interval?: string
197-
/** Property to group by (up to two). */
198+
/** Property to group by. */
198199
groupBy?: string
199200
/** JSON segment definition(s) applied to the query. */
200201
segment?: string

0 commit comments

Comments
 (0)