forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-tracking.html
More file actions
197 lines (177 loc) · 6.63 KB
/
Copy pathform-tracking.html
File metadata and controls
197 lines (177 loc) · 6.63 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Page for Form Tracking testing with Snowplow Micro</title>
<script>
function parseQuery() {
var query = {};
var pairs = window.location.search.substring(1).split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
}
</script>
<script type="text/javascript">
function addField() {
var fields = document.getElementById('fields');
fields.appendChild(document.createTextNode('New Field: '));
var input = document.createElement('input');
input.type = 'text';
input.id = 'newfield';
input.name = 'newfield';
input.value = 'new';
fields.appendChild(input);
fields.appendChild(document.createElement('br'));
snowplow('enableFormTracking');
}
</script>
</head>
<body>
<p id="title">Page for Form Tracking testing with Snowplow Micro</p>
<div id="init"></div>
<form id="myForm" class="formy-mcformface" action="/form-tracking.html">
<fieldset id="fields">
<legend>Personal Info:</legend>
<label for="fname">First name:</label><br />
<input type="text" id="fname" name="fname" value="John" class="test" /><br />
<label for="lname">Last name:</label><br />
<input type="text" id="lname" name="lname" value="Doe" /><br /><br />
<label for="pii">Personal Info:</label><br />
<input type="text" id="pid" name="pname" value="danger" /><br /><br />
<input type="radio" id="bike" name="vehicle" value="Bike" />
<label for="bike"> I have a bike</label><br />
</fieldset>
<label for="cars">Choose a car:</label>
<select id="cars" name="cars">
<option id="volvo" value="volvo">Volvo</option>
<option id="saab" value="saab">Saab</option></select
><br />
<label for="message">Enter a message:</label><br />
<textarea id="message" name="message" rows="10" cols="30">This is a message</textarea><br />
<input type="checkbox" id="terms" name="terms" value="agree" />
<label for="terms"> Agree to terms</label><br />
<input type="submit" value="Submit" id="submit" />
</form>
<form id="excludedForm" class="excluded-form" action="/form-tracking.html">
<label for="excluded-fname">First name:</label><br />
<input type="text" id="excluded-fname" name="excluded-fname" value="John" class="test" /><br />
<input type="submit" value="Submit" id="excluded-submit" />
</form>
<iframe id="form_iframe" title="form_iframe"></iframe>
<script>
var collector_endpoint = document.cookie.split('container=')[1].split(';')[0];
var testIdentifier = document.cookie.split('testIdentifier=')[1].split(';')[0].trim();
document.body.className += ' loaded';
var formHtml = '<form><input type="text" name="fname" id="fname"></form>';
var iframe = document.getElementById('form_iframe');
var iframeDocument = iframe.contentWindow.document.open();
iframeDocument.open();
iframeDocument.write(formHtml);
iframeDocument.close();
window.customElements.define(
'shadow-form',
class extends HTMLElement {
connectedCallback() {
const form = Object.assign(document.createElement('form'), { id: 'shadow-form', className: 'shadow-form' });
form.addEventListener(
'submit',
function (e) {
e.preventDefault();
},
false
);
const input = document.createElement('input');
form.appendChild(input);
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(form);
}
}
);
</script>
<shadow-form></shadow-form>
<script>
(function (p, l, o, w, i, n, g) {
if (!p[i]) {
p.GlobalSnowplowNamespace = p.GlobalSnowplowNamespace || [];
p.GlobalSnowplowNamespace.push(i);
p[i] = function () {
(p[i].q = p[i].q || []).push(arguments);
};
p[i].q = p[i].q || [];
n = l.createElement(o);
g = l.getElementsByTagName(o)[0];
n.async = 1;
n.src = w;
g.parentNode.insertBefore(n, g);
}
})(window, document, 'script', './snowplow.js', 'snowplow');
document.write(collector_endpoint);
snowplow('newTracker', 'sp', collector_endpoint, {
appId: 'autotracking-form-' + testIdentifier,
});
snowplow(function () {
document.getElementById('init').innerText = 'true';
});
function formFilter(formElement) {
return formElement.id !== 'lname';
}
function redactPII(eltValue, _, elt) {
if (elt.id === 'pid') {
return 'redacted';
}
return eltValue;
}
switch (parseQuery().filter) {
case 'exclude':
snowplow('enableFormTracking', { options: { fields: { denylist: ['fname'] } } });
break;
case 'include':
snowplow('enableFormTracking', { options: { fields: { allowlist: ['lname'] } } });
break;
case 'filter':
snowplow('enableFormTracking', {
options: {
forms: { allowlist: ['formy-mcformface'] },
fields: { filter: formFilter },
},
});
break;
case 'transform':
snowplow('enableFormTracking', {
options: {
fields: { transform: redactPII },
},
});
break;
case 'excludedForm':
snowplow('enableFormTracking', { options: { forms: { denylist: ['excluded-form'] } } });
break;
case 'onlyFocus':
snowplow('enableFormTracking', { options: { events: ['focus_form'] } });
break;
case 'iframeForm':
var forms = iframe.contentWindow.document.getElementsByTagName('form');
snowplow('enableFormTracking', { options: { forms: forms } });
break;
case 'shadow':
snowplow('enableFormTracking', { options: { forms: { allowlist: ['shadow-form'] } } });
break;
default:
snowplow('enableFormTracking', {
context: [
{
schema: 'iglu:org.schema/WebPage/jsonschema/1-0-0',
data: {
keywords: ['tester'],
},
},
],
});
break;
}
snowplow('trackPageView');
</script>
</body>
</html>