0

Get attribution metrics for one or more Brevo campaigns or workflows

by
Published Apr 8, 2025
Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get attribution metrics for one or more Brevo campaigns or workflows
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  periodFrom: string | undefined,
12
  periodTo: string | undefined,
13
  emailCampaignId__: string | undefined,
14
  smsCampaignId__: string | undefined,
15
  automationWorkflowEmailId__: string | undefined,
16
  automationWorkflowSmsId__: string | undefined,
17
) {
18
  const url = new URL(`https://api.brevo.com/v3/ecommerce/attribution/metrics`);
19
  for (const [k, v] of [
20
    ["periodFrom", periodFrom],
21
    ["periodTo", periodTo],
22
    ["emailCampaignId[]", emailCampaignId__],
23
    ["smsCampaignId[]", smsCampaignId__],
24
    ["automationWorkflowEmailId[]", automationWorkflowEmailId__],
25
    ["automationWorkflowSmsId[]", automationWorkflowSmsId__],
26
  ]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      "api-key": auth.apiKey,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44