1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get ad analytics |
7 | * Get analytics for the specified ads 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_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 | pin_ids: string | undefined, |
25 | campaign_ids: string | undefined, |
26 | ) { |
27 | const url = new URL( |
28 | `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/ads/analytics`, |
29 | ); |
30 | for (const [k, v] of [ |
31 | ["start_date", start_date], |
32 | ["end_date", end_date], |
33 | ["ad_ids", ad_ids], |
34 | ["columns", columns], |
35 | ["granularity", granularity], |
36 | ["click_window_days", click_window_days], |
37 | ["engagement_window_days", engagement_window_days], |
38 | ["view_window_days", view_window_days], |
39 | ["conversion_report_time", conversion_report_time], |
40 | ["pin_ids", pin_ids], |
41 | ["campaign_ids", campaign_ids], |
42 | ]) { |
43 | if (v !== undefined && v !== "" && k !== undefined) { |
44 | url.searchParams.append(k, v); |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: "GET", |
49 | headers: { |
50 | Authorization: "Bearer " + auth.token, |
51 | }, |
52 | body: undefined, |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|