1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get Pin analytics |
7 | * Get analytics for a Pin owned by the "operation user_account" - or on a group board that has been shared with this account. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | pin_id: string, |
12 | start_date: string | undefined, |
13 | end_date: string | undefined, |
14 | app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined, |
15 | metric_types: string | undefined, |
16 | split_field: "NO_SPLIT" | "APP_TYPE" | undefined, |
17 | ad_account_id: string | undefined, |
18 | ) { |
19 | const url = new URL(`https://api.pinterest.com/v5/pins/${pin_id}/analytics`); |
20 | for (const [k, v] of [ |
21 | ["start_date", start_date], |
22 | ["end_date", end_date], |
23 | ["app_types", app_types], |
24 | ["metric_types", metric_types], |
25 | ["split_field", split_field], |
26 | ["ad_account_id", ad_account_id], |
27 | ]) { |
28 | if (v !== undefined && v !== "" && k !== undefined) { |
29 | url.searchParams.append(k, v); |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: "GET", |
34 | headers: { |
35 | Authorization: "Bearer " + auth.token, |
36 | }, |
37 | body: undefined, |
38 | }); |
39 | if (!response.ok) { |
40 | const text = await response.text(); |
41 | throw new Error(`${response.status} ${text}`); |
42 | } |
43 | return await response.json(); |
44 | } |
45 |
|