0

Update campaigns

by
Published Dec 20, 2024

Update multiple ad campaigns based on campaign_ids.

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 campaigns
7
 * Update multiple ad campaigns based on campaign_ids.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  body: { id?: string } & {
13
    ad_account_id?: string;
14
    name?: string;
15
    status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT";
16
    lifetime_spend_cap?: number;
17
    daily_spend_cap?: number;
18
    order_line_id?: string;
19
    tracking_urls?: {
20
      impression?: string[];
21
      click?: string[];
22
      engagement?: string[];
23
      buyable_button?: string[];
24
      audience_verification?: string[];
25
    };
26
    start_time?: number;
27
    end_time?: number;
28
    is_flexible_daily_budgets?: false | true;
29
  } & {
30
    default_ad_group_budget_in_micro_currency?: number;
31
    is_automated_campaign?: false | true;
32
  } & {
33
      is_campaign_budget_optimization?: false | true;
34
      objective_type?:
35
        | "AWARENESS"
36
        | "CONSIDERATION"
37
        | "VIDEO_VIEW"
38
        | "WEB_CONVERSION"
39
        | "CATALOG_SALES"
40
        | "WEB_SESSIONS"
41
        | "VIDEO_COMPLETION";
42
    }[],
43
) {
44
  const url = new URL(
45
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/campaigns`,
46
  );
47

48
  const response = await fetch(url, {
49
    method: "PATCH",
50
    headers: {
51
      "Content-Type": "application/json",
52
      Authorization: "Bearer " + auth.token,
53
    },
54
    body: JSON.stringify(body),
55
  });
56
  if (!response.ok) {
57
    const text = await response.text();
58
    throw new Error(`${response.status} ${text}`);
59
  }
60
  return await response.json();
61
}
62