0

Create targeting templates

by
Published Dec 20, 2024

Targeting templates allow advertisers to save a set of targeting details including audience lists, keywords & interest, demographics, and placements to use more than once during the campaign creation process. Templates can be used to build out basic targeting criteria that you plan to use across campaigns and to reuse performance targeting from prior campaigns for new campaigns.

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 targeting templates
7
 * Targeting templates allow advertisers to save a set of targeting details including audience lists,
8
 keywords & interest, demographics, and placements to use more than once during the campaign creation process.
9
 Templates can be used to build out basic targeting criteria that you plan to use across campaigns and to reuse
10
  performance targeting from prior campaigns for new campaigns.
11
 */
12
export async function main(
13
  auth: Pinterest,
14
  ad_account_id: string,
15
  body: {
16
    name?: string;
17
    auto_targeting_enabled?: false | true;
18
    targeting_attributes?: {
19
      AGE_BUCKET?:
20
        | "18-24"
21
        | "21+"
22
        | "25-34"
23
        | "35-44"
24
        | "45-49"
25
        | "50-54"
26
        | "55-64"
27
        | "65+"[];
28
      APPTYPE?:
29
        | "android_mobile"
30
        | "android_tablet"
31
        | "ipad"
32
        | "iphone"
33
        | "web"
34
        | "web_mobile"[];
35
      AUDIENCE_EXCLUDE?: string[];
36
      AUDIENCE_INCLUDE?: string[];
37
      GENDER?: "unknown" | "male" | "female"[];
38
      GEO?: string[];
39
      INTEREST?: string[];
40
      LOCALE?: string[];
41
      LOCATION?: string[];
42
      SHOPPING_RETARGETING?: {
43
        lookback_window?: number;
44
        tag_types?: number[];
45
        exclusion_window?: number;
46
      }[];
47
      TARGETING_STRATEGY?:
48
        | "CHOOSE_YOUR_OWN"
49
        | "FIND_NEW_CUSTOMERS"
50
        | "RECONNECT_WITH_USERS"[];
51
    };
52
    placement_group?: "ALL" | "SEARCH" | "BROWSE" | "OTHER";
53
    keywords?: {
54
      match_type?:
55
        | "BROAD"
56
        | "PHRASE"
57
        | "EXACT"
58
        | "EXACT_NEGATIVE"
59
        | "PHRASE_NEGATIVE";
60
      value?: string;
61
    }[];
62
    tracking_urls?: {
63
      impression?: string[];
64
      click?: string[];
65
      engagement?: string[];
66
      buyable_button?: string[];
67
      audience_verification?: string[];
68
    };
69
  } & {
70
    name: string;
71
    targeting_attributes: {
72
      AGE_BUCKET?:
73
        | "18-24"
74
        | "21+"
75
        | "25-34"
76
        | "35-44"
77
        | "45-49"
78
        | "50-54"
79
        | "55-64"
80
        | "65+"[];
81
      APPTYPE?:
82
        | "android_mobile"
83
        | "android_tablet"
84
        | "ipad"
85
        | "iphone"
86
        | "web"
87
        | "web_mobile"[];
88
      AUDIENCE_EXCLUDE?: string[];
89
      AUDIENCE_INCLUDE?: string[];
90
      GENDER?: "unknown" | "male" | "female"[];
91
      GEO?: string[];
92
      INTEREST?: string[];
93
      LOCALE?: string[];
94
      LOCATION?: string[];
95
      SHOPPING_RETARGETING?: {
96
        lookback_window?: number;
97
        tag_types?: number[];
98
        exclusion_window?: number;
99
      }[];
100
      TARGETING_STRATEGY?:
101
        | "CHOOSE_YOUR_OWN"
102
        | "FIND_NEW_CUSTOMERS"
103
        | "RECONNECT_WITH_USERS"[];
104
    };
105
  },
106
) {
107
  const url = new URL(
108
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/targeting_templates`,
109
  );
110

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