-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.tpl
218 lines (188 loc) · 6.2 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
212
213
214
215
216
___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": "URL Parser",
"description": "With this variable template, input any complete URL and select which components of the URL you\u0027d like returned.",
"containerContexts": [
"WEB"
],
"categories": ["UTILITY"]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "inputURL",
"displayName": "Input a URL or URL Variable",
"simpleValueType": true,
"help": "In this field, input a full URL or a URL variable, from which you\u0027d like to extract URL components. For instance, \u003cstrong\u003e{{Click URL}}\u003c/strong\u003e or \u003cstrong\u003e{{Page URL}}\u003c/strong\u003e",
"valueHint": "e.g. https://iihnordic.com/services/"
},
{
"type": "SELECT",
"name": "returnComponent",
"displayName": "Return URL Component",
"macrosInSelect": false,
"selectItems": [
{
"value": "protocol",
"displayValue": "Protocol"
},
{
"value": "hostname",
"displayValue": "Full Hostname"
},
{
"value": "rootHostname",
"displayValue": "Root Hostname (Hostname without subdomains)"
},
{
"value": "uri",
"displayValue": "Page Path / URI"
},
{
"value": "queryParam",
"displayValue": "Query Parameter"
},
{
"value": "fragment",
"displayValue": "Fragment"
},
{
"value": "baseURL",
"displayValue": "Base URL (URL without query parameters and fragments)"
}
],
"simpleValueType": true
},
{
"type": "CHECKBOX",
"name": "returnUriIndex",
"checkboxText": "Return a certain Page Path level",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "returnComponent",
"paramValue": "uri",
"type": "EQUALS"
}
],
"help": "Check this box if you\u0027d like to return a certain part/level of the input Page Path, based on index. Leave unchecked to return the full Page Path."
},
{
"type": "CHECKBOX",
"name": "inverseIndex",
"checkboxText": "Use Inverted Indexing",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "returnUriIndex",
"paramValue": true,
"type": "EQUALS"
}
],
"help": "Check this box if you\u0027d like to use inversed indexing for the query path. If this box is checked, the input \u003cstrong\u003e1\u003c/strong\u003e returns the last page path level, \u003cstrong\u003e2\u003c/strong\u003e returns the second last, etc."
},
{
"type": "TEXT",
"name": "uriIndex",
"displayName": "Page Path Index",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "returnUriIndex",
"paramValue": true,
"type": "EQUALS"
}
],
"valueValidators": [
{
"type": "POSITIVE_NUMBER"
}
],
"help": "In this field, input the index of the page path level you would like to return (indexing starts at \u003cstrong\u003e1\u003c/strong\u003e). Check \u0027Use Inverted Indexing\u0027 to invert indexing.",
"valueHint": "e.g. 1, 2 or 3 etc."
},
{
"type": "TEXT",
"name": "returnQueryKey",
"displayName": "Input the Query Parameter key",
"simpleValueType": true,
"enablingConditions": [
{
"paramName": "returnComponent",
"paramValue": "queryParam",
"type": "EQUALS"
}
],
"help": "In this text field, input the key to the URL query parameter whose value you\u0027d like to return",
"valueHint": "e.g. apikey",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "CHECKBOX",
"name": "uriDecode",
"checkboxText": "URI decode",
"simpleValueType": true,
"help": "Check this box if you\u0027d like to URI Decode the output.",
"defaultValue": true
}
]
___SANDBOXED_JS_FOR_WEB_TEMPLATE___
// Enter your template code here.
const decode = require("decodeUriComponent");
// const print = require("logToConsole");
// const makeNumber = require("makeNumber");
// Pre-checks and work
const url = data.inputURL;
const urlIsValid = url.indexOf("http") === 0;
if (!urlIsValid){ return undefined; }
// Initial breakdown of the URL
const urlComponents = (() => {
const comps = {};
const fragmentIndex = url.indexOf("#") === -1 ? url.length : url.indexOf("#");
const queriesIndex = url.indexOf("?") === -1 ? fragmentIndex : url.indexOf("?");
comps.baseURL = url.slice(0, queriesIndex);
comps.queries = url.slice(queriesIndex, fragmentIndex);
comps.fragment = url.slice(fragmentIndex, url.length);
return comps;
})();
const parsedURL = {};
parsedURL.protocol = urlComponents.baseURL.slice(0, urlComponents.baseURL.indexOf(":")).toUpperCase();
parsedURL.hostname = urlComponents.baseURL.split("/").filter(x => x)[1];
parsedURL.rootHostname = parsedURL.hostname.split(".").slice(parsedURL.hostname.split(".").length - 2).join(".");
parsedURL.uri = uri(data.uriIndex, data.inverseIndex);
parsedURL.queryParam = data.returnObjectified ? objectify(urlComponents.queries) : objectify(urlComponents.queries)[data.returnQueryKey];
parsedURL.fragment = urlComponents.fragment.slice(1);
parsedURL.baseURL = urlComponents.baseURL;
// Variable myst return a value
return parsedURL[data.returnComponent];
// Helper functions
function objectify(keyValuePairs) {
return keyValuePairs.slice(1).split("&").reduce((agg, current) => {
const pair = current.split("=");
agg[pair[0]] = pair[1];
return agg;
}, {});
}
function uri(findByIndex, inverted) {
const uriSections = urlComponents.baseURL.split("/").filter(x => x).slice(2);
const index = inverted === true ? (uriSections.length - findByIndex) : (findByIndex - 1);
return data.returnUriIndex ? uriSections[index] : ("/" + uriSections.join("/") + "/");
}
___TESTS___
scenarios: []
___NOTES___
Created on 04/12/2019, 10:45:23