|
| 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> |
0 commit comments