1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create/update ad entities in bulk |
7 | * Either create or update any combination of campaigns, ad groups, product groups, ads, or keywords. |
8 | Note that this request will be processed asynchronously; the response will include a request_id |
9 | that can be used to obtain the status of the request. |
10 | */ |
11 | export async function main( |
12 | auth: Pinterest, |
13 | ad_account_id: string, |
14 | body: { |
15 | create?: { |
16 | campaigns?: { |
17 | ad_account_id?: string; |
18 | name?: string; |
19 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
20 | lifetime_spend_cap?: number; |
21 | daily_spend_cap?: number; |
22 | order_line_id?: string; |
23 | tracking_urls?: { |
24 | impression?: string[]; |
25 | click?: string[]; |
26 | engagement?: string[]; |
27 | buyable_button?: string[]; |
28 | audience_verification?: string[]; |
29 | }; |
30 | start_time?: number; |
31 | end_time?: number; |
32 | is_flexible_daily_budgets?: false | true; |
33 | } & { |
34 | default_ad_group_budget_in_micro_currency?: number; |
35 | is_automated_campaign?: false | true; |
36 | } & { |
37 | is_flexible_daily_budgets?: false | true; |
38 | is_automated_campaign?: false | true; |
39 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
40 | objective_type: |
41 | | "AWARENESS" |
42 | | "CONSIDERATION" |
43 | | "VIDEO_VIEW" |
44 | | "WEB_CONVERSION" |
45 | | "CATALOG_SALES" |
46 | | "WEB_SESSIONS" |
47 | | "VIDEO_COMPLETION"; |
48 | }[]; |
49 | ad_groups?: { |
50 | name?: string; |
51 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
52 | budget_in_micro_currency?: number; |
53 | bid_in_micro_currency?: number; |
54 | optimization_goal_metadata?: { |
55 | conversion_tag_v3_goal_metadata?: { |
56 | attribution_windows?: { |
57 | click_window_days?: number; |
58 | engagement_window_days?: number; |
59 | view_window_days?: number; |
60 | }; |
61 | conversion_event?: |
62 | | "PAGE_VISIT" |
63 | | "SIGNUP" |
64 | | "CHECKOUT" |
65 | | "CUSTOM" |
66 | | "VIEW_CATEGORY" |
67 | | "SEARCH" |
68 | | "ADD_TO_CART" |
69 | | "WATCH_VIDEO" |
70 | | "LEAD" |
71 | | "APP_INSTALL"; |
72 | conversion_tag_id?: string; |
73 | cpa_goal_value_in_micro_currency?: string; |
74 | is_roas_optimized?: false | true; |
75 | learning_mode_type?: "ACTIVE" | "NOT_ACTIVE"; |
76 | }; |
77 | frequency_goal_metadata?: { |
78 | frequency?: number; |
79 | timerange?: |
80 | | "THIRTY_DAY" |
81 | | "DAY" |
82 | | "SEVEN_DAY" |
83 | | "TWENTY_MINUTE" |
84 | | "TEN_MINUTE" |
85 | | "TWENTY_FOUR_HOUR"; |
86 | }; |
87 | scrollup_goal_metadata?: { |
88 | scrollup_goal_value_in_micro_currency?: string; |
89 | }; |
90 | }; |
91 | budget_type?: "DAILY" | "LIFETIME" | "CBO_ADGROUP"; |
92 | start_time?: number; |
93 | end_time?: number; |
94 | targeting_spec?: { |
95 | AGE_BUCKET?: |
96 | | "18-24" |
97 | | "21+" |
98 | | "25-34" |
99 | | "35-44" |
100 | | "45-49" |
101 | | "50-54" |
102 | | "55-64" |
103 | | "65+"[]; |
104 | APPTYPE?: |
105 | | "android_mobile" |
106 | | "android_tablet" |
107 | | "ipad" |
108 | | "iphone" |
109 | | "web" |
110 | | "web_mobile"[]; |
111 | AUDIENCE_EXCLUDE?: string[]; |
112 | AUDIENCE_INCLUDE?: string[]; |
113 | GENDER?: "unknown" | "male" | "female"[]; |
114 | GEO?: string[]; |
115 | INTEREST?: string[]; |
116 | LOCALE?: string[]; |
117 | LOCATION?: string[]; |
118 | SHOPPING_RETARGETING?: { |
119 | lookback_window?: number; |
120 | tag_types?: number[]; |
121 | exclusion_window?: number; |
122 | }[]; |
123 | TARGETING_STRATEGY?: |
124 | | "CHOOSE_YOUR_OWN" |
125 | | "FIND_NEW_CUSTOMERS" |
126 | | "RECONNECT_WITH_USERS"[]; |
127 | }; |
128 | lifetime_frequency_cap?: number; |
129 | tracking_urls?: { |
130 | impression?: string[]; |
131 | click?: string[]; |
132 | engagement?: string[]; |
133 | buyable_button?: string[]; |
134 | audience_verification?: string[]; |
135 | }; |
136 | auto_targeting_enabled?: false | true; |
137 | placement_group?: "SEARCH" | "ALL" | "BROWSE" | "OTHER"; |
138 | pacing_delivery_type?: "STANDARD" | "ACCELERATED"; |
139 | campaign_id?: string; |
140 | billable_event?: "CLICKTHROUGH" | "IMPRESSION" | "VIDEO_V_50_MRC"; |
141 | bid_strategy_type?: "AUTOMATIC_BID" | "MAX_BID" | "TARGET_AVG"; |
142 | targeting_template_ids?: string[]; |
143 | } & { |
144 | pacing_delivery_type?: "STANDARD" | "ACCELERATED"; |
145 | auto_targeting_enabled?: false | true; |
146 | budget_type?: "DAILY" | "LIFETIME" | "CBO_ADGROUP"; |
147 | }[]; |
148 | ads?: { |
149 | ad_group_id?: string; |
150 | android_deep_link?: string; |
151 | carousel_android_deep_links?: string[]; |
152 | carousel_destination_urls?: string[]; |
153 | carousel_ios_deep_links?: string[]; |
154 | click_tracking_url?: string; |
155 | creative_type?: |
156 | | "REGULAR" |
157 | | "VIDEO" |
158 | | "SHOPPING" |
159 | | "CAROUSEL" |
160 | | "MAX_VIDEO" |
161 | | "SHOP_THE_PIN" |
162 | | "COLLECTION" |
163 | | "IDEA" |
164 | | "SHOWCASE" |
165 | | "QUIZ"; |
166 | destination_url?: string; |
167 | ios_deep_link?: string; |
168 | is_pin_deleted?: false | true; |
169 | is_removable?: false | true; |
170 | name?: string; |
171 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
172 | tracking_urls?: { |
173 | impression?: string[]; |
174 | click?: string[]; |
175 | engagement?: string[]; |
176 | buyable_button?: string[]; |
177 | audience_verification?: string[]; |
178 | }; |
179 | view_tracking_url?: string; |
180 | lead_form_id?: string; |
181 | grid_click_type?: "CLOSEUP" | "DIRECT_TO_DESTINATION"; |
182 | customizable_cta_type?: |
183 | | "ADD_TO_CART" |
184 | | "GET_OFFER" |
185 | | "LEARN_MORE" |
186 | | "ORDER_NOW" |
187 | | "SHOP_NOW" |
188 | | "SIGN_UP" |
189 | | "SUBSCRIBE" |
190 | | "BUY_NOW" |
191 | | "CONTACT_US" |
192 | | "GET_QUOTE" |
193 | | "VISIT_SITE" |
194 | | "APPLY_NOW" |
195 | | "BOOK_NOW" |
196 | | "REQUEST_DEMO" |
197 | | "REGISTER_NOW" |
198 | | "FIND_A_DEALER" |
199 | | "WATCH_NOW" |
200 | | "READ_MORE"; |
201 | quiz_pin_data?: { |
202 | questions?: { |
203 | question_id?: number; |
204 | question_text?: string; |
205 | options?: { id?: number; text?: string }[]; |
206 | }[]; |
207 | results?: { |
208 | organic_pin_id?: string; |
209 | android_deep_link?: string; |
210 | ios_deep_link?: string; |
211 | destination_url?: string; |
212 | result_id?: number; |
213 | }[]; |
214 | tie_breaker_type?: "CUSTOM" | "RANDOM"; |
215 | tie_breaker_custom_result?: { |
216 | organic_pin_id?: string; |
217 | android_deep_link?: string; |
218 | ios_deep_link?: string; |
219 | destination_url?: string; |
220 | result_id?: number; |
221 | }; |
222 | }; |
223 | } & { pin_id?: string } & {}[]; |
224 | product_groups?: { |
225 | ad_group_id: string; |
226 | product_group_promotion: { |
227 | id?: string; |
228 | ad_group_id?: string; |
229 | bid_in_micro_currency?: number; |
230 | included?: false | true; |
231 | definition?: string; |
232 | relative_definition?: string; |
233 | parent_id?: string; |
234 | slideshow_collections_title?: string; |
235 | slideshow_collections_description?: string; |
236 | is_mdl?: false | true; |
237 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
238 | tracking_url?: string; |
239 | catalog_product_group_id?: string; |
240 | catalog_product_group_name?: string; |
241 | collections_hero_pin_id?: string; |
242 | collections_hero_destination_url?: string; |
243 | grid_click_type?: "CLOSEUP" | "DIRECT_TO_DESTINATION"; |
244 | } & { |
245 | creative_type?: |
246 | | "REGULAR" |
247 | | "VIDEO" |
248 | | "SHOPPING" |
249 | | "CAROUSEL" |
250 | | "MAX_VIDEO" |
251 | | "SHOP_THE_PIN" |
252 | | "COLLECTION" |
253 | | "IDEA" |
254 | | "SHOWCASE" |
255 | | "QUIZ"; |
256 | }[]; |
257 | }[]; |
258 | keywords?: { |
259 | keywords: { |
260 | bid?: number; |
261 | match_type: |
262 | | "BROAD" |
263 | | "PHRASE" |
264 | | "EXACT" |
265 | | "EXACT_NEGATIVE" |
266 | | "PHRASE_NEGATIVE"; |
267 | value: string; |
268 | }[]; |
269 | parent_id: string; |
270 | }[]; |
271 | }; |
272 | update?: { |
273 | campaigns?: { id?: string } & { |
274 | ad_account_id?: string; |
275 | name?: string; |
276 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
277 | lifetime_spend_cap?: number; |
278 | daily_spend_cap?: number; |
279 | order_line_id?: string; |
280 | tracking_urls?: { |
281 | impression?: string[]; |
282 | click?: string[]; |
283 | engagement?: string[]; |
284 | buyable_button?: string[]; |
285 | audience_verification?: string[]; |
286 | }; |
287 | start_time?: number; |
288 | end_time?: number; |
289 | is_flexible_daily_budgets?: false | true; |
290 | } & { |
291 | default_ad_group_budget_in_micro_currency?: number; |
292 | is_automated_campaign?: false | true; |
293 | } & { |
294 | is_campaign_budget_optimization?: false | true; |
295 | objective_type?: |
296 | | "AWARENESS" |
297 | | "CONSIDERATION" |
298 | | "VIDEO_VIEW" |
299 | | "WEB_CONVERSION" |
300 | | "CATALOG_SALES" |
301 | | "WEB_SESSIONS" |
302 | | "VIDEO_COMPLETION"; |
303 | }[]; |
304 | ad_groups?: { |
305 | name?: string; |
306 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
307 | budget_in_micro_currency?: number; |
308 | bid_in_micro_currency?: number; |
309 | optimization_goal_metadata?: { |
310 | conversion_tag_v3_goal_metadata?: { |
311 | attribution_windows?: { |
312 | click_window_days?: number; |
313 | engagement_window_days?: number; |
314 | view_window_days?: number; |
315 | }; |
316 | conversion_event?: |
317 | | "PAGE_VISIT" |
318 | | "SIGNUP" |
319 | | "CHECKOUT" |
320 | | "CUSTOM" |
321 | | "VIEW_CATEGORY" |
322 | | "SEARCH" |
323 | | "ADD_TO_CART" |
324 | | "WATCH_VIDEO" |
325 | | "LEAD" |
326 | | "APP_INSTALL"; |
327 | conversion_tag_id?: string; |
328 | cpa_goal_value_in_micro_currency?: string; |
329 | is_roas_optimized?: false | true; |
330 | learning_mode_type?: "ACTIVE" | "NOT_ACTIVE"; |
331 | }; |
332 | frequency_goal_metadata?: { |
333 | frequency?: number; |
334 | timerange?: |
335 | | "THIRTY_DAY" |
336 | | "DAY" |
337 | | "SEVEN_DAY" |
338 | | "TWENTY_MINUTE" |
339 | | "TEN_MINUTE" |
340 | | "TWENTY_FOUR_HOUR"; |
341 | }; |
342 | scrollup_goal_metadata?: { |
343 | scrollup_goal_value_in_micro_currency?: string; |
344 | }; |
345 | }; |
346 | budget_type?: "DAILY" | "LIFETIME" | "CBO_ADGROUP"; |
347 | start_time?: number; |
348 | end_time?: number; |
349 | targeting_spec?: { |
350 | AGE_BUCKET?: |
351 | | "18-24" |
352 | | "21+" |
353 | | "25-34" |
354 | | "35-44" |
355 | | "45-49" |
356 | | "50-54" |
357 | | "55-64" |
358 | | "65+"[]; |
359 | APPTYPE?: |
360 | | "android_mobile" |
361 | | "android_tablet" |
362 | | "ipad" |
363 | | "iphone" |
364 | | "web" |
365 | | "web_mobile"[]; |
366 | AUDIENCE_EXCLUDE?: string[]; |
367 | AUDIENCE_INCLUDE?: string[]; |
368 | GENDER?: "unknown" | "male" | "female"[]; |
369 | GEO?: string[]; |
370 | INTEREST?: string[]; |
371 | LOCALE?: string[]; |
372 | LOCATION?: string[]; |
373 | SHOPPING_RETARGETING?: { |
374 | lookback_window?: number; |
375 | tag_types?: number[]; |
376 | exclusion_window?: number; |
377 | }[]; |
378 | TARGETING_STRATEGY?: |
379 | | "CHOOSE_YOUR_OWN" |
380 | | "FIND_NEW_CUSTOMERS" |
381 | | "RECONNECT_WITH_USERS"[]; |
382 | }; |
383 | lifetime_frequency_cap?: number; |
384 | tracking_urls?: { |
385 | impression?: string[]; |
386 | click?: string[]; |
387 | engagement?: string[]; |
388 | buyable_button?: string[]; |
389 | audience_verification?: string[]; |
390 | }; |
391 | auto_targeting_enabled?: false | true; |
392 | placement_group?: "SEARCH" | "ALL" | "BROWSE" | "OTHER"; |
393 | pacing_delivery_type?: "STANDARD" | "ACCELERATED"; |
394 | campaign_id?: string; |
395 | billable_event?: "CLICKTHROUGH" | "IMPRESSION" | "VIDEO_V_50_MRC"; |
396 | bid_strategy_type?: "AUTOMATIC_BID" | "MAX_BID" | "TARGET_AVG"; |
397 | targeting_template_ids?: string[]; |
398 | } & { id: string }[]; |
399 | ads?: { |
400 | ad_group_id?: string; |
401 | android_deep_link?: string; |
402 | carousel_android_deep_links?: string[]; |
403 | carousel_destination_urls?: string[]; |
404 | carousel_ios_deep_links?: string[]; |
405 | click_tracking_url?: string; |
406 | creative_type?: |
407 | | "REGULAR" |
408 | | "VIDEO" |
409 | | "SHOPPING" |
410 | | "CAROUSEL" |
411 | | "MAX_VIDEO" |
412 | | "SHOP_THE_PIN" |
413 | | "COLLECTION" |
414 | | "IDEA" |
415 | | "SHOWCASE" |
416 | | "QUIZ"; |
417 | destination_url?: string; |
418 | ios_deep_link?: string; |
419 | is_pin_deleted?: false | true; |
420 | is_removable?: false | true; |
421 | name?: string; |
422 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
423 | tracking_urls?: { |
424 | impression?: string[]; |
425 | click?: string[]; |
426 | engagement?: string[]; |
427 | buyable_button?: string[]; |
428 | audience_verification?: string[]; |
429 | }; |
430 | view_tracking_url?: string; |
431 | lead_form_id?: string; |
432 | grid_click_type?: "CLOSEUP" | "DIRECT_TO_DESTINATION"; |
433 | customizable_cta_type?: |
434 | | "ADD_TO_CART" |
435 | | "GET_OFFER" |
436 | | "LEARN_MORE" |
437 | | "ORDER_NOW" |
438 | | "SHOP_NOW" |
439 | | "SIGN_UP" |
440 | | "SUBSCRIBE" |
441 | | "BUY_NOW" |
442 | | "CONTACT_US" |
443 | | "GET_QUOTE" |
444 | | "VISIT_SITE" |
445 | | "APPLY_NOW" |
446 | | "BOOK_NOW" |
447 | | "REQUEST_DEMO" |
448 | | "REGISTER_NOW" |
449 | | "FIND_A_DEALER" |
450 | | "WATCH_NOW" |
451 | | "READ_MORE"; |
452 | quiz_pin_data?: { |
453 | questions?: { |
454 | question_id?: number; |
455 | question_text?: string; |
456 | options?: { id?: number; text?: string }[]; |
457 | }[]; |
458 | results?: { |
459 | organic_pin_id?: string; |
460 | android_deep_link?: string; |
461 | ios_deep_link?: string; |
462 | destination_url?: string; |
463 | result_id?: number; |
464 | }[]; |
465 | tie_breaker_type?: "CUSTOM" | "RANDOM"; |
466 | tie_breaker_custom_result?: { |
467 | organic_pin_id?: string; |
468 | android_deep_link?: string; |
469 | ios_deep_link?: string; |
470 | destination_url?: string; |
471 | result_id?: number; |
472 | }; |
473 | }; |
474 | } & { id: string; pin_id?: string }[]; |
475 | product_groups?: { |
476 | ad_group_id: string; |
477 | product_group_promotion: { |
478 | id?: string; |
479 | ad_group_id?: string; |
480 | bid_in_micro_currency?: number; |
481 | included?: false | true; |
482 | definition?: string; |
483 | relative_definition?: string; |
484 | parent_id?: string; |
485 | slideshow_collections_title?: string; |
486 | slideshow_collections_description?: string; |
487 | is_mdl?: false | true; |
488 | status?: "ACTIVE" | "PAUSED" | "ARCHIVED" | "DRAFT" | "DELETED_DRAFT"; |
489 | tracking_url?: string; |
490 | catalog_product_group_id?: string; |
491 | catalog_product_group_name?: string; |
492 | collections_hero_pin_id?: string; |
493 | collections_hero_destination_url?: string; |
494 | grid_click_type?: "CLOSEUP" | "DIRECT_TO_DESTINATION"; |
495 | }[]; |
496 | }[]; |
497 | keywords?: { id: string; archived?: false | true; bid?: number }[]; |
498 | }; |
499 | }, |
500 | ) { |
501 | const url = new URL( |
502 | `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/bulk/upsert`, |
503 | ); |
504 |
|
505 | const response = await fetch(url, { |
506 | method: "POST", |
507 | headers: { |
508 | "Content-Type": "application/json", |
509 | Authorization: "Bearer " + auth.token, |
510 | }, |
511 | body: JSON.stringify(body), |
512 | }); |
513 | if (!response.ok) { |
514 | const text = await response.text(); |
515 | throw new Error(`${response.status} ${text}`); |
516 | } |
517 | return await response.json(); |
518 | } |
519 |
|