import {z} from 'zod' export type CartDataItem = {itemId?: string | undefined, quantity?: number | undefined, unitPrice?: number | undefined}; export type CartData = {items?: Array | undefined, merchantFeedLabel?: string | undefined, merchantFeedLanguage?: string | undefined, merchantId?: unknown | undefined}; export type CustomFloodlightVariable = {kind?: string | undefined, type?: ('U1' | 'U2' | 'U3' | 'U4' | 'U5' | 'U6' | 'U7' | 'U8' | 'U9' | 'U10' | 'U11' | 'U12' | 'U13' | 'U14' | 'U15' | 'U16' | 'U17' | 'U18' | 'U19' | 'U20' | 'U21' | 'U22' | 'U23' | 'U24' | 'U25' | 'U26' | 'U27' | 'U28' | 'U29' | 'U30' | 'U31' | 'U32' | 'U33' | 'U34' | 'U35' | 'U36' | 'U37' | 'U38' | 'U39' | 'U40' | 'U41' | 'U42' | 'U43' | 'U44' | 'U45' | 'U46' | 'U47' | 'U48' | 'U49' | 'U50' | 'U51' | 'U52' | 'U53' | 'U54' | 'U55' | 'U56' | 'U57' | 'U58' | 'U59' | 'U60' | 'U61' | 'U62' | 'U63' | 'U64' | 'U65' | 'U66' | 'U67' | 'U68' | 'U69' | 'U70' | 'U71' | 'U72' | 'U73' | 'U74' | 'U75' | 'U76' | 'U77' | 'U78' | 'U79' | 'U80' | 'U81' | 'U82' | 'U83' | 'U84' | 'U85' | 'U86' | 'U87' | 'U88' | 'U89' | 'U90' | 'U91' | 'U92' | 'U93' | 'U94' | 'U95' | 'U96' | 'U97' | 'U98' | 'U99' | 'U100') | undefined, value?: string | undefined}; export type OfflineUserAddressInfo = {city?: string | undefined, countryCode?: string | undefined, hashedFirstName?: string | undefined, hashedLastName?: string | undefined, hashedStreetAddress?: string | undefined, postalCode?: string | undefined, state?: string | undefined}; export type UserIdentifier = {addressInfo?: OfflineUserAddressInfo | undefined, hashedEmail?: string | undefined, hashedPhoneNumber?: string | undefined}; export type Conversion = {adUserDataConsent?: ('GRANTED' | 'DENIED') | undefined, cartData?: CartData | undefined, childDirectedTreatment?: boolean | undefined, customVariables?: Array | undefined, dclid?: string | undefined, encryptedUserId?: string | undefined, encryptedUserIdCandidates?: Array | undefined, floodlightActivityId?: unknown | undefined, floodlightConfigurationId?: unknown | undefined, gclid?: string | undefined, impressionId?: string | undefined, kind?: string | undefined, limitAdTracking?: boolean | undefined, matchId?: string | undefined, mobileDeviceId?: string | undefined, nonPersonalizedAd?: boolean | undefined, ordinal?: string | undefined, quantity?: unknown | undefined, sessionAttributesEncoded?: string | undefined, timestampMicros?: unknown | undefined, treatmentForUnderage?: boolean | undefined, userIdentifiers?: Array | undefined, value?: number | undefined}; export type EncryptionInfo = {encryptionEntityId?: unknown | undefined, encryptionEntityType?: ('ENCRYPTION_ENTITY_TYPE_UNKNOWN' | 'DCM_ACCOUNT' | 'DCM_ADVERTISER' | 'DBM_PARTNER' | 'DBM_ADVERTISER' | 'ADWORDS_CUSTOMER' | 'DFP_NETWORK_CODE') | undefined, encryptionSource?: ('ENCRYPTION_SCOPE_UNKNOWN' | 'AD_SERVING' | 'DATA_TRANSFER') | undefined, kind?: string | undefined}; export type ConversionsBatchInsertRequest = {conversions?: Array | undefined, encryptionInfo?: EncryptionInfo | undefined, kind?: string | undefined}; export type CreateApiUserprofilesProfileIdConversionsBatchinsertArgs = {profileId: unknown, body: ConversionsBatchInsertRequest}; export type CreateApiUserprofilesProfileIdConversionsBatchinsertOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const cartDataItem = z.object({itemId: z.string().optional(), quantity: z.number().int().optional(), unitPrice: z.number().optional()}); export const cartData = z.object({items: z.array(cartDataItem).optional(), merchantFeedLabel: z.string().optional(), merchantFeedLanguage: z.string().optional(), merchantId: z.string().optional()}); export const customFloodlightVariable = z.object({kind: z.string().optional(), type: z.enum(["U1", "U2", "U3", "U4", "U5", "U6", "U7", "U8", "U9", "U10", "U11", "U12", "U13", "U14", "U15", "U16", "U17", "U18", "U19", "U20", "U21", "U22", "U23", "U24", "U25", "U26", "U27", "U28", "U29", "U30", "U31", "U32", "U33", "U34", "U35", "U36", "U37", "U38", "U39", "U40", "U41", "U42", "U43", "U44", "U45", "U46", "U47", "U48", "U49", "U50", "U51", "U52", "U53", "U54", "U55", "U56", "U57", "U58", "U59", "U60", "U61", "U62", "U63", "U64", "U65", "U66", "U67", "U68", "U69", "U70", "U71", "U72", "U73", "U74", "U75", "U76", "U77", "U78", "U79", "U80", "U81", "U82", "U83", "U84", "U85", "U86", "U87", "U88", "U89", "U90", "U91", "U92", "U93", "U94", "U95", "U96", "U97", "U98", "U99", "U100"]).optional(), value: z.string().optional()}); export const offlineUserAddressInfo = z.object({city: z.string().optional(), countryCode: z.string().optional(), hashedFirstName: z.string().optional(), hashedLastName: z.string().optional(), hashedStreetAddress: z.string().optional(), postalCode: z.string().optional(), state: z.string().optional()}); export const userIdentifier = z.object({addressInfo: offlineUserAddressInfo.optional(), hashedEmail: z.string().optional(), hashedPhoneNumber: z.string().optional()}); export const conversion = z.object({adUserDataConsent: z.enum(["GRANTED", "DENIED"]).optional(), cartData: cartData.optional(), childDirectedTreatment: z.boolean().optional(), customVariables: z.array(customFloodlightVariable).optional(), dclid: z.string().optional(), encryptedUserId: z.string().optional(), encryptedUserIdCandidates: z.array(z.string()).optional(), floodlightActivityId: z.string().optional(), floodlightConfigurationId: z.string().optional(), gclid: z.string().optional(), impressionId: z.string().optional(), kind: z.string().optional(), limitAdTracking: z.boolean().optional(), matchId: z.string().optional(), mobileDeviceId: z.string().optional(), nonPersonalizedAd: z.boolean().optional(), ordinal: z.string().optional(), quantity: z.string().optional(), sessionAttributesEncoded: z.string().optional(), timestampMicros: z.string().optional(), treatmentForUnderage: z.boolean().optional(), userIdentifiers: z.array(userIdentifier).optional(), value: z.number().optional()}); export const conversionError = z.object({code: z.enum(["INVALID_ARGUMENT", "INTERNAL", "PERMISSION_DENIED", "NOT_FOUND"]).optional(), kind: z.string().optional(), message: z.string().optional()}); export const conversionStatus = z.object({conversion: conversion.optional(), errors: z.array(conversionError).optional(), kind: z.string().optional()}); export const conversionsBatchInsertResponse = z.object({hasFailures: z.boolean().optional(), kind: z.string().optional(), status: z.array(conversionStatus).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 }; /** * Inserts conversions. * * POST /userprofiles/{profileId}/conversions/batchinsert */ export const createApiUserprofilesProfileIdConversionsBatchinsert = async (args: CreateApiUserprofilesProfileIdConversionsBatchinsertArgs, options?: CreateApiUserprofilesProfileIdConversionsBatchinsertOptions) => { const {baseUrl = "https://dfareporting.googleapis.com/dfareporting/v5", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/userprofiles/${encodeURIComponent(String(args.profileId))}/conversions/batchinsert${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(conversionsBatchInsertResponse, await res.json()) };