0

Create ad groups

by
Published Dec 20, 2024

Create multiple new ad groups.

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 ad groups
7
 * Create multiple new ad groups.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  body: {
13
    name?: string;
14
    status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT";
15
    budget_in_micro_currency?: number;
16
    bid_in_micro_currency?: number;
17
    optimization_goal_metadata?: {
18
      conversion_tag_v3_goal_metadata?: {
19
        attribution_windows?: {
20
          click_window_days?: number;
21
          engagement_window_days?: number;
22
          view_window_days?: number;
23
        };
24
        conversion_event?:
25
          | "PAGE_VISIT"
26
          | "SIGNUP"
27
          | "CHECKOUT"
28
          | "CUSTOM"
29
          | "VIEW_CATEGORY"
30
          | "SEARCH"
31
          | "ADD_TO_CART"
32
          | "WATCH_VIDEO"
33
          | "LEAD"
34
          | "APP_INSTALL";
35
        conversion_tag_id?: string;
36
        cpa_goal_value_in_micro_currency?: string;
37
        is_roas_optimized?: false | true;
38
        learning_mode_type?: "ACTIVE" | "NOT_ACTIVE";
39
      };
40
      frequency_goal_metadata?: {
41
        frequency?: number;
42
        timerange?:
43
          | "THIRTY_DAY"
44
          | "DAY"
45
          | "SEVEN_DAY"
46
          | "TWENTY_MINUTE"
47
          | "TEN_MINUTE"
48
          | "TWENTY_FOUR_HOUR";
49
      };
50
      scrollup_goal_metadata?: {
51
        scrollup_goal_value_in_micro_currency?: string;
52
      };
53
    };
54
    budget_type?: "DAILY" | "LIFETIME" | "CBO_ADGROUP";
55
    start_time?: number;
56
    end_time?: number;
57
    targeting_spec?: {
58
      AGE_BUCKET?:
59
        | "18-24"
60
        | "21+"
61
        | "25-34"
62
        | "35-44"
63
        | "45-49"
64
        | "50-54"
65
        | "55-64"
66
        | "65+"[];
67
      APPTYPE?:
68
        | "android_mobile"
69
        | "android_tablet"
70
        | "ipad"
71
        | "iphone"
72
        | "web"
73
        | "web_mobile"[];
74
      AUDIENCE_EXCLUDE?: string[];
75
      AUDIENCE_INCLUDE?: string[];
76
      GENDER?: "unknown" | "male" | "female"[];
77
      GEO?: string[];
78
      INTEREST?: string[];
79
      LOCALE?: string[];
80
      LOCATION?: string[];
81
      SHOPPING_RETARGETING?: {
82
        lookback_window?: number;
83
        tag_types?: number[];
84
        exclusion_window?: number;
85
      }[];
86
      TARGETING_STRATEGY?:
87
        | "CHOOSE_YOUR_OWN"
88
        | "FIND_NEW_CUSTOMERS"
89
        | "RECONNECT_WITH_USERS"[];
90
    };
91
    lifetime_frequency_cap?: number;
92
    tracking_urls?: {
93
      impression?: string[];
94
      click?: string[];
95
      engagement?: string[];
96
      buyable_button?: string[];
97
      audience_verification?: string[];
98
    };
99
    auto_targeting_enabled?: false | true;
100
    placement_group?: "SEARCH" | "ALL" | "BROWSE" | "OTHER";
101
    pacing_delivery_type?: "STANDARD" | "ACCELERATED";
102
    campaign_id?: string;
103
    billable_event?: "CLICKTHROUGH" | "IMPRESSION" | "VIDEO_V_50_MRC";
104
    bid_strategy_type?: "AUTOMATIC_BID" | "MAX_BID" | "TARGET_AVG";
105
    targeting_template_ids?: string[];
106
  } & {
107
    pacing_delivery_type?: "STANDARD" | "ACCELERATED";
108
    auto_targeting_enabled?: false | true;
109
    budget_type?: "DAILY" | "LIFETIME" | "CBO_ADGROUP";
110
  }[],
111
) {
112
  const url = new URL(
113
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/ad_groups`,
114
  );
115

116
  const response = await fetch(url, {
117
    method: "POST",
118
    headers: {
119
      "Content-Type": "application/json",
120
      Authorization: "Bearer " + auth.token,
121
    },
122
    body: JSON.stringify(body),
123
  });
124
  if (!response.ok) {
125
    const text = await response.text();
126
    throw new Error(`${response.status} ${text}`);
127
  }
128
  return await response.json();
129
}
130