0

Create audience

by
Published Dec 20, 2024

Create an audience you can use in targeting for specific ad groups. Targeting combines customer information with the ways users interact with Pinterest to help you reach specific groups of users; you can include or exclude specific audience_ids when you create an ad group. For more, see Audience targeting.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Create audience
7
 * Create an audience you can use in targeting for specific ad groups. Targeting combines customer information with
8
the ways users interact with Pinterest to help you reach specific groups of users; you can include or exclude
9
specific audience_ids when you create an ad group. 
10
For more, see Audience targeting.
11
 */
12
export async function main(
13
  auth: Pinterest,
14
  ad_account_id: string,
15
  body: {
16
    ad_account_id?: string;
17
    name?: string;
18
    rule?: {
19
      country?: string;
20
      customer_list_id?: string;
21
      engagement_domain?: string[];
22
      engagement_type?: string;
23
      event?: string;
24
      event_data?: {
25
        currency?:
26
          | "UNK"
27
          | "USD"
28
          | "GBP"
29
          | "CAD"
30
          | "EUR"
31
          | "AUD"
32
          | "NZD"
33
          | "SEK"
34
          | "ILS"
35
          | "CHF"
36
          | "HKD"
37
          | "JPY"
38
          | "SGD"
39
          | "KRW"
40
          | "NOK"
41
          | "DKK"
42
          | "PLN"
43
          | "RON"
44
          | "HUF"
45
          | "CZK"
46
          | "BRL"
47
          | "MXN"
48
          | "ARS"
49
          | "CLP"
50
          | "COP"
51
          | "INR"
52
          | "TRY";
53
        lead_type?: string;
54
        line_items?: {
55
          product_brand?: string;
56
          product_category?: string;
57
          product_id?: number;
58
          product_name?: string;
59
          product_price?: string;
60
          product_quantity?: number;
61
          product_variant?: string;
62
          product_variant_id?: string;
63
        };
64
        order_id?: string;
65
        order_quantity?: number;
66
        page_name?: string;
67
        promo_code?: string;
68
        property?: string;
69
        search_query?: string;
70
        value?: string;
71
        video_title?: string;
72
      };
73
      percentage?: number;
74
      pin_id?: string[];
75
      prefill?: false | true;
76
      retention_days?: number;
77
      seed_id?: string[];
78
      url?: string[];
79
      visitor_source_id?: string;
80
      event_source?: {};
81
      ingestion_source?: {};
82
      engager_type?: number;
83
      campaign_id?: string[];
84
      ad_id?: string[];
85
      objective_type?:
86
        | "AWARENESS"
87
        | "CONSIDERATION"
88
        | "VIDEO_VIEW"
89
        | "WEB_CONVERSION"
90
        | "CATALOG_SALES"
91
        | "WEB_SESSIONS"
92
        | "VIDEO_COMPLETION"[];
93
      ad_account_id?: string;
94
    };
95
  } & {
96
    description?: string;
97
    audience_type:
98
      | ("CUSTOMER_LIST" & {})
99
      | ("VISITOR" & {})
100
      | ("ENGAGEMENT" & {})
101
      | ("ACTALIKE" & {})
102
      | ("PERSONA" & {});
103
  },
104
) {
105
  const url = new URL(
106
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audiences`,
107
  );
108

109
  const response = await fetch(url, {
110
    method: "POST",
111
    headers: {
112
      "Content-Type": "application/json",
113
      Authorization: "Bearer " + auth.token,
114
    },
115
    body: JSON.stringify(body),
116
  });
117
  if (!response.ok) {
118
    const text = await response.text();
119
    throw new Error(`${response.status} ${text}`);
120
  }
121
  return await response.json();
122
}
123