import {z} from 'zod' export type CreateApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesArgs = {experienceId: string, body: {startTs?: string | undefined, endTs?: string | undefined, granularity?: ('hour' | 'day' | 'week' | 'month') | undefined, mode?: ('cumulative' | 'noncumulative') | undefined, metrics?: (Array<'conversion_rate' | 'net_revenue_per_visitor' | 'gross_profit_per_visitor' | 'aov' | 'n_visitors' | 'n_orders' | 'net_revenue'>) | undefined, filters?: ({deviceType?: ('any' | 'mobile' | 'desktop' | null) | undefined, visitorType?: ('any' | 'new' | 'returning' | null) | undefined, customerType?: ('new' | 'returning' | null) | undefined, sourceSitesOrChannels?: (Array<'Instagram' | 'Facebook' | 'Google' | 'Twitter' | 'TikTok' | 'Youtube' | 'Klaviyo' | 'Attentive' | 'Postscript' | 'Paid Shopping' | 'Paid Search' | 'Paid Social' | 'Paid Video' | 'Display' | 'Paid Other' | 'Organic Shopping' | 'Organic Social' | 'Organic Video' | 'Organic Search' | 'Other' | 'Referral' | 'Affiliate' | 'SMS' | 'Email' | 'Direct'>) | undefined, urlParam?: ({queryParam: string | null, queryParamValue: string | null, queryParamFilterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | null}) | undefined, landingPage?: ({landingPageUrlPath: string | null, landingPageUrlPathFilterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | null} | null) | undefined, landingPageFilters?: (Array<{values?: Array | undefined, filterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull'}>) | undefined, landingPageFiltersOperator?: ('or' | 'and') | undefined, userBehavior?: ({viewedCheckout?: (boolean | null) | undefined, addedToCart?: (boolean | null) | undefined, viewedProductPage?: (boolean | null) | undefined, viewedCollectionPage?: (boolean | null) | undefined}) | undefined, countryCodes?: Array | undefined, countryCodesFilterType?: ('include' | 'exclude') | undefined, maxZScore?: (number | string | null) | undefined, triggeredCustomEvents?: Array | undefined, customEventsFilterType?: ('any' | 'all' | 'none') | undefined, reachedCheckoutStage?: ('started' | 'contact_info_submitted' | 'address_info_submitted' | null) | undefined, minNetRevenue?: (number | string | null) | undefined, maxNetRevenue?: (number | string | null) | undefined, minShippingRevenue?: (number | string | null) | undefined, maxShippingRevenue?: (number | string | null) | undefined, shippingMethods?: Array | undefined, onlyProductIds?: (Array | null) | undefined, landingPageTypes?: (Array<'PDP' | 'Collection' | 'Content' | 'Blog' | 'Homepage' | 'Search' | 'Cart' | 'Other'>) | undefined, experimentProductsOnly?: (boolean | null) | undefined, onlyProductHandles?: (Array | null) | undefined}) | undefined}}; export type CreateApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesResponse = z.object({currency: z.string(), granularity: z.enum(["hour", "day", "week", "month"]), mode: z.enum(["cumulative", "noncumulative"]), experienceId: z.string(), experienceName: z.string(), variations: z.array(z.object({id: z.string(), name: z.string(), isControl: z.boolean()})), cogsConfigured: z.boolean().optional(), cogsCoveragePct: z.number().optional(), segments: z.record(z.string(), z.object({data: z.array(z.object({dt: z.string()}).and(z.record(z.string(), z.number().nullable())))}))}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get experience timeseries * * POST /v25-10-beta/analytics/experience/{experienceId}/timeseries */ export const createApiV2510BetaAnalyticsExperienceExperienceIdTimeseries = async (args: CreateApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesArgs, options?: CreateApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesOptions) => { const {baseUrl = "https://api.intelligems.io", fetch: fetchFn = fetch, intelligemsAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (intelligemsAccessToken !== undefined) headers.set("intelligems-access-token", intelligemsAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v25-10-beta/analytics/experience/${encodeURIComponent(String(args.experienceId))}/timeseries${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2510BetaAnalyticsExperienceExperienceIdTimeseriesResponse, await res.json()) };