0

Get user account analytics

by
Published Dec 20, 2024

Get analytics for the "operation user_account" - 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 analytics
7
 * Get analytics for the "operation user_account"
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
  from_claimed_content: "OTHER" | "CLAIMED" | "BOTH" | undefined,
17
  pin_format:
18
    | "ALL"
19
    | "ORGANIC_IMAGE"
20
    | "ORGANIC_PRODUCT"
21
    | "ORGANIC_VIDEO"
22
    | "ADS_STANDARD"
23
    | "ADS_PRODUCT"
24
    | "ADS_VIDEO"
25
    | "ADS_IDEA"
26
    | undefined,
27
  app_types: "ALL" | "MOBILE" | "TABLET" | "WEB" | undefined,
28
  content_type: "ALL" | "PAID" | "ORGANIC" | undefined,
29
  source: "ALL" | "YOUR_PINS" | "OTHER_PINS" | undefined,
30
  metric_types: string | undefined,
31
  split_field:
32
    | "NO_SPLIT"
33
    | "APP_TYPE"
34
    | "OWNED_CONTENT"
35
    | "SOURCE"
36
    | "PIN_FORMAT"
37
    | undefined,
38
  ad_account_id: string | undefined,
39
) {
40
  const url = new URL(`https://api.pinterest.com/v5/user_account/analytics`);
41
  for (const [k, v] of [
42
    ["start_date", start_date],
43
    ["end_date", end_date],
44
    ["from_claimed_content", from_claimed_content],
45
    ["pin_format", pin_format],
46
    ["app_types", app_types],
47
    ["content_type", content_type],
48
    ["source", source],
49
    ["metric_types", metric_types],
50
    ["split_field", split_field],
51
    ["ad_account_id", ad_account_id],
52
  ]) {
53
    if (v !== undefined && v !== "" && k !== undefined) {
54
      url.searchParams.append(k, v);
55
    }
56
  }
57
  const response = await fetch(url, {
58
    method: "GET",
59
    headers: {
60
      Authorization: "Bearer " + auth.token,
61
    },
62
    body: undefined,
63
  });
64
  if (!response.ok) {
65
    const text = await response.text();
66
    throw new Error(`${response.status} ${text}`);
67
  }
68
  return await response.json();
69
}
70