0

Get available metrics' definitions

by
Published Dec 20, 2024

Get the definitions for ads and organic metrics available across both synchronous and asynchronous report endpoints. The `display_name` attribute will match how the metric is named in our native tools like Ads Manager. See Organic Analytics and Ads Analytics for more information.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Get available metrics' definitions
7
 * Get the definitions for ads and organic metrics available across both synchronous and asynchronous report endpoints.
8
The `display_name` attribute will match how the metric is named in our native tools like Ads Manager.
9
See Organic Analytics and Ads Analytics for more information.
10
 */
11
export async function main(
12
  auth: Pinterest,
13
  report_type: "SYNC" | "ASYNC" | undefined,
14
) {
15
  const url = new URL(
16
    `https://api.pinterest.com/v5/resources/delivery_metrics`,
17
  );
18
  for (const [k, v] of [["report_type", report_type]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36