Skip to content

Commit 585c30e

Browse files
committed
Add a debug panel and Login page
1 parent a0d5570 commit 585c30e

File tree

6 files changed

+170
-3
lines changed

6 files changed

+170
-3
lines changed

src/public/layouts/layout.eta

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
} else {
2525
gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode')));
2626
}
27+
28+
if(localStorage.getItem('userId') != null) {
29+
window.dataLayer.push({'user_id': localStorage.getItem('userId')});
30+
}
2731
</script>
2832
<!-- Google Tag Manager -->
2933
<script>(function (w, d, s, l, i) {
@@ -46,10 +50,12 @@
4650

4751
<%~ it.body %>
4852

53+
<%~ includeFile("debug") %>
54+
4955
<%~ includeFile("consent") %>
5056

5157
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
5258
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
5359
crossorigin="anonymous"></script>
5460
</body>
55-
</html>
61+
</html>

src/public/login.eta

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<% layout("layout", { title: "Login - GA Tutorials" }) %>
2+
3+
<%~ includeFile("nav") %>
4+
5+
<main class="container-lg pt-3">
6+
<h1>Simulate login to set User ID</h1>
7+
<form id="login-form">
8+
<div class="form-group">
9+
<label for="user-id">GA4 User ID</label>
10+
<input type="text" class="form-control" id="user" name="user-id" aria-describedby="userIdHelp"
11+
placeholder="Enter User ID">
12+
<small id="userIdHelp" class="form-text text-muted">This form simply
13+
sets your User ID so tagging will include it in events. To log out, submit
14+
the form without a value.</small>
15+
</div>
16+
<button type="submit" id="btn-login" class="btn btn-primary">Submit</button>
17+
<button type="button" id="gen-id" class="btn btn-info">Generate ID</button>
18+
<button type="button" id="btn-logout" class="btn btn-warning">Log out</button>
19+
</form>
20+
</main>
21+
22+
<script>
23+
24+
document.getElementById('btn-login').addEventListener('click', function() {
25+
loginUserId(document.getElementById('user').value);
26+
});
27+
28+
function loginUserId(userId) {
29+
// Sets 'userId' in storage so layout.eta can set this in the dataLayer on
30+
// each page.
31+
localStorage.setItem('userId', userId);
32+
}
33+
34+
document.getElementById('btn-logout').addEventListener('click', function() {
35+
logout(document.getElementById('user'));
36+
});
37+
38+
function logout(userIdField) {
39+
localStorage.removeItem('userId');
40+
userIdField.value = null;
41+
}
42+
43+
document.getElementById('gen-id').addEventListener('click', function() {
44+
generateUserId(document.getElementById('user'));
45+
});
46+
47+
function generateUserId(userField) {
48+
const prefixes = [
49+
'doubloons',
50+
'oceanographer',
51+
'retrogressed',
52+
'unmistakably',
53+
'counterbalanced',
54+
'wainscottings',
55+
'servomechanism',
56+
'expressionist',
57+
'preciousness',
58+
'fossilization',
59+
'conglomerated',
60+
'anthropomorphism',
61+
'reappointment',
62+
'transfigures',
63+
'choreographer',
64+
'sophisticated',
65+
'calibrations',
66+
'propitiating',
67+
'effortlessly',
68+
'prioritizing',
69+
'rechargeable',
70+
'thermostatic',
71+
'acclimatization',
72+
];
73+
const randIndex = Math.floor(Math.random() * prefixes.length);
74+
const randId = Math.floor(Math.random() * 1000 * 1000 * 1000 * 1000);
75+
userField.value = prefixes[randIndex] + '-' + randId;
76+
}
77+
78+
window.onload = () => document.getElementById('user').value = localStorage.getItem('userId');
79+
80+
</script>
81+
82+
<%~ includeFile("footer.eta") %>
83+

src/public/newsletter.eta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<h1>Signup for our Newsletter</h1>
77
<form id="newsletter-form" action="/thank-you">
88
<div class="form-group">
9-
<label for="exampleInputEmail1">Email address</label>
9+
<label for="email">Email address</label>
1010
<input type="email" class="form-control" id="email" name="email" aria-describedby="emailHelp"
1111
placeholder="Enter email">
1212
<small id="emailHelp" class="form-text text-muted">This form does not save your email. It is for the

src/public/partials/debug.eta

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<div class="container-sm">
2+
<button class="btn btn-info" type="button" data-bs-toggle="collapse" data-bs-target="#debug-panel" aria-expanded="true" aria-controls="debug-panel">Toggle debug panel</button>
3+
<!-- Collapsible debug panel -->
4+
<div class="collapse" id="debug-panel">
5+
<button id="btn-fetch" class="btn btn-outline-primary">Fetch debug values</button>
6+
<button id="btn-clear" class="btn btn-outline-warning">Clear client and session</button>
7+
<br/>
8+
<label for="tag-id">GA4 Tag ID</label>
9+
<input type="text" class="form-control" id="tag-id" name="tag-id" disabled="true">
10+
<label for="session-id">GA4 Session ID</label>
11+
<input type="text" class="form-control" id="session-id" name="session-id" disabled="true">
12+
<label for="client-id">GA4 Client ID</label>
13+
<input type="text" class="form-control" id="client-id" name="client-id" disabled="true">
14+
<label for="user-id">GA4 User ID</label>
15+
<input type="text" class="form-control" id="user-id" name="user-id" disabled="true">
16+
</div>
17+
</div>
18+
19+
<script>
20+
// Function for populating the debug panel values.
21+
function setIds() {
22+
// TODO: This may not be a reliable solution. From:
23+
// https://stackoverflow.com/questions/76075126/given-gtm-installed-ga4-how-to-send-events-via-gtag-without-send-to
24+
var gtagIds = Object.keys( window.google_tag_manager || [] ).filter( e => { return e.substring(0,2) == 'G-' } );
25+
if (gtagIds) {
26+
// Uses the first tag ID found.
27+
const firstTagId = gtagIds[0];
28+
29+
// Gets each field that will be updated.
30+
const tagIdField = document.getElementById('tag-id');
31+
const sessionIdField = document.getElementById('session-id');
32+
const clientIdField = document.getElementById('client-id');
33+
const userIdField = document.getElementById('user-id');
34+
35+
Promise.all([
36+
firstTagId,
37+
localStorage.getItem('userId'),
38+
// Gets values from gtag.
39+
// See https://developers.google.com/tag-platform/gtagjs/reference#set_examples.
40+
new Promise(cid => gtag('get', firstTagId, 'client_id', cid)),
41+
new Promise(sid => gtag('get', firstTagId, 'session_id', sid)),
42+
]).then(([tagId, userId, clientId, sessionId]) => {
43+
// Updates value for each field.
44+
tagIdField.value = tagId;
45+
userIdField.value = userId;
46+
clientIdField.value = clientId;
47+
sessionIdField.value = sessionId;
48+
49+
// Logs info as an object.
50+
console.log(JSON.stringify(
51+
{
52+
"user_id": userIdField.value,
53+
"client_id": clientId,
54+
"session_id": sessionId,
55+
},
56+
null,
57+
' '));
58+
});
59+
}
60+
}
61+
document.getElementById('btn-fetch').addEventListener("click", setIds);
62+
63+
// Function for clearing the GA cookies.
64+
function clearCookies() {
65+
document.cookie.split('; ').map(c => c.split('=')[0]).forEach(
66+
name => document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;');
67+
// Reloads the page.
68+
location.reload();
69+
}
70+
document.getElementById('btn-clear').addEventListener("click", clearCookies);
71+
72+
// Populates the debug panel when shown.
73+
document.getElementById('debug-panel').addEventListener('shown.bs.collapse', event =>
74+
setIds());
75+
</script>

src/public/partials/footer.eta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<h5>Subscribe to our newsletter</h5>
2424
<p>This is a demo and does not save your email.</p>
2525
<div class="d-flex flex-column flex-sm-row w-100 gap-2">
26-
<label for="newsletter1" class="visually-hidden">Email address</label>
26+
<label for="email" class="visually-hidden">Email address</label>
2727
<input id="email" name="email" type="text" class="form-control" placeholder="Email address">
2828
<button class="btn btn-primary" type="submit">Subscribe</button>
2929
</div>

src/public/partials/nav.eta

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<li class="nav-item">
1616
<a class="nav-link" href="/profile">Profile</a>
1717
</li>
18+
<li class="nav-item">
19+
<a class="nav-link" href="/login">Login</a>
20+
</li>
1821
</ul>
1922
</div>
2023
</div>

0 commit comments

Comments
 (0)