0

Get marketing metrics report

by
Published Oct 17, 2025

Get the marketing metrics from an accounting source for a given company. Request an Excel report for download.

Script codat Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Codat = {
3
	encodedKey: string
4
}
5
/**
6
 * Get marketing metrics report
7
 * Get the marketing metrics from an accounting source for a given company.
8

9
Request an Excel report for download.
10
 */
11
export async function main(
12
	auth: Codat,
13
	companyId: string,
14
	connectionId: string,
15
	reportDate: string | undefined,
16
	periodLength: string | undefined,
17
	numberOfPeriods: string | undefined,
18
	periodUnit: 'Day' | 'Week' | 'Month' | 'Year' | undefined,
19
	includeDisplayNames: string | undefined,
20
	showInputValues: string | undefined
21
) {
22
	const url = new URL(
23
		`https://api.codat.io/data/companies/${companyId}/connections/${connectionId}/assess/accountingMetrics/marketing`
24
	)
25
	for (const [k, v] of [
26
		['reportDate', reportDate],
27
		['periodLength', periodLength],
28
		['numberOfPeriods', numberOfPeriods],
29
		['periodUnit', periodUnit],
30
		['includeDisplayNames', includeDisplayNames],
31
		['showInputValues', showInputValues]
32
	]) {
33
		if (v !== undefined && v !== '' && k !== undefined) {
34
			url.searchParams.append(k, v)
35
		}
36
	}
37

38
	const response = await fetch(url, {
39
		method: 'GET',
40
		headers: {
41
			Authorization: `Basic ${auth.encodedKey}`
42
		},
43
		body: undefined
44
	})
45
	if (!response.ok) {
46
		const text = await response.text()
47
		throw new Error(`${response.status} ${text}`)
48
	}
49
	return await response.json()
50
}
51