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