From 53d61fbaa692fd7038b9d1a895ac91fd3c355c33 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 8 Oct 2025 16:44:39 -0300 Subject: [PATCH] utm --- public/tryModal.js | 54 ++++++++++++++++++++++++++++++++++++++++-- public/tryModal.min.js | 2 +- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/public/tryModal.js b/public/tryModal.js index a53ddf0..aeccec6 100644 --- a/public/tryModal.js +++ b/public/tryModal.js @@ -1,5 +1,44 @@ const TRYNOW_WEBHOOK_URL = "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a"; +// --- Helper: Get URL parameters --- +function getUrlParams() { + const params = {}; + window.location.search + .substring(1) + .split("&") + .forEach(pair => { + if (pair) { + const [key, value] = pair.split("="); + params[decodeURIComponent(key)] = decodeURIComponent(value || ""); + } + }); + return params; +} + +// --- Add UTM fields to form --- +function addUtmFields(form) { + const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]; + const urlParams = getUrlParams(); + + const defaults = { + utm_source: "homepage", + utm_medium: "direct", + utm_campaign: "none" + }; + + utmParams.forEach(param => { + let input = form.querySelector(`input[name="${param}"]`); + if (!input) { + input = document.createElement("input"); + input.type = "hidden"; + input.name = param; + form.appendChild(input); + } + input.value = urlParams[param] || defaults[param] || ""; + }); +} + +// --- Create modal --- function tryNow_createModal() { const modal = document.createElement("div"); modal.id = "trynowModal"; @@ -92,6 +131,10 @@ function tryNow_createModal() { document.body.appendChild(modal); + // Add UTM fields after form exists + const form = modal.querySelector("#trynowForm"); + addUtmFields(form); + // Close modal handlers document.getElementById("trynowCloseModal").onclick = () => { modal.style.display = "none"; }; document.getElementById("trynowCloseConfirmation").onclick = () => { @@ -103,12 +146,17 @@ function tryNow_createModal() { return modal; } +// --- Handle form submission --- function tryNow_handleFormSubmit() { const form = document.getElementById("trynowForm"); const confirmation = document.getElementById("trynowConfirmation"); form.addEventListener("submit", async (e) => { e.preventDefault(); + + // Update UTM fields in case URL changed + addUtmFields(form); + const data = {}; new FormData(form).forEach((value, key) => (data[key] = value)); @@ -133,6 +181,7 @@ function tryNow_handleFormSubmit() { }); } +// --- Attach "Try Now" buttons --- function tryNow_attachButtons() { const buttons = Array.from(document.querySelectorAll("a, button")); buttons.forEach(btn => { @@ -142,12 +191,14 @@ function tryNow_attachButtons() { const modal = document.getElementById("trynowModal"); if (modal) { modal.style.display = "flex"; + // Reset modal state const form = document.getElementById("trynowForm"); const confirmation = document.getElementById("trynowConfirmation"); if (form && confirmation) { form.style.display = "flex"; confirmation.style.display = "none"; + addUtmFields(form); // refresh UTM fields each open } } }); @@ -155,11 +206,10 @@ function tryNow_attachButtons() { }); } - +// --- Initialize --- document.addEventListener("DOMContentLoaded", () => { tryNow_createModal(); tryNow_handleFormSubmit(); tryNow_attachButtons(); }); - diff --git a/public/tryModal.min.js b/public/tryModal.min.js index fb73aae..49dd43a 100644 --- a/public/tryModal.min.js +++ b/public/tryModal.min.js @@ -1 +1 @@ -const TRYNOW_WEBHOOK_URL="https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a";function tryNow_createModal(){const n=document.createElement("div");return n.id="trynowModal",n.style.position="fixed",n.style.top="0",n.style.left="0",n.style.width="100%",n.style.height="100%",n.style.backgroundColor="rgba(0,0,0,0.6)",n.style.display="none",n.style.justifyContent="center",n.style.alignItems="center",n.style.zIndex="1000",n.innerHTML='\n
\n ×\n \n

Order Details

\n\n
\n \n\n \n\n \n\n \n
\n\n
\n

Thank you for your submission! 🎉

\n \n
\n
\n ',document.body.appendChild(n),document.getElementById("trynowCloseModal").onclick=()=>{n.style.display="none"},document.getElementById("trynowCloseConfirmation").onclick=()=>{document.getElementById("trynowConfirmation").style.display="none",n.style.display="none"},n.onclick=t=>{t.target===n&&(n.style.display="none")},n}function tryNow_handleFormSubmit(){const n=document.getElementById("trynowForm"),t=document.getElementById("trynowConfirmation");n.addEventListener("submit",async o=>{o.preventDefault();const e={};new FormData(n).forEach((n,t)=>e[t]=n);try{(await fetch(TRYNOW_WEBHOOK_URL,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})).ok?(n.style.display="none",t.style.display="block",n.reset()):alert("Failed to submit form.")}catch(n){console.error(n),alert("Error submitting form.")}})}function tryNow_attachButtons(){Array.from(document.querySelectorAll("a, button")).forEach(n=>{n.textContent&&"Try Now"===n.textContent.trim()&&n.addEventListener("click",n=>{n.preventDefault();const t=document.getElementById("trynowModal");if(t){t.style.display="flex";const n=document.getElementById("trynowForm"),o=document.getElementById("trynowConfirmation");n&&o&&(n.style.display="flex",o.style.display="none")}})})}document.addEventListener("DOMContentLoaded",()=>{tryNow_createModal(),tryNow_handleFormSubmit(),tryNow_attachButtons()}); \ No newline at end of file +const TRYNOW_WEBHOOK_URL="https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a";function getUrlParams(){const n={};return window.location.search.substring(1).split("&").forEach(t=>{if(t){const[e,o]=t.split("=");n[decodeURIComponent(e)]=decodeURIComponent(o||"")}}),n}function addUtmFields(n){const t=getUrlParams(),e={utm_source:"homepage",utm_medium:"direct",utm_campaign:"none"};["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(o=>{let r=n.querySelector(`input[name="${o}"]`);r||(r=document.createElement("input"),r.type="hidden",r.name=o,n.appendChild(r)),r.value=t[o]||e[o]||""})}function tryNow_createModal(){const n=document.createElement("div");n.id="trynowModal",n.style.position="fixed",n.style.top="0",n.style.left="0",n.style.width="100%",n.style.height="100%",n.style.backgroundColor="rgba(0,0,0,0.6)",n.style.display="none",n.style.justifyContent="center",n.style.alignItems="center",n.style.zIndex="1000",n.innerHTML='\n
\n ×\n \n

Order Details

\n\n
\n \n\n \n\n \n\n \n
\n\n
\n

Thank you for your submission! 🎉

\n \n
\n
\n ',document.body.appendChild(n);return addUtmFields(n.querySelector("#trynowForm")),document.getElementById("trynowCloseModal").onclick=()=>{n.style.display="none"},document.getElementById("trynowCloseConfirmation").onclick=()=>{document.getElementById("trynowConfirmation").style.display="none",n.style.display="none"},n.onclick=t=>{t.target===n&&(n.style.display="none")},n}function tryNow_handleFormSubmit(){const n=document.getElementById("trynowForm"),t=document.getElementById("trynowConfirmation");n.addEventListener("submit",async e=>{e.preventDefault(),addUtmFields(n);const o={};new FormData(n).forEach((n,t)=>o[t]=n);try{(await fetch(TRYNOW_WEBHOOK_URL,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).ok?(n.style.display="none",t.style.display="block",n.reset()):alert("Failed to submit form.")}catch(n){console.error(n),alert("Error submitting form.")}})}function tryNow_attachButtons(){Array.from(document.querySelectorAll("a, button")).forEach(n=>{n.textContent&&"Try Now"===n.textContent.trim()&&n.addEventListener("click",n=>{n.preventDefault();const t=document.getElementById("trynowModal");if(t){t.style.display="flex";const n=document.getElementById("trynowForm"),e=document.getElementById("trynowConfirmation");n&&e&&(n.style.display="flex",e.style.display="none",addUtmFields(n))}})})}document.addEventListener("DOMContentLoaded",()=>{tryNow_createModal(),tryNow_handleFormSubmit(),tryNow_attachButtons()}); \ No newline at end of file