1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get ad account analytics |
7 | * Get analytics for the specified ad_account_id, filtered by the specified options. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | ad_account_id: string, |
12 | start_date: string | undefined, |
13 | end_date: string | undefined, |
14 | columns: string | undefined, |
15 | granularity: "TOTAL" | "DAY" | "HOUR" | "WEEK" | "MONTH" | undefined, |
16 | click_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
17 | engagement_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
18 | view_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
19 | conversion_report_time: |
20 | | "TIME_OF_AD_ACTION" |
21 | | "TIME_OF_CONVERSION" |
22 | | undefined, |
23 | ) { |
24 | const url = new URL( |
25 | `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/analytics`, |
26 | ); |
27 | for (const [k, v] of [ |
28 | ["start_date", start_date], |
29 | ["end_date", end_date], |
30 | ["columns", columns], |
31 | ["granularity", granularity], |
32 | ["click_window_days", click_window_days], |
33 | ["engagement_window_days", engagement_window_days], |
34 | ["view_window_days", view_window_days], |
35 | ["conversion_report_time", conversion_report_time], |
36 | ]) { |
37 | if (v !== undefined && v !== "" && k !== undefined) { |
38 | url.searchParams.append(k, v); |
39 | } |
40 | } |
41 | const response = await fetch(url, { |
42 | method: "GET", |
43 | headers: { |
44 | Authorization: "Bearer " + auth.token, |
45 | }, |
46 | body: undefined, |
47 | }); |
48 | if (!response.ok) { |
49 | const text = await response.text(); |
50 | throw new Error(`${response.status} ${text}`); |
51 | } |
52 | return await response.json(); |
53 | } |
54 |
|