Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test newsletter_signup
(version: 1)
Comparing performance of:
test1 vs test2
Created:
3 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
test1
<script> (function () { // --- LÓGICA 1: Checkboxes en la página de newsletters --- // Comprobamos si el path contiene '/newsletters/' dentro del script if (window.location.pathname.indexOf('/newsletters/') > -1) { var newsletterTitle = /nlSigupHandler\(\s*['"][^'"]+['"]\s*,\s*['"]([^'"]+)['"]\s*,/; document.addEventListener('change', function (e) { var el = e.target; if (!el || el.type !== 'checkbox') return; var onchangeAttr = el.getAttribute('onchange'); if (!onchangeAttr) return; var match = onchangeAttr.match(newsletterTitle); if (!match) return; var text = match[1]; var item = el.checked ? "recibir newsletter" : "abandonar newsletter"; window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'newsletter_signup', link: { text: text, item: item } }); }, true); } // --- LÓGICA 2: Alta por parámetro URL (pimec-mlname) --- // Función auxiliar para decodificar Base64 URL Safe function decodeBase64Url(str) { try { // 1. Reemplazar caracteres URL safe (- por +, _ por /) str = str.replace(/-/g, '+').replace(/_/g, '/'); // 2. Rellenar padding si es necesario while (str.length % 4) { str += '='; } // 3. Decodificar (usamos decodeURIComponent(escape(...)) para soportar UTF-8 correctamente) return decodeURIComponent(escape(window.atob(str))); } catch (e) { console.error("GTM: Error decodificando base64 param", e); return str; // Fallback: devolvemos el valor original si falla } } // Obtenemos los parámetros de la URL var urlParams = new URLSearchParams(window.location.search); var paramName = 'pimec-mlname'; // Obtenemos el valor de la variable GTM // Nota: Al ser una inyección directa, si la variable no está definida podría dar error de sintaxis JS. // Asumimos que la variable devuelve un número o undefined. var isUserLoggedIn = 1; // Comprobamos si existe el parámetro Y el usuario está logueado (valor 1) if (urlParams.has(paramName) && isUserLoggedIn === 1) { var rawValue = urlParams.get(paramName); var decodedText = decodeBase64Url(rawValue); window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'newsletter_signup', link: { text: decodedText, // Asumimos que si viene por parámetro es una confirmación de alta ("recibir") item: "recibir newsletter" } }); } })(); </script>
test2
<script> (function () { 'use strict'; // ------------------------------------------------------- // 1. FUNCIÓN UNIFICADA DE ENVÍO (Helper) // ------------------------------------------------------- function sendNewsletterEvent(text, actionType) { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'newsletter_signup', link: { text: text, item: actionType // "recibir newsletter" o "abandonar newsletter" } }); } // ------------------------------------------------------- // 2. FUNCIÓN DE DECODIFICACIÓN (Helper) // ------------------------------------------------------- function decodeB64(str) { try { // Reemplazo para Base64 URL Safe y decodificación UTF-8 return decodeURIComponent(escape(window.atob(str.replace(/-/g, '+').replace(/_/g, '/')))); } catch (e) { console.warn("GTM: Error decodificando parámetro", e); return null; } } // ------------------------------------------------------- // 3. LÓGICA A: COMPROBACIÓN DE PARÁMETRO URL (Carga de página) // ------------------------------------------------------- // Optimizacion: Solo leemos variables y procesamos si el parametro existe var paramName = 'pimec-mlname'; var urlParams = new URLSearchParams(window.location.search); if (urlParams.has(paramName)) { // Inyección de variable GTM. // Al ser un tag HTML, GTM resuelve esto antes de inyectar el script. var isLoggedIn = 1; // Convertimos a int por seguridad y comprobamos if (Number(isLoggedIn) === 1) { var rawValue = urlParams.get(paramName); var decodedText = decodeB64(rawValue); if (decodedText) { sendNewsletterEvent(decodedText, "recibir newsletter"); } } } // ------------------------------------------------------- // 4. LÓGICA B: EVENT LISTENER (Interacción de usuario) // ------------------------------------------------------- // Optimización: Solo añadimos el listener si estamos en la ruta correcta if (window.location.pathname.indexOf('/newsletters/') > -1) { // Cacheamos la Regex fuera del listener para no recompilarla en cada clic var regexTitle = /nlSigupHandler\(\s*['"][^'"]+['"]\s*,\s*['"]([^'"]+)['"]\s*,/; document.addEventListener('change', function (e) { // Fail-fast: comprobaciones baratas primero var el = e.target; if (!el || el.type !== 'checkbox') return; var onchangeAttr = el.getAttribute('onchange'); if (!onchangeAttr) return; var match = onchangeAttr.match(regexTitle); if (match) { // match[1] es el texto capturado var action = el.checked ? "recibir newsletter" : "abandonar newsletter"; sendNewsletterEvent(match[1], action); } }, true); // UseCapture } })(); </script>
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
test1
test2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Comments
Confirm delete:
Do you really want to delete benchmark?