This commit is contained in:
Oliver
2025-10-08 16:44:39 -03:00
parent f593aca58e
commit 53d61fbaa6
2 changed files with 53 additions and 3 deletions

View File

@@ -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();
});

File diff suppressed because one or more lines are too long