0

Update audience

by
Published Dec 20, 2024

Update (edit or remove) an existing targeting 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
 * Update audience
7
 * Update (edit or remove) an existing targeting audience.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  audience_id: string,
13
  body: {
14
    ad_account_id?: string;
15
    name?: string;
16
    rule?: {
17
      country?: string;
18
      customer_list_id?: string;
19
      engagement_domain?: string[];
20
      engagement_type?: string;
21
      event?: string;
22
      event_data?: {
23
        currency?:
24
          | "UNK"
25
          | "USD"
26
          | "GBP"
27
          | "CAD"
28
          | "EUR"
29
          | "AUD"
30
          | "NZD"
31
          | "SEK"
32
          | "ILS"
33
          | "CHF"
34
          | "HKD"
35
          | "JPY"
36
          | "SGD"
37
          | "KRW"
38
          | "NOK"
39
          | "DKK"
40
          | "PLN"
41
          | "RON"
42
          | "HUF"
43
          | "CZK"
44
          | "BRL"
45
          | "MXN"
46
          | "ARS"
47
          | "CLP"
48
          | "COP"
49
          | "INR"
50
          | "TRY";
51
        lead_type?: string;
52
        line_items?: {
53
          product_brand?: string;
54
          product_category?: string;
55
          product_id?: number;
56
          product_name?: string;
57
          product_price?: string;
58
          product_quantity?: number;
59
          product_variant?: string;
60
          product_variant_id?: string;
61
        };
62
        order_id?: string;
63
        order_quantity?: number;
64
        page_name?: string;
65
        promo_code?: string;
66
        property?: string;
67
        search_query?: string;
68
        value?: string;
69
        video_title?: string;
70
      };
71
      percentage?: number;
72
      pin_id?: string[];
73
      prefill?: false | true;
74
      retention_days?: number;
75
      seed_id?: string[];
76
      url?: string[];
77
      visitor_source_id?: string;
78
      event_source?: {};
79
      ingestion_source?: {};
80
      engager_type?: number;
81
      campaign_id?: string[];
82
      ad_id?: string[];
83
      objective_type?:
84
        | "AWARENESS"
85
        | "CONSIDERATION"
86
        | "VIDEO_VIEW"
87
        | "WEB_CONVERSION"
88
        | "CATALOG_SALES"
89
        | "WEB_SESSIONS"
90
        | "VIDEO_COMPLETION"[];
91
      ad_account_id?: string;
92
    };
93
  } & { description?: string; operation_type?: "UPDATE" | "REMOVE" },
94
) {
95
  const url = new URL(
96
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audiences/${audience_id}`,
97
  );
98

99
  const response = await fetch(url, {
100
    method: "PATCH",
101
    headers: {
102
      "Content-Type": "application/json",
103
      Authorization: "Bearer " + auth.token,
104
    },
105
    body: JSON.stringify(body),
106
  });
107
  if (!response.ok) {
108
    const text = await response.text();
109
    throw new Error(`${response.status} ${text}`);
110
  }
111
  return await response.json();
112
}
113