0

Get targeting options

by
Published Dec 20, 2024

You can use targeting values in ads placement to define your intended audience.

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 targeting options
7
 * You can use targeting values in ads placement to define your intended audience.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  targeting_type:
12
    | "APPTYPE"
13
    | "GENDER"
14
    | "LOCALE"
15
    | "AGE_BUCKET"
16
    | "LOCATION"
17
    | "GEO"
18
    | "INTEREST"
19
    | "KEYWORD"
20
    | "AUDIENCE_INCLUDE"
21
    | "AUDIENCE_EXCLUDE",
22
  client_id: string | undefined,
23
  oauth_signature: string | undefined,
24
  timestamp: string | undefined,
25
  ad_account_id: string | undefined,
26
) {
27
  const url = new URL(
28
    `https://api.pinterest.com/v5/resources/targeting/${targeting_type}`,
29
  );
30
  for (const [k, v] of [
31
    ["client_id", client_id],
32
    ["oauth_signature", oauth_signature],
33
    ["timestamp", timestamp],
34
    ["ad_account_id", ad_account_id],
35
  ]) {
36
    if (v !== undefined && v !== "" && k !== undefined) {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Bearer " + auth.token,
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53