-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(amplitude): correct wire formats and add funnels/retention analytics #5355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
waleedlatif1
wants to merge
2
commits into
staging
Choose a base branch
from
worktree-amplitude-validate
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,150
−45
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import type { AmplitudeFunnelsParams, AmplitudeFunnelsResponse } from '@/tools/amplitude/types' | ||
| import { getDashboardHost } from '@/tools/amplitude/utils' | ||
| import type { ToolConfig } from '@/tools/types' | ||
|
|
||
| export const funnelsTool: ToolConfig<AmplitudeFunnelsParams, AmplitudeFunnelsResponse> = { | ||
| id: 'amplitude_funnels', | ||
| name: 'Amplitude Funnels', | ||
| description: 'Analyze conversion rates and drop-off between a sequence of events.', | ||
| version: '1.0.0', | ||
|
|
||
| params: { | ||
| apiKey: { | ||
| type: 'string', | ||
| required: true, | ||
| visibility: 'user-only', | ||
| description: 'Amplitude API Key', | ||
| }, | ||
| secretKey: { | ||
| type: 'string', | ||
| required: true, | ||
| visibility: 'user-only', | ||
| description: 'Amplitude Secret Key', | ||
| }, | ||
| events: { | ||
| type: 'string', | ||
| required: true, | ||
| visibility: 'user-or-llm', | ||
| description: | ||
| 'JSON array of event objects, one per funnel step in order, e.g. [{"event_type":"signup"},{"event_type":"purchase"}]', | ||
| }, | ||
| start: { | ||
| type: 'string', | ||
| required: true, | ||
| visibility: 'user-or-llm', | ||
| description: 'Start date in YYYYMMDD format', | ||
| }, | ||
| end: { | ||
| type: 'string', | ||
| required: true, | ||
| visibility: 'user-or-llm', | ||
| description: 'End date in YYYYMMDD format', | ||
| }, | ||
| mode: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'Funnel ordering: "ordered", "unordered", or "sequential" (default: ordered)', | ||
| }, | ||
| userType: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'User type: "new" or "active" (default: active)', | ||
| }, | ||
| interval: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: | ||
| 'Time interval: -300000 (real-time), -3600000 (hourly), 1 (daily), 7 (weekly), or 30 (monthly)', | ||
| }, | ||
| conversionWindowSeconds: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'Conversion window in seconds (default: 2592000, i.e. 30 days)', | ||
| }, | ||
| groupBy: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'Property to group by (limit: one; prefix custom properties with "gp:")', | ||
| }, | ||
| limit: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'Maximum number of group-by values (default: 100, max: 1000)', | ||
| }, | ||
| segment: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'JSON segment definition(s) applied to the query', | ||
| }, | ||
| dataResidency: { | ||
| type: 'string', | ||
| required: false, | ||
| visibility: 'user-or-llm', | ||
| description: 'Data residency region: "us" (default) or "eu"', | ||
| }, | ||
| }, | ||
|
|
||
| request: { | ||
| url: (params) => { | ||
| const url = new URL(`${getDashboardHost(params.dataResidency)}/api/2/funnels`) | ||
| let parsed: unknown | ||
| try { | ||
| parsed = JSON.parse(params.events) | ||
| } catch { | ||
| throw new Error('Amplitude Funnels: "events" must be a valid JSON array of event objects') | ||
| } | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| const isPlainObject = (value: unknown): value is Record<string, unknown> => | ||
| Boolean(value) && typeof value === 'object' && !Array.isArray(value) | ||
|
|
||
| if (!Array.isArray(parsed) || parsed.length === 0 || !parsed.every(isPlainObject)) { | ||
| throw new Error( | ||
| 'Amplitude Funnels: "events" must be a non-empty JSON array of event objects' | ||
| ) | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| } | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| for (const step of parsed) { | ||
| url.searchParams.append('e', JSON.stringify(step)) | ||
| } | ||
|
waleedlatif1 marked this conversation as resolved.
waleedlatif1 marked this conversation as resolved.
|
||
| url.searchParams.set('start', params.start) | ||
| url.searchParams.set('end', params.end) | ||
| if (params.mode) url.searchParams.set('mode', params.mode) | ||
| if (params.userType) url.searchParams.set('n', params.userType) | ||
| if (params.interval) url.searchParams.set('i', params.interval) | ||
| if (params.conversionWindowSeconds) url.searchParams.set('cs', params.conversionWindowSeconds) | ||
| if (params.groupBy) url.searchParams.set('g', params.groupBy) | ||
| if (params.limit) url.searchParams.set('limit', params.limit) | ||
| if (params.segment) url.searchParams.set('s', params.segment) | ||
| return url.toString() | ||
| }, | ||
| method: 'GET', | ||
| headers: (params) => ({ | ||
| Authorization: `Basic ${btoa(`${params.apiKey}:${params.secretKey}`)}`, | ||
| }), | ||
| }, | ||
|
|
||
| transformResponse: async (response: Response) => { | ||
| const data = await response.json() | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(data.error || `Amplitude Funnels API error: ${response.status}`) | ||
| } | ||
|
|
||
| const results = (Array.isArray(data.data) ? data.data : []) as Array<Record<string, unknown>> | ||
|
|
||
| const funnels = results.map((r) => { | ||
|
waleedlatif1 marked this conversation as resolved.
|
||
| const dayFunnels = r.dayFunnels as Record<string, unknown> | undefined | ||
| return { | ||
| stepByStep: (r.stepByStep as number[]) ?? [], | ||
| cumulative: (r.cumulative as number[]) ?? [], | ||
| cumulativeRaw: (r.cumulativeRaw as number[]) ?? [], | ||
| medianTransTimes: (r.medianTransTimes as number[]) ?? [], | ||
| avgTransTimes: (r.avgTransTimes as number[]) ?? [], | ||
| events: (r.events as string[]) ?? [], | ||
| dayFunnels: dayFunnels | ||
| ? { | ||
| series: (dayFunnels.series as number[][]) ?? [], | ||
| xValues: (dayFunnels.xValues as string[]) ?? [], | ||
| } | ||
| : null, | ||
| } | ||
| }) | ||
|
|
||
| return { | ||
| success: true, | ||
| output: { funnels }, | ||
| } | ||
| }, | ||
|
|
||
| outputs: { | ||
| funnels: { | ||
| type: 'array', | ||
| description: 'Funnel results, one entry per segment', | ||
| items: { | ||
| type: 'object', | ||
| properties: { | ||
| stepByStep: { type: 'json', description: 'Conversion count at each step' }, | ||
| cumulative: { | ||
| type: 'json', | ||
| description: 'Cumulative conversion percentage at each step', | ||
| }, | ||
| cumulativeRaw: { type: 'json', description: 'Cumulative conversion count at each step' }, | ||
| medianTransTimes: { | ||
| type: 'json', | ||
| description: 'Median transition time between steps (ms)', | ||
| }, | ||
| avgTransTimes: { | ||
| type: 'json', | ||
| description: 'Average transition time between steps (ms)', | ||
| }, | ||
| events: { type: 'json', description: 'Event names for each funnel step' }, | ||
| dayFunnels: { | ||
| type: 'json', | ||
| description: 'Daily funnel breakdown {series, xValues}', | ||
| optional: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.