1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get user account top video pins analytics |
7 | * Gets analytics data about a user's top video pins (limited to the top 50). |
8 | - By default, the "operation user_account" is the token user_account. |
9 |
|
10 | Optional: Business Access: Specify an ad_account_id to use the owner of that ad_account as the "operation user_account". |
11 | */ |
12 | export async function main( |
13 | auth: Pinterest, |
14 | start_date: string | undefined, |
15 | end_date: string | undefined, |
16 | sort_by: |
17 | | "IMPRESSION" |
18 | | "SAVE" |
19 | | "OUTBOUND_CLICK" |
20 | | "VIDEO_MRC_VIEW" |
21 | | "VIDEO_AVG_WATCH_TIME" |
22 | | "VIDEO_V50_WATCH_TIME" |
23 | | "QUARTILE_95_PERCENT_VIEW" |
24 | | "VIDEO_10S_VIEW" |
25 | | "VIDEO_START" |
26 | | undefined, |
27 | from_claimed_content: "OTHER" | "CLAIMED" | "BOTH" | undefined, |
28 | pin_format: |
29 | | "ALL" |
30 | | "ORGANIC_IMAGE" |
31 | | "ORGANIC_PRODUCT" |
32 | | "ORGANIC_VIDEO" |
33 | | "ADS_STANDARD" |
34 | | "ADS_PRODUCT" |
35 | | "ADS_VIDEO" |
36 | | "ADS_IDEA" |
37 | | undefined, |
38 | app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined, |
39 | content_type: "ALL" | "PAID" | "ORGANIC" | undefined, |
40 | source: "ALL" | "YOUR_PINS" | "OTHER_PINS" | undefined, |
41 | metric_types: string | undefined, |
42 | num_of_pins: string | undefined, |
43 | created_in_last_n_days: "30" | undefined, |
44 | ad_account_id: string | undefined, |
45 | ) { |
46 | const url = new URL( |
47 | `https://api.pinterest.com/v5/user_account/analytics/top_video_pins`, |
48 | ); |
49 | for (const [k, v] of [ |
50 | ["start_date", start_date], |
51 | ["end_date", end_date], |
52 | ["sort_by", sort_by], |
53 | ["from_claimed_content", from_claimed_content], |
54 | ["pin_format", pin_format], |
55 | ["app_types", app_types], |
56 | ["content_type", content_type], |
57 | ["source", source], |
58 | ["metric_types", metric_types], |
59 | ["num_of_pins", num_of_pins], |
60 | ["created_in_last_n_days", created_in_last_n_days], |
61 | ["ad_account_id", ad_account_id], |
62 | ]) { |
63 | if (v !== undefined && v !== "" && k !== undefined) { |
64 | url.searchParams.append(k, v); |
65 | } |
66 | } |
67 | const response = await fetch(url, { |
68 | method: "GET", |
69 | headers: { |
70 | Authorization: "Bearer " + auth.token, |
71 | }, |
72 | body: undefined, |
73 | }); |
74 | if (!response.ok) { |
75 | const text = await response.text(); |
76 | throw new Error(`${response.status} ${text}`); |
77 | } |
78 | return await response.json(); |
79 | } |
80 |
|