import { tallySchema } from "@automate.ax/integration-contracts/tally" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getTallyApi, TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT, TALLY_ID_SCHEMA, } from "../lib" const ANALYTICS_INPUT_SCHEMA = z.object({ formId: TALLY_ID_SCHEMA, period: z.enum([ "today", "yesterday", "24h", "7d", "30d", "3m", "6m", "12m", "all", ]), }) /** * Builds one period-scoped Tally analytics action. * * @param name Action display name. * @param description Action behavior. * @param path Analytics endpoint suffix. * @param responseSchema Frozen response schema name. */ function analyticsAction< TName extends | "GetFormsFormIdAnalyticsDimensionsResponse" | "GetFormsFormIdAnalyticsDropOffResponse" | "GetFormsFormIdAnalyticsMetricsResponse" | "GetFormsFormIdAnalyticsSubmissionsResponse" | "GetFormsFormIdAnalyticsVisitsResponse", >(name: string, description: string, path: string, responseSchema: TName) { return defineAction(name) .describe(description) .account(TALLY_ACCOUNT, TALLY_API_KEY_ACCOUNT) .input(ANALYTICS_INPUT_SCHEMA) .output(tallySchema(responseSchema)) .retry({ replaySafety: "safe" }) .handler(({ account, input }) => getTallyApi(account).request( `forms/${encodeURIComponent(input.formId)}/analytics/${path}`, { query: { period: input.period }, responseSchema }, ), ) } /** Gets aggregate traffic and completion metrics for one Tally form. */ export const getTallyFormMetrics = analyticsAction( "Get Tally form metrics", "Gets aggregate traffic, response, and completion metrics for one period.", "metrics", "GetFormsFormIdAnalyticsMetricsResponse", ) /** Gets Tally form visits grouped into time intervals. */ export const getTallyFormVisits = analyticsAction( "Get Tally form visits", "Gets form visit totals grouped into provider-selected time intervals.", "visits", "GetFormsFormIdAnalyticsVisitsResponse", ) /** Gets completed and partial Tally submissions over time. */ export const getTallyFormSubmissionAnalytics = analyticsAction( "Get Tally form submission analytics", "Gets completed and partial submission counts over time.", "submissions", "GetFormsFormIdAnalyticsSubmissionsResponse", ) /** Gets Tally form visitors by source, browser, device, and location. */ export const getTallyFormDimensions = analyticsAction( "Get Tally form dimensions", "Gets visitor breakdowns by source, browser, operating system, device, and location.", "dimensions", "GetFormsFormIdAnalyticsDimensionsResponse", ) /** Gets per-question Tally form drop-off statistics. */ export const getTallyFormDropOff = analyticsAction( "Get Tally form drop-off", "Gets form funnel and per-question answer and drop-off rates.", "drop-off", "GetFormsFormIdAnalyticsDropOffResponse", )