1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get targeting analytics for campaigns |
7 | * Get targeting analytics for one or more campaigns. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | ad_account_id: string, |
12 | campaign_ids: string | undefined, |
13 | start_date: string | undefined, |
14 | end_date: string | undefined, |
15 | targeting_types: string | undefined, |
16 | columns: string | undefined, |
17 | granularity: "TOTAL" | "DAY" | "HOUR" | "WEEK" | "MONTH" | undefined, |
18 | click_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
19 | engagement_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
20 | view_window_days: "0" | "1" | "7" | "14" | "30" | "60" | undefined, |
21 | conversion_report_time: |
22 | | "TIME_OF_AD_ACTION" |
23 | | "TIME_OF_CONVERSION" |
24 | | undefined, |
25 | attribution_types: "INDIVIDUAL" | "HOUSEHOLD" | undefined, |
26 | ) { |
27 | const url = new URL( |
28 | `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/campaigns/targeting_analytics`, |
29 | ); |
30 | for (const [k, v] of [ |
31 | ["campaign_ids", campaign_ids], |
32 | ["start_date", start_date], |
33 | ["end_date", end_date], |
34 | ["targeting_types", targeting_types], |
35 | ["columns", columns], |
36 | ["granularity", granularity], |
37 | ["click_window_days", click_window_days], |
38 | ["engagement_window_days", engagement_window_days], |
39 | ["view_window_days", view_window_days], |
40 | ["conversion_report_time", conversion_report_time], |
41 | ["attribution_types", attribution_types], |
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 |
|