0

Get customer retention metrics

by
Published Oct 17, 2025

The *Get customer retention metrics* endpoint returns customer retention insights for a specific company's commerce connection over one or more periods of time.

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 customer retention metrics
7
 * The *Get customer retention metrics* endpoint returns customer retention insights for a specific company's commerce connection over one or more periods of time.
8
 */
9
export async function main(
10
	auth: Codat,
11
	companyId: string,
12
	connectionId: string,
13
	reportDate: string | undefined,
14
	periodLength: string | undefined,
15
	numberOfPeriods: string | undefined,
16
	periodUnit: 'Day' | 'Week' | 'Month' | 'Year' | undefined,
17
	includeDisplayNames: string | undefined
18
) {
19
	const url = new URL(
20
		`https://api.codat.io/data/companies/${companyId}/connections/${connectionId}/assess/commerceMetrics/customerRetention`
21
	)
22
	for (const [k, v] of [
23
		['reportDate', reportDate],
24
		['periodLength', periodLength],
25
		['numberOfPeriods', numberOfPeriods],
26
		['periodUnit', periodUnit],
27
		['includeDisplayNames', includeDisplayNames]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33

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