0

Update Tracking Setting

by
Published Apr 8, 2025

Update the tracking setting with the given account ID.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `tracking-settings:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Update Tracking Setting
7
 * Update the tracking setting with the given account ID.*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  id: string,
13
  revision: string,
14
  body: {
15
    data: {
16
      type: "tracking-setting";
17
      id: string;
18
      attributes: {
19
        auto_add_parameters?: false | true;
20
        utm_source?: {
21
          flow?:
22
            | {
23
                type: "dynamic";
24
                value:
25
                  | "email_subject"
26
                  | "flow_id"
27
                  | "flow_name"
28
                  | "link_alt_text"
29
                  | "message_name"
30
                  | "message_name_id"
31
                  | "message_type"
32
                  | "profile_external_id"
33
                  | "profile_id";
34
              }
35
            | { type: "static"; value: string };
36
          campaign?:
37
            | {
38
                type: "dynamic";
39
                value:
40
                  | "email_subject"
41
                  | "link_alt_text"
42
                  | "message_type"
43
                  | "profile_external_id"
44
                  | "profile_id"
45
                  | "campaign_id"
46
                  | "campaign_name"
47
                  | "campaign_name_id"
48
                  | "campaign_name_send_day"
49
                  | "group_id"
50
                  | "group_name"
51
                  | "group_name_id";
52
              }
53
            | { type: "static"; value: string };
54
        };
55
        utm_medium?: {
56
          flow?:
57
            | {
58
                type: "dynamic";
59
                value:
60
                  | "email_subject"
61
                  | "flow_id"
62
                  | "flow_name"
63
                  | "link_alt_text"
64
                  | "message_name"
65
                  | "message_name_id"
66
                  | "message_type"
67
                  | "profile_external_id"
68
                  | "profile_id";
69
              }
70
            | { type: "static"; value: string };
71
          campaign?:
72
            | {
73
                type: "dynamic";
74
                value:
75
                  | "email_subject"
76
                  | "link_alt_text"
77
                  | "message_type"
78
                  | "profile_external_id"
79
                  | "profile_id"
80
                  | "campaign_id"
81
                  | "campaign_name"
82
                  | "campaign_name_id"
83
                  | "campaign_name_send_day"
84
                  | "group_id"
85
                  | "group_name"
86
                  | "group_name_id";
87
              }
88
            | { type: "static"; value: string };
89
        };
90
        utm_campaign?: {
91
          flow?:
92
            | {
93
                type: "dynamic";
94
                value:
95
                  | "email_subject"
96
                  | "flow_id"
97
                  | "flow_name"
98
                  | "link_alt_text"
99
                  | "message_name"
100
                  | "message_name_id"
101
                  | "message_type"
102
                  | "profile_external_id"
103
                  | "profile_id";
104
              }
105
            | { type: "static"; value: string };
106
          campaign?:
107
            | {
108
                type: "dynamic";
109
                value:
110
                  | "email_subject"
111
                  | "link_alt_text"
112
                  | "message_type"
113
                  | "profile_external_id"
114
                  | "profile_id"
115
                  | "campaign_id"
116
                  | "campaign_name"
117
                  | "campaign_name_id"
118
                  | "campaign_name_send_day"
119
                  | "group_id"
120
                  | "group_name"
121
                  | "group_name_id";
122
              }
123
            | { type: "static"; value: string };
124
        };
125
        utm_id?: {
126
          flow?:
127
            | {
128
                type: "dynamic";
129
                value:
130
                  | "email_subject"
131
                  | "flow_id"
132
                  | "flow_name"
133
                  | "link_alt_text"
134
                  | "message_name"
135
                  | "message_name_id"
136
                  | "message_type"
137
                  | "profile_external_id"
138
                  | "profile_id";
139
              }
140
            | { type: "static"; value: string };
141
          campaign?:
142
            | {
143
                type: "dynamic";
144
                value:
145
                  | "email_subject"
146
                  | "link_alt_text"
147
                  | "message_type"
148
                  | "profile_external_id"
149
                  | "profile_id"
150
                  | "campaign_id"
151
                  | "campaign_name"
152
                  | "campaign_name_id"
153
                  | "campaign_name_send_day"
154
                  | "group_id"
155
                  | "group_name"
156
                  | "group_name_id";
157
              }
158
            | { type: "static"; value: string };
159
        };
160
        utm_term?: {
161
          flow?:
162
            | {
163
                type: "dynamic";
164
                value:
165
                  | "email_subject"
166
                  | "flow_id"
167
                  | "flow_name"
168
                  | "link_alt_text"
169
                  | "message_name"
170
                  | "message_name_id"
171
                  | "message_type"
172
                  | "profile_external_id"
173
                  | "profile_id";
174
              }
175
            | { type: "static"; value: string };
176
          campaign?:
177
            | {
178
                type: "dynamic";
179
                value:
180
                  | "email_subject"
181
                  | "link_alt_text"
182
                  | "message_type"
183
                  | "profile_external_id"
184
                  | "profile_id"
185
                  | "campaign_id"
186
                  | "campaign_name"
187
                  | "campaign_name_id"
188
                  | "campaign_name_send_day"
189
                  | "group_id"
190
                  | "group_name"
191
                  | "group_name_id";
192
              }
193
            | { type: "static"; value: string };
194
        };
195
        custom_parameters?: {
196
          flow?:
197
            | {
198
                type: "dynamic";
199
                value:
200
                  | "email_subject"
201
                  | "flow_id"
202
                  | "flow_name"
203
                  | "link_alt_text"
204
                  | "message_name"
205
                  | "message_name_id"
206
                  | "message_type"
207
                  | "profile_external_id"
208
                  | "profile_id";
209
              }
210
            | { type: "static"; value: string };
211
          campaign?:
212
            | {
213
                type: "dynamic";
214
                value:
215
                  | "email_subject"
216
                  | "link_alt_text"
217
                  | "message_type"
218
                  | "profile_external_id"
219
                  | "profile_id"
220
                  | "campaign_id"
221
                  | "campaign_name"
222
                  | "campaign_name_id"
223
                  | "campaign_name_send_day"
224
                  | "group_id"
225
                  | "group_name"
226
                  | "group_name_id";
227
              }
228
            | { type: "static"; value: string };
229
          name: string;
230
        }[];
231
      };
232
    };
233
  },
234
) {
235
  const url = new URL(`https://a.klaviyo.com/api/tracking-settings/${id}`);
236

237
  const response = await fetch(url, {
238
    method: "PATCH",
239
    headers: {
240
      revision: revision,
241
      "Accept": "application/vnd.api+json",
242
      "Content-Type": "application/vnd.api+json",
243
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
244
    },
245
    body: JSON.stringify(body),
246
  });
247
  if (!response.ok) {
248
    const text = await response.text();
249
    throw new Error(`${response.status} ${text}`);
250
  }
251
  return await response.json();
252
}
253