-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.tpl
213 lines (179 loc) · 4.88 KB
/
template.tpl
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
___TERMS_OF_SERVICE___
By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
https://developers.google.com/tag-manager/gallery-tos (or such other URL as
Google may provide), as modified from time to time.
___INFO___
{
"type": "MACRO",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Unique ID Generator",
"categories": ["TAG_MANAGEMENT", "Marketing", "ANALYTICS"],
"description": "Generates a random ID based on different options.",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "RADIO",
"name": "options",
"displayName": "Please choose one of the options.",
"radioItems": [
{
"value": "default",
"displayValue": "Use default generated value.",
"help": "This will generate an ID in following format: \"J90v-Aap9-9LXg-gKVm-UBe9\""
},
{
"value": "custom",
"displayValue": "Customize value for ID."
}
],
"simpleValueType": true,
"defaultValue": "default"
},
{
"type": "GROUP",
"name": "customFields",
"displayName": "",
"groupStyle": "NO_ZIPPY",
"subParams": [
{
"type": "SELECT",
"name": "characterLength",
"displayName": "Please choose character length.",
"macrosInSelect": false,
"selectItems": [
{
"value": 16,
"displayValue": "16 character"
},
{
"value": 24,
"displayValue": "24 character"
},
{
"value": 32,
"displayValue": "32 character"
}
],
"simpleValueType": true,
"help": "You can choose between a length of 16, 24 or 32 character (excl. separators).",
"alwaysInSummary": true
},
{
"type": "SELECT",
"name": "separator",
"displayName": "Type of separator.",
"macrosInSelect": false,
"selectItems": [
{
"value": "-",
"displayValue": "Minus"
},
{
"value": ".",
"displayValue": "Dot"
},
{
"value": "",
"displayValue": "None"
}
],
"simpleValueType": true,
"alwaysInSummary": true
},
{
"type": "GROUP",
"name": "chars",
"displayName": "Select which characters to include.",
"subParams": [
{
"type": "CHECKBOX",
"name": "alphaUpper",
"checkboxText": "Include uppercase letters",
"simpleValueType": true,
"valueValidators": []
},
{
"type": "CHECKBOX",
"name": "alphaLower",
"checkboxText": "Include lowercase letters",
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "numbers",
"checkboxText": "Include numbers",
"simpleValueType": true
}
],
"groupStyle": "NO_ZIPPY",
"help": "If nothing is selected, all characters will be included per default."
}
],
"enablingConditions": [
{
"paramName": "options",
"paramValue": "custom",
"type": "EQUALS"
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
const generateRandom = require('generateRandom');
const alphabeticCharUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const alphabeticCharLower = 'abcdefghijklmnopqrstuvwxyz';
const nums = '0123456789';
const userSeparator = data.options === 'default' ? '-' : (data.separator === 'none' ? '' : data.separator);
let characters = '';
if (data.alphaUpper) {
characters += alphabeticCharUpper;
}
if (data.alphaLower) {
characters += alphabeticCharLower;
}
if (data.numbers) {
characters += nums;
}
if(!data.alphaUpper && !data.alphaLower && !data.numbers) {
characters += alphabeticCharUpper + alphabeticCharLower + nums;
}
const separatorIndices = () => {
if(data.characterLength === 16) {
return [2,6,8,12];
}
if (data.characterLength === 24) {
return [4,8,12,18];
}
if (data.characterLength === 32) {
return [8,12,16,20];
}
};
const randomElement = (arr) => {
return arr[generateRandom(0, arr.length - 1)];
};
const separator = userSeparator;
const characterLength = data.characterLength || 32;
const randomIdGenerator = () => {
const charactersArr = characters.split('');
let idPlain = '';
const indices = separatorIndices() || [8,12,16,20];
for (let i = 0; i < characterLength; i++) {
idPlain += randomElement(charactersArr);
}
var idCustom = idPlain.split('');
indices.reverse().forEach(i => idCustom.splice(i,0,separator));
return idCustom.join('');
};
const final = randomIdGenerator();
return final;
___TESTS___
scenarios: []
___NOTES___
Created on 12.12.2022, 14:19:35