1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get multiple Pin analytics |
7 | * This endpoint is currently in beta and not available to all apps. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | pin_ids: string | undefined, |
12 | start_date: string | undefined, |
13 | end_date: string | undefined, |
14 | app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined, |
15 | metric_types: string | undefined, |
16 | ad_account_id: string | undefined, |
17 | ) { |
18 | const url = new URL(`https://api.pinterest.com/v5/pins/analytics`); |
19 | for (const [k, v] of [ |
20 | ["pin_ids", pin_ids], |
21 | ["start_date", start_date], |
22 | ["end_date", end_date], |
23 | ["app_types", app_types], |
24 | ["metric_types", metric_types], |
25 | ["ad_account_id", ad_account_id], |
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 | Authorization: "Bearer " + auth.token, |
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 |
|