-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmp.html
348 lines (331 loc) · 19.6 KB
/
mp.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Accept-CH" content="DPR, Viewport-Width, Width, Content-DPR, Downlink, Save-Data">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google Analytics Measurement Protocol Hit Builder</title>
<style>
:root {
--black: rgba(0, 0, 0, .8);
--blue-dark: rgb(0, 112, 201);
--blue-light: rgb(102, 187, 255);
--gray: rgb(204, 204, 204);
--white: rgba(255, 255, 255, 1);
}
html {
overflow-y: scroll;
overflow-x: hidden;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
body {
margin: 1% auto;
padding: 0 3%;
max-width: 30em;
background-color: rgba(0, 0, 0, 0);
color: var(--black);
font-family: BlinkMacSystemFont, -apple-system, "Segoe UI", system-ui, Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
font-size: 1.125rem;
line-height: 1.618;
}
footer, small {
font-size: 80%;
}
b, dt, strong {
font-weight: 700;
}
h1, h2 {
line-height: 1.2;
letter-spacing: .02em;
}
a {
background-color: transparent;
color: var(--blue-dark);
text-decoration: none;
text-decoration-skip: ink;
-ms-touch-action: manipulation;
touch-action: manipulation;
}
a:hover, a:active, a:focus {
color: var(--blue-light);
}
abbr[title] {
text-decoration: none;
}
code {
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Monaco, "Courier New", monospace;
font-size: 1em;
}
input, select {
margin-bottom: 1.5em;
width: 95%;
display: block;
padding: .5em 1%;
font-size: 1em;
}
hr {
border: 0;
border-top: 1px solid var(--gray);
}
.hidden {
display: none;
}
@media (prefers-color-scheme: dark) {
body {
background-color: var(--black);
color: var(--white);
word-spacing: .05em;
}
a {
color: var(--blue-light);
}
a:hover, a:active, a:focus {
color: var(--blue-dark);
}
input, select {
background-color: var(--black);
color: var(--white);
}
}
</style>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://gtm.vreeman.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-WKSDWK');</script>
<link rel="canonical" href="https://vreeman.com/mp">
<link rel="alternate" href="https://vreeman.com/mp" hreflang="x-default">
<link rel="alternate" href="https://vreeman.com/mp" hreflang="en">
<meta name="description" content="The Google Analytics Measurement Protocol Hit Builder allows anybody to send hits to any Google Analytics property via the Measurement Protocol.">
<meta property="og:title" content="Google Analytics Measurement Protocol Hit Builder">
<meta property="og:url" content="https://vreeman.com/mp">
<meta property="og:description" content="The Google Analytics Measurement Protocol Hit Builder allows anybody to send hits to any Google Analytics property via the Measurement Protocol.">
<meta property="og:image" content="https://vreeman.com/img/tile-wide.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@vreeman">
<meta name="twitter:creator" content="@vreeman">
<meta name="twitter:title" content="Google Analytics Measurement Protocol Hit Builder">
<meta name="twitter:description" content="The Google Analytics Measurement Protocol Hit Builder allows anybody to send hits to any Google Analytics property via the Measurement Protocol.">
<meta name="twitter:image" content="https://vreeman.com/img/tile-wide.png">
<meta name="twitter:creator" content="@vreeman">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="Hit Builder">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<meta name="theme-color" content="#000">
</head>
<body>
<noscript><iframe src="https://gtm.vreeman.com/ns.html?id=GTM-WKSDWK" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<header>
<h1>Google Analytics Measurement Protocol Hit Builder</h1>
<p><a href="https://vreeman.com/" title="Simon Vreeman">Home</a> | <a href="https://vreeman.com/cro" title="CRO Tools"><abbr title="Conversion Rate Optimization">CRO</abbr> Tools</a></p>
</header>
<p><strong>RIP Google Analytics Universal</strong></p>
<p>Universal Analytics has been replaced by Google Analytics 4. Since July 2023 UA stopped processing data. So this PoC does not work anymore. GA4 has its own new <a href="https://developers.google.com/analytics/devguides/collection/protocol/ga4" target="_blank" rel="nofollow noopener noreferrer">Measurement Protocol</a> with some important issues solved that this version had.</p>
<hr>
<p>A Proof of Concept to learn how easy it is to use the Google Analytics Measurement Protocol. With this PoC, you can send different types of hits (pageview, event, refund) and a configured hit can automatically send every second. The Measurement Protocol Hit Builder generates a random Google Analytics Client ID, IP address and has an array with multiple user agents, viewports, and screen resolutions. With the Queue Time, it is possible to send latent hits up to 4 hours.</p><p><strong>Please use responsibly</strong>.</p>
<label for="hittype">Hit type (<code>t</code>)</label>
<select id="hittype" onchange="getHittype();">
<option value="pageview" selected>Pageview</option>
<option value="event">Event</option>
<option value="transaction" disabled>Transaction</option>
<option value="item" disabled>Item</option>
<option value="social" disabled>Social</option>
<option value="exception" disabled>Exception</option>
<option value="timing" disabled>Timing</option>
<option value="screenview" disabled>Screenview</option>
</select>
<label for="trackingid">Tracking ID (<code>tid</code>)</label>
<input type="text" id="trackingid" name="trackingid" autocapitalize="none" value="UA-2826482-22" placeholder="UA-2826482-22">
<label for="hostname">Hostname (<code>dh</code>)</label>
<input type="text" id="hostname" name="hostname" autocapitalize="none" value="measurement-protocol.com" placeholder="measurement-protocol.com">
<label for="pagepath">Page Path (<code>dp</code>)</label>
<input type="text" id="pagepath" name="pagepath" autocapitalize="none" value="mp-test" placeholder="mp-test">
<label for="pagetitle">Page Title (<code>dt</code>)</label>
<input type="text" id="pagetitle" name="pagetitle" autocapitalize="none" value="Measurement Protocol hit" placeholder="Measurement Protocol hit">
<label for="eventcategory" class="event hidden">Event Category (<code>ec</code>)</label>
<input type="text" id="eventcategory" name="eventcategory" autocapitalize="none" placeholder="Event Category" class="event hidden">
<label for="eventaction" class="event hidden">Event Action (<code>ea</code>)</label>
<input type="text" id="eventaction" name="eventaction" autocapitalize="none" placeholder="Event Action" class="event hidden">
<label for="eventlabel" class="event hidden">Event Label (<code>el</code>)</label>
<input type="text" id="eventlabel" name="eventlabel" autocapitalize="none" placeholder="Event Label" class="event hidden">
<label for="transactionid" class="event hidden transaction">Transaction ID (<code>ti</code>)</label>
<input type="text" id="transactionid" name="transactionid" autocapitalize="none" placeholder="12345" class="event hidden">
<label for="source">Source (<code>cs</code>)</label>
<input type="text" id="source" name="source" autocapitalize="none" placeholder="bing">
<label for="medium">Medium (<code>cm</code>)</label>
<input type="text" id="medium" name="medium" autocapitalize="none" placeholder="social">
<label for="campaign">Campaign Name (<code>cn</code>)</label>
<input type="text" id="campaign" name="campaign" autocapitalize="none" placeholder="adwords">
<label for="gclid">Google AdWords ID (<code>gclid</code>)</label>
<input type="text" id="gclid" name="gclid" autocapitalize="none" placeholder="CL6Q-OXyqKUCFcgK2goddQuoHg">
<label for="dclid">Google Display Ads ID (<code>dclid</code>)</label>
<input type="text" id="dclid" name="dclid" autocapitalize="none" placeholder="1234567890abc">
<label for="queuetime">Queue Time in hours (<code>qt</code>)</label>
<input type="number" id="queuetime" name="queuetime" autocapitalize="none" placeholder="4">
<label for="non-interaction">Non-Interaction (<code>ni</code>)</label>
<input type="checkbox" id="non-interaction" name="non-interaction" value="non-interaction">
<button onclick="sentHit()">Send one hit</button>
<button onclick="startCount()">Start sending hits</button>
<button onclick="stopCount()">Stop sending hits</button>
<p id="txt"></p>
<p id="feedback"></p>
<p>More info:</p>
<ul>
<li><a href="https://ga-dev-tools.appspot.com/hit-builder/" target="_blank" rel="nofollow noopener noreferrer">Measurement Protocol Hit Builder</a></li>
<li><a href="https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters" target="_blank" rel="nofollow noopener noreferrer">Measurement Protocol Parameters</a></li>
<li><a href="https://developers.google.com/analytics/devguides/collection/protocol/v1/reference" target="_blank" rel="nofollow noopener noreferrer">Measurement Protocol Reference</a></li>
<li><a href="https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide" target="_blank" rel="nofollow noopener noreferrer">Measurement Protocol Devguide</a></li>
</ul>
<h2>What is the Measurement Protocol?</h2>
<p>The Measurement Protocol allows you to send raw data to a Google Analytics property. It allows developers to send data from both the client and server to, for example, tie online to offline behavior.</p>
<p>Besides this Google Analytics Measurement Protocol Hit Builder I made a list of handy <a href="https://vreeman.com/cro" title="CRO Tools"><abbr title="Conversion Rate Optimization">CRO</abbr> tools</a>, <a href="https://vreeman.com/utm" title="Google Analytics 4 UTM Campaign URL Builder">Google Analytics 4 UTM Campaign URL Builder</a> and <a href="https://vreeman.com/hypothesis" title="A/B Test Hypothesis Builder">Experiment Hypothesis Builder</a>.</p>
<hr>
<footer>
<p>Vreeman.com is the personal website of <a class="h-card" rel="me" href="https://vreeman.com" title="Simon Vreeman">Simon Vreeman</a>.</p>
<dl>
<dt>Privacy & Cookie Policy</dt>
<dd>Vreeman.com uses Google Analytics (<code>_ga</code>, <code>FPID</code>, & <code>FPLC</code>), and Google Tag Manager Server-side to measure and analyze anonymous user behavior data to improve the website in an ethical manner.</dd>
<dt>Colophon</dt>
<dd>Vreeman.com uses Cloudflare Pages to ensure it loads fast and the source code is hosted on Github.</dd>
<dt>Licenses</dt>
<dd>Text is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" rel="license" title="Creative Commons Attribution 4.0 International License" data-vars-outbound-text="Creative Commons License" data-vars-outbound-link="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>. <code></></code> is licensed under <a href="https://opensource.org/licenses/MIT" rel="license" title="MIT License" data-vars-outbound-text="MIT License" data-vars-outbound-link="https://opensource.org/licenses/MIT">MIT License</a>.</dd>
<dt>Other</dt>
<dd>
<ul>
<li><a href="https://vreeman.com/cro" title="CRO (Conversion Rate Optimization) Tools" data-vars-outbound-text="CRO Tools" data-vars-outbound-link="https://vreeman.com/cro"><abbr title="Conversion Rate Optimization">CRO</abbr> Tools</a></li>
<li><a href="https://vreeman.com/f1" title="FIA Formula One (F1) 2024 Calendar / Agenda, Teams and Drivers" data-vars-outbound-text="F1 Calendar" data-vars-outbound-link="https://vreeman.com/f1">F1 2024 Calendar, Teams & Drivers</a></li>
<li><a href="https://vreeman.com/gear" title="What is in my backpack?" data-vars-outbound-text="Travel Gear" data-vars-outbound-link="https://vreeman.com/gear">Travel Gear</a></li>
<li><a href="https://vreeman.com/meditations/" title="Meditations by Marcus Aurelius. Translations by Gregory Hays." data-vars-outbound-text="Meditations" data-vars-outbound-link="https://vreeman.com/meditations/">Meditations by Marcus Aurelius. Translations by Gregory Hays.</a></li>
<li><a href="https://vreeman.com/discourses/" title="Discourses of Epictetus. Translated and edited by Robert Dobbin" data-vars-outbound-text="Discourses" data-vars-outbound-link="https://vreeman.com/discourses/">Discourses of Epictetus. Translated and edited by Robert Dobbin.</a></li>
</ul>
</dd>
</dl>
</footer>
<script>
function sentHit() {
var request = new XMLHttpRequest();
var hitType = document.getElementById("hittype").options[document.getElementById("hittype").selectedIndex].value;
var trackingID = document.getElementById("trackingid").value;
var clientID = uuid();
var ipAddress = randomIp();
var documentHostname = document.getElementById("hostname").value;
var pagePath = document.getElementById("pagepath").value;
var documentTitle = document.getElementById("pagetitle").value;
var source = document.getElementById("source").value;
var medium = document.getElementById("medium").value;
var campaign = document.getElementById("campaign").value;
var eventCategory = document.getElementById("eventcategory").value;
var eventAction = document.getElementById("eventaction").value;
var eventLabel = document.getElementById("eventlabel").value;
var transactionID = document.getElementById("transactionid").value;
var gclID = document.getElementById("gclid").value;
var dclID = document.getElementById("dclid").value;
var queueTime = document.getElementById("queuetime").value;
var nonInteraction = document.getElementById("non-interaction").checked;
var milliseconds = ((queueTime * 60) * 60) * 1000;
var useragentArray = [
'&sr=2560x1440&vp=1280x1343&ua=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5',
'&sr=412x732&vp=412x660&ua=Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Mobile Safari/537.36',
'&sr=375x667&vp=375x667&ua=Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'&sr=1440x900&vp=800x600&ua=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586',
'&sr=1024x768&vp=1024x768&ua=Mozilla/5.0 (iPad; CPU OS 10_3 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.3 Mobile/14E277 Safari/603.1.30'
];
var userAgent = useragentArray[Math.floor(Math.random() * useragentArray.length)];
var hitPayload = 'v=1&t=' + hitType + '&tid=' + trackingID + '&cid=' + clientID + '&uip=' + ipAddress + '&dp=' + pagePath + '&dh=' + documentHostname + '&dt=' + documentTitle + '&cs=' + source + '&cm=' + medium + '&cn=' + campaign + userAgent + '&aip=1&ds=web&ul=en-us&de=UTF-8&sd=24-bit&je=1';
if (eventCategory) {
hitPayload = hitPayload + '&ec=' + eventCategory;
}
if (eventAction) {
hitPayload = hitPayload + '&ea=' + eventAction;
}
if (eventLabel) {
hitPayload = hitPayload + '&el=' + eventLabel;
}
if (transactionID) {
hitPayload = hitPayload + '&ni=1&pa=refund&ti=' + transactionID;
}
if (gclID) {
hitPayload = hitPayload + '&gclid=' + gclID;
}
if (dclID) {
hitPayload = hitPayload + '&dclid=' + dclID;
}
if (queueTime) {
hitPayload = hitPayload + '&qt=' + milliseconds;
}
if (nonInteraction == true) {
hitPayload = hitPayload + '&ni=1';
}
localStorage.setItem('testObject', hitPayload);
var retrievedObject = localStorage.getItem('testObject');
console.log('typeof retrievedObject: ' + typeof retrievedObject);
console.log('Value of retrievedObject: ' + retrievedObject);
request.open('POST', 'https://www.google-analytics.com/collect', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(hitPayload);
document.getElementById("feedback").innerHTML = 'The following hit has been sent: <code>' + hitPayload + '</code>';
dataLayer.push({'event': 'hitsent'});
}
function getHittype() {
var hitType = document.getElementById("hittype").options[document.getElementById("hittype").selectedIndex].value;
if (hitType === 'event') {
removeClass();
}
else {
addClass();
}
}
// Remove class
function removeClass() {
var elems = document.querySelectorAll(".hidden");
[].forEach.call(elems, function(el) {
el.classList.remove("hidden");
});
}
// Add class & reset events values
function addClass() {
var elems = document.querySelectorAll(".event");
[].forEach.call(elems, function(el) {
el.classList.add("hidden");
});
var eventCategory = document.getElementById("eventcategory").value = '';
var eventAction = document.getElementById("eventaction").value = '';
var eventLabel = document.getElementById("eventlabel").value = '';
}
// Generate random Client ID
function uuid() {
var uuid = ( [1e7]+-1e3+-4e3+-8e3+-1e11 ).replace( /[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) );
return uuid;
}
// let uuid = self.crypto.randomUUID();
// console.log(uuid);
// Generate random IP address
randomByte = function() {
return Math.round(Math.random()*256);
};
function randomIp() {
var randomIp = randomByte() +'.' + randomByte() +'.' + randomByte() +'.' + randomByte();
return randomIp;
}
// Timer stuff
var c = 1;
var t;
var timer_is_on = 0;
function timedCount() {
document.getElementById("txt").innerHTML = c + ' hits sent';
sentHit();
c = c + 1;
t = setTimeout(function(){ timedCount(); }, 1000);
}
function startCount() {
if (!timer_is_on) {
timer_is_on = 1;
timedCount();
}
}
function stopCount() {
clearTimeout(t);
timer_is_on = 0;
}
</script>
</body>
</html>