export type CreateApiTrackArgs = {ip?: number | undefined, verbose?: number | undefined, img?: number | undefined, callback?: string | undefined, body: Array<{event: string, properties: {token?: string | undefined, time?: number | undefined, distinct_id?: string | undefined, $insert_id?: string | undefined} | {[key: string]: unknown}}>}; export type CreateApiTrackOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Track Events * * POST /track */ export const createApiTrack = async (args: CreateApiTrackArgs, options?: CreateApiTrackOptions) => { const {baseUrl = "https://mixpanel.com/api/query", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/plain") if (args.ip != null) query.append("ip", String(args.ip)) if (args.verbose != null) query.append("verbose", String(args.verbose)) if (args.img != null) query.append("img", String(args.img)) if (args.callback != null) query.append("callback", String(args.callback)) const url = `${baseUrl.replace(/\/+$/, '')}/track${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 res.text() };