import {z} from 'zod' export type TopVideoPinsSortBy = 'SAVE' | 'IMPRESSION' | 'OUTBOUND_CLICK' | 'VIDEO_MRC_VIEW' | 'VIDEO_AVG_WATCH_TIME' | 'VIDEO_V50_WATCH_TIME' | 'QUARTILE_95_PERCENT_VIEW' | 'VIDEO_10S_VIEW' | 'VIDEO_START'; export type QueryvideopinmetrictypesItems = 'IMPRESSION' | 'SAVE' | 'VIDEO_MRC_VIEW' | 'VIDEO_AVG_WATCH_TIME' | 'VIDEO_V50_WATCH_TIME' | 'QUARTILE_95_PERCENT_VIEW' | 'VIDEO_10S_VIEW' | 'VIDEO_START' | 'OUTBOUND_CLICK'; export type GetApiUserAccountAnalyticsTopVideoPinsArgs = {start_date: string, end_date: string, sort_by: TopVideoPinsSortBy, from_claimed_content?: ('OTHER' | 'CLAIMED' | 'BOTH') | undefined, pin_format?: ('ALL' | 'ORGANIC_IMAGE' | 'ORGANIC_PRODUCT' | 'ORGANIC_VIDEO' | 'ADS_STANDARD' | 'ADS_PRODUCT' | 'ADS_VIDEO' | 'ADS_IDEA') | undefined, app_types?: ('ALL' | 'MOBILE' | 'TABLET' | 'WEB') | undefined, content_type?: ('ALL' | 'PAID' | 'ORGANIC') | undefined, source?: ('ALL' | 'YOUR_PINS' | 'OTHER_PINS') | undefined, metric_types?: Array | undefined, num_of_pins?: number | undefined, created_in_last_n_days?: number | undefined, ad_account_id?: string | undefined}; export type GetApiUserAccountAnalyticsTopVideoPinsOptions = {baseUrl?: string | undefined, pinterestOauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const topVideoPinsAnalyticsResponseDateAvailability = z.object({is_realtime: z.boolean().optional(), latest_available_timestamp: z.number().optional()}); export const dataStatus = z.enum(["PROCESSING", "READY", "ESTIMATE", "BEFORE_BUSINESS_CREATED", "BEFORE_DATA_RETENTION_PERIOD", "BEFORE_PIN_DATA_RETENTION_PERIOD", "BEFORE_METRIC_START_DATE", "BEFORE_CORE_METRIC_START_DATE", "BEFORE_PIN_FORMAT_METRIC_START_DATE", "BEFORE_AUDIENCE_METRIC_START_DATE", "BEFORE_AUDIENCE_MONTHLY_METRIC_START_DATE", "BEFORE_VIDEO_METRIC_START_DATE", "BEFORE_CONVERSION_METRIC_START_DATE", "PURCHASERS_METRIC_SMALLER_THAN_THRESHOLD", "IN_BAD_TAG_DATE", "BEFORE_PUBLISHED_METRIC_START_DATE", "BEFORE_ASSIST_METRIC_START_DATE", "BEFORE_PIN_CREATED", "BEFORE_ACCOUNT_CLAIMED", "BEFORE_DEMOGRAPHIC_FILTERS_START_DATE", "AUDIENCE_SEGMENT_SMALLER_THAN_THRESHOLD", "AUDIENCE_TOTAL_SMALLER_THAN_THRESHOLD", "BEFORE_PRODUCT_GROUP_FILTER_START_DATE"]); export const topVideoPinsAnalyticsResponsePinsItems = z.object({data_status: z.record(z.string(), dataStatus).optional(), metrics: z.record(z.string(), z.number()).optional(), pin_id: z.string().optional()}); export const topVideoPinsSortBy = z.enum(["SAVE", "IMPRESSION", "OUTBOUND_CLICK", "VIDEO_MRC_VIEW", "VIDEO_AVG_WATCH_TIME", "VIDEO_V50_WATCH_TIME", "QUARTILE_95_PERCENT_VIEW", "VIDEO_10S_VIEW", "VIDEO_START"]); export const topVideoPinsAnalyticsResponse = z.object({date_availability: topVideoPinsAnalyticsResponseDateAvailability.optional(), pins: z.array(topVideoPinsAnalyticsResponsePinsItems).optional(), sort_by: topVideoPinsSortBy.optional()}); 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 user account top video pins analytics * * GET /user_account/analytics/top_video_pins * * Also accepts auth: client_credentials */ export const getApiUserAccountAnalyticsTopVideoPins = async (args: GetApiUserAccountAnalyticsTopVideoPinsArgs, options?: GetApiUserAccountAnalyticsTopVideoPinsOptions) => { const {baseUrl = "https://api.pinterest.com/v5", fetch: fetchFn = fetch, pinterestOauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.from_claimed_content != null) query.append("from_claimed_content", String(args.from_claimed_content)) if (args.pin_format != null) query.append("pin_format", String(args.pin_format)) if (args.app_types != null) query.append("app_types", String(args.app_types)) if (args.content_type != null) query.append("content_type", String(args.content_type)) if (args.source != null) query.append("source", String(args.source)) if (args.metric_types != null) query.append("metric_types", args.metric_types.join(',')) if (args.num_of_pins != null) query.append("num_of_pins", String(args.num_of_pins)) if (args.created_in_last_n_days != null) query.append("created_in_last_n_days", String(args.created_in_last_n_days)) if (args.ad_account_id != null) query.append("ad_account_id", String(args.ad_account_id)) if (pinterestOauth2 !== undefined) headers.set('authorization', `Bearer ${pinterestOauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/user_account/analytics/top_video_pins${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(topVideoPinsAnalyticsResponse, await res.json()) };