forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzaraz.go
430 lines (359 loc) · 14.8 KB
/
zaraz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
type ZarazConfig struct {
DebugKey string `json:"debugKey"`
Tools map[string]ZarazTool `json:"tools"`
Triggers map[string]ZarazTrigger `json:"triggers"`
ZarazVersion int64 `json:"zarazVersion"`
Consent ZarazConsent `json:"consent,omitempty"`
DataLayer *bool `json:"dataLayer,omitempty"`
Dlp []any `json:"dlp,omitempty"`
HistoryChange *bool `json:"historyChange,omitempty"`
Settings ZarazConfigSettings `json:"settings,omitempty"`
Variables map[string]ZarazVariable `json:"variables,omitempty"`
}
type ZarazWorker struct {
EscapedWorkerName string `json:"escapedWorkerName"`
WorkerTag string `json:"workerTag"`
MutableId string `json:"mutableId,omitempty"`
}
type ZarazConfigSettings struct {
AutoInjectScript *bool `json:"autoInjectScript"`
InjectIframes *bool `json:"injectIframes,omitempty"`
Ecommerce *bool `json:"ecommerce,omitempty"`
HideQueryParams *bool `json:"hideQueryParams,omitempty"`
HideIpAddress *bool `json:"hideIPAddress,omitempty"`
HideUserAgent *bool `json:"hideUserAgent,omitempty"`
HideExternalReferer *bool `json:"hideExternalReferer,omitempty"`
CookieDomain string `json:"cookieDomain,omitempty"`
InitPath string `json:"initPath,omitempty"`
ScriptPath string `json:"scriptPath,omitempty"`
TrackPath string `json:"trackPath,omitempty"`
EventsApiPath string `json:"eventsApiPath,omitempty"`
McRootPath string `json:"mcRootPath,omitempty"`
ContextEnricher ZarazWorker `json:"contextEnricher,omitempty"`
}
// Deprecated: To be removed pending migration of existing configs.
type ZarazNeoEvent struct {
BlockingTriggers []string `json:"blockingTriggers"`
FiringTriggers []string `json:"firingTriggers"`
Data map[string]any `json:"data"`
ActionType string `json:"actionType,omitempty"`
}
type ZarazAction struct {
BlockingTriggers []string `json:"blockingTriggers"`
FiringTriggers []string `json:"firingTriggers"`
Data map[string]any `json:"data"`
ActionType string `json:"actionType,omitempty"`
}
type ZarazToolType string
const (
ZarazToolLibrary ZarazToolType = "library"
ZarazToolComponent ZarazToolType = "component"
ZarazToolCustomMc ZarazToolType = "custom-mc"
)
type ZarazTool struct {
BlockingTriggers []string `json:"blockingTriggers"`
Enabled *bool `json:"enabled"`
DefaultFields map[string]any `json:"defaultFields"`
Name string `json:"name"`
NeoEvents []ZarazNeoEvent `json:"neoEvents"`
Actions map[string]ZarazAction `json:"actions"`
Type ZarazToolType `json:"type"`
DefaultPurpose string `json:"defaultPurpose,omitempty"`
Library string `json:"library,omitempty"`
Component string `json:"component,omitempty"`
Permissions []string `json:"permissions"`
Settings map[string]any `json:"settings"`
Worker ZarazWorker `json:"worker,omitempty"`
}
type ZarazTriggerSystem string
const ZarazPageload ZarazTriggerSystem = "pageload"
type ZarazLoadRuleOp string
type ZarazRuleType string
const (
ZarazClickListener ZarazRuleType = "clickListener"
ZarazTimer ZarazRuleType = "timer"
ZarazFormSubmission ZarazRuleType = "formSubmission"
ZarazVariableMatch ZarazRuleType = "variableMatch"
ZarazScrollDepth ZarazRuleType = "scrollDepth"
ZarazElementVisibility ZarazRuleType = "elementVisibility"
ZarazClientEval ZarazRuleType = "clientEval"
)
type ZarazSelectorType string
const (
ZarazXPath ZarazSelectorType = "xpath"
ZarazCSS ZarazSelectorType = "css"
)
type ZarazRuleSettings struct {
Type ZarazSelectorType `json:"type,omitempty"`
Selector string `json:"selector,omitempty"`
WaitForTags int `json:"waitForTags,omitempty"`
Interval int `json:"interval,omitempty"`
Limit int `json:"limit,omitempty"`
Validate *bool `json:"validate,omitempty"`
Variable string `json:"variable,omitempty"`
Match string `json:"match,omitempty"`
Positions string `json:"positions,omitempty"`
Op ZarazLoadRuleOp `json:"op,omitempty"`
Value string `json:"value,omitempty"`
}
type ZarazTriggerRule struct {
Id string `json:"id"`
Match string `json:"match,omitempty"`
Op ZarazLoadRuleOp `json:"op,omitempty"`
Value string `json:"value,omitempty"`
Action ZarazRuleType `json:"action"`
Settings ZarazRuleSettings `json:"settings"`
}
type ZarazTrigger struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
LoadRules []ZarazTriggerRule `json:"loadRules"`
ExcludeRules []ZarazTriggerRule `json:"excludeRules"`
ClientRules []any `json:"clientRules,omitempty"` // what is this?
System ZarazTriggerSystem `json:"system,omitempty"`
}
type ZarazVariableType string
const (
ZarazVarString ZarazVariableType = "string"
ZarazVarSecret ZarazVariableType = "secret"
ZarazVarWorker ZarazVariableType = "worker"
)
type ZarazVariable struct {
Name string `json:"name"`
Type ZarazVariableType `json:"type"`
Value interface{} `json:"value"`
}
type ZarazButtonTextTranslations struct {
AcceptAll map[string]string `json:"accept_all"`
RejectAll map[string]string `json:"reject_all"`
ConfirmMyChoices map[string]string `json:"confirm_my_choices"`
}
type ZarazPurpose struct {
Name string `json:"name"`
Description string `json:"description"`
}
type ZarazPurposeWithTranslations struct {
Name map[string]string `json:"name"`
Description map[string]string `json:"description"`
Order int `json:"order"`
}
type ZarazConsent struct {
Enabled *bool `json:"enabled"`
ButtonTextTranslations ZarazButtonTextTranslations `json:"buttonTextTranslations,omitempty"`
CompanyEmail string `json:"companyEmail,omitempty"`
CompanyName string `json:"companyName,omitempty"`
CompanyStreetAddress string `json:"companyStreetAddress,omitempty"`
ConsentModalIntroHTML string `json:"consentModalIntroHTML,omitempty"`
ConsentModalIntroHTMLWithTranslations map[string]string `json:"consentModalIntroHTMLWithTranslations,omitempty"`
CookieName string `json:"cookieName,omitempty"`
CustomCSS string `json:"customCSS,omitempty"`
CustomIntroDisclaimerDismissed *bool `json:"customIntroDisclaimerDismissed,omitempty"`
DefaultLanguage string `json:"defaultLanguage,omitempty"`
HideModal *bool `json:"hideModal,omitempty"`
Purposes map[string]ZarazPurpose `json:"purposes,omitempty"`
PurposesWithTranslations map[string]ZarazPurposeWithTranslations `json:"purposesWithTranslations,omitempty"`
}
type ZarazConfigResponse struct {
Result ZarazConfig `json:"result"`
Response
}
type ZarazWorkflowResponse struct {
Result string `json:"result"`
Response
}
type ZarazPublishResponse struct {
Result string `json:"result"`
Response
}
type UpdateZarazConfigParams struct {
DebugKey string `json:"debugKey"`
Tools map[string]ZarazTool `json:"tools"`
Triggers map[string]ZarazTrigger `json:"triggers"`
ZarazVersion int64 `json:"zarazVersion"`
Consent ZarazConsent `json:"consent,omitempty"`
DataLayer *bool `json:"dataLayer,omitempty"`
Dlp []any `json:"dlp,omitempty"`
HistoryChange *bool `json:"historyChange,omitempty"`
Settings ZarazConfigSettings `json:"settings,omitempty"`
Variables map[string]ZarazVariable `json:"variables,omitempty"`
}
type UpdateZarazWorkflowParams struct {
Workflow string `json:"workflow"`
}
type PublishZarazConfigParams struct {
Description string `json:"description"`
}
type ZarazHistoryRecord struct {
ID int64 `json:"id,omitempty"`
UserID string `json:"userId,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt *time.Time `json:"createdAt,omitempty"`
UpdatedAt *time.Time `json:"updatedAt,omitempty"`
}
type ZarazConfigHistoryListResponse struct {
Result []ZarazHistoryRecord `json:"result"`
Response
ResultInfo `json:"result_info"`
}
type ListZarazConfigHistoryParams struct {
ResultInfo
}
type GetZarazConfigsByIdResponse = map[string]interface{}
// listZarazConfigHistoryDefaultPageSize represents the default per_page size of the API.
var listZarazConfigHistoryDefaultPageSize int = 100
func (api *API) GetZarazConfig(ctx context.Context, rc *ResourceContainer) (ZarazConfigResponse, error) {
if rc.Identifier == "" {
return ZarazConfigResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/config", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZarazConfigResponse{}, err
}
var recordResp ZarazConfigResponse
err = json.Unmarshal(res, &recordResp)
if err != nil {
return ZarazConfigResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return recordResp, nil
}
func (api *API) UpdateZarazConfig(ctx context.Context, rc *ResourceContainer, params UpdateZarazConfigParams) (ZarazConfigResponse, error) {
if rc.Identifier == "" {
return ZarazConfigResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/config", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return ZarazConfigResponse{}, err
}
var updateResp ZarazConfigResponse
err = json.Unmarshal(res, &updateResp)
if err != nil {
return ZarazConfigResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return updateResp, nil
}
func (api *API) GetZarazWorkflow(ctx context.Context, rc *ResourceContainer) (ZarazWorkflowResponse, error) {
if rc.Identifier == "" {
return ZarazWorkflowResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/workflow", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZarazWorkflowResponse{}, err
}
var response ZarazWorkflowResponse
err = json.Unmarshal(res, &response)
if err != nil {
return ZarazWorkflowResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response, nil
}
func (api *API) UpdateZarazWorkflow(ctx context.Context, rc *ResourceContainer, params UpdateZarazWorkflowParams) (ZarazWorkflowResponse, error) {
if rc.Identifier == "" {
return ZarazWorkflowResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/workflow", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.Workflow)
if err != nil {
return ZarazWorkflowResponse{}, err
}
var response ZarazWorkflowResponse
err = json.Unmarshal(res, &response)
if err != nil {
return ZarazWorkflowResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response, nil
}
func (api *API) PublishZarazConfig(ctx context.Context, rc *ResourceContainer, params PublishZarazConfigParams) (ZarazPublishResponse, error) {
if rc.Identifier == "" {
return ZarazPublishResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/publish", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params.Description)
if err != nil {
return ZarazPublishResponse{}, err
}
var response ZarazPublishResponse
err = json.Unmarshal(res, &response)
if err != nil {
return ZarazPublishResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return response, nil
}
func (api *API) ListZarazConfigHistory(ctx context.Context, rc *ResourceContainer, params ListZarazConfigHistoryParams) ([]ZarazHistoryRecord, *ResultInfo, error) {
if rc.Identifier == "" {
return nil, nil, ErrMissingZoneID
}
autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}
if params.PerPage < 1 {
params.PerPage = listZarazConfigHistoryDefaultPageSize
}
if params.Page < 1 {
params.Page = 1
}
var records []ZarazHistoryRecord
var lastResultInfo ResultInfo
for {
uri := buildURI(fmt.Sprintf("/zones/%s/settings/zaraz/v2/history", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []ZarazHistoryRecord{}, &ResultInfo{}, err
}
var listResponse ZarazConfigHistoryListResponse
err = json.Unmarshal(res, &listResponse)
if err != nil {
return []ZarazHistoryRecord{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
records = append(records, listResponse.Result...)
lastResultInfo = listResponse.ResultInfo
params.ResultInfo = listResponse.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
return records, &lastResultInfo, nil
}
func (api *API) GetDefaultZarazConfig(ctx context.Context, rc *ResourceContainer) (ZarazConfigResponse, error) {
if rc.Identifier == "" {
return ZarazConfigResponse{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/default", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return ZarazConfigResponse{}, err
}
var recordResp ZarazConfigResponse
err = json.Unmarshal(res, &recordResp)
if err != nil {
return ZarazConfigResponse{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return recordResp, nil
}
func (api *API) ExportZarazConfig(ctx context.Context, rc *ResourceContainer) error {
if rc.Identifier == "" {
return ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/settings/zaraz/v2/export", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return err
}
var recordResp ZarazConfig
err = json.Unmarshal(res, &recordResp)
if err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return nil
}