0

Get user account top pins analytics

by
Published Dec 20, 2024

Gets analytics data about a user's top pins (limited to the top 50). - By default, the "operation user_account" is the token user_account. Optional: Business Access: Specify an ad_account_id to use the owner of that ad_account as the "operation user_account".

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Get user account top pins analytics
7
 * Gets analytics data about a user's top 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
    | "ENGAGEMENT"
18
    | "IMPRESSION"
19
    | "OUTBOUND_CLICK"
20
    | "PIN_CLICK"
21
    | "SAVE"
22
    | undefined,
23
  from_claimed_content: "OTHER" | "CLAIMED" | "BOTH" | undefined,
24
  pin_format:
25
    | "ALL"
26
    | "ORGANIC_IMAGE"
27
    | "ORGANIC_PRODUCT"
28
    | "ORGANIC_VIDEO"
29
    | "ADS_STANDARD"
30
    | "ADS_PRODUCT"
31
    | "ADS_VIDEO"
32
    | "ADS_IDEA"
33
    | undefined,
34
  app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined,
35
  content_type: "ALL" | "PAID" | "ORGANIC" | undefined,
36
  source: "ALL" | "YOUR_PINS" | "OTHER_PINS" | undefined,
37
  metric_types: string | undefined,
38
  num_of_pins: string | undefined,
39
  created_in_last_n_days: "30" | undefined,
40
  ad_account_id: string | undefined,
41
) {
42
  const url = new URL(
43
    `https://api.pinterest.com/v5/user_account/analytics/top_pins`,
44
  );
45
  for (const [k, v] of [
46
    ["start_date", start_date],
47
    ["end_date", end_date],
48
    ["sort_by", sort_by],
49
    ["from_claimed_content", from_claimed_content],
50
    ["pin_format", pin_format],
51
    ["app_types", app_types],
52
    ["content_type", content_type],
53
    ["source", source],
54
    ["metric_types", metric_types],
55
    ["num_of_pins", num_of_pins],
56
    ["created_in_last_n_days", created_in_last_n_days],
57
    ["ad_account_id", ad_account_id],
58
  ]) {
59
    if (v !== undefined && v !== "" && k !== undefined) {
60
      url.searchParams.append(k, v);
61
    }
62
  }
63
  const response = await fetch(url, {
64
    method: "GET",
65
    headers: {
66
      Authorization: "Bearer " + auth.token,
67
    },
68
    body: undefined,
69
  });
70
  if (!response.ok) {
71
    const text = await response.text();
72
    throw new Error(`${response.status} ${text}`);
73
  }
74
  return await response.json();
75
}
76