This commit is contained in:
Oliver
2025-10-09 08:14:28 -03:00
parent 53d61fbaa6
commit bd26988584
5 changed files with 59 additions and 22 deletions

View File

@@ -4,25 +4,32 @@ const CATEGORY_CONFIG = {
4: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // hosting
5: { showLocation: false, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/fd20e194-b821-4b1f-814f-7bb93ae16046" }, // Workshops
7: { showLocation: false, webhook: "" }, // modules
// Add more categories as needed
};
// --- Get URL parameters as an object ---
function getUrlParams() {
return Object.fromEntries(new URLSearchParams(window.location.search).entries());
// --- Helper: Get cookie by name ---
function getCookie(name) {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}
// --- Add UTM fields to form with defaults ---
// --- Helper: Set cookie if not already set ---
function setCookieIfEmpty(name, value, days = 90) {
if (!value) return;
if (getCookie(name)) return; // Don't overwrite
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${encodeURIComponent(value)}; expires=${date.toUTCString()}; path=/; SameSite=Lax`;
}
// --- Add UTM fields to form using cookies ---
function addUtmFields(form) {
const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
const urlParams = getUrlParams();
// Define default values
const defaults = {
utm_source: "homepage",
utm_medium: "direct",
utm_campaign: "none"
utm_campaign: "none",
utm_term: "",
utm_content: ""
};
utmParams.forEach(param => {
@@ -33,8 +40,8 @@ function addUtmFields(form) {
input.name = param;
form.appendChild(input);
}
// Use URL param if exists, otherwise fallback to default, else empty string
input.value = urlParams[param] || defaults[param] || "";
// First check cookie, then fallback to default
input.value = getCookie(param) || defaults[param];
});
}
@@ -111,14 +118,12 @@ function openModal(productHref) {
form.style.display = "flex";
confirmation.style.display = "none";
// Parse URL /<id>/<price>/<category>/<product>
const parts = productHref.split("/").filter(Boolean);
const [id, price, category, ...productParts] = parts;
const product = decodeURIComponent(productParts.join("/"));
const categoryNum = parseInt(category, 10);
const config = CATEGORY_CONFIG[categoryNum] || { showLocation: false, webhook: "" };
// Handle location field visibility
const locationSelect = document.getElementById("locationSelect");
if (config.showLocation) {
locationSelect.style.display = "block";
@@ -129,20 +134,14 @@ function openModal(productHref) {
locationSelect.value = "";
}
// Update form fields
form.querySelector('input[name="id"]').value = id || "";
form.querySelector('input[name="price"]').value = price || "0";
form.querySelector('input[name="category"]').value = category || "";
form.querySelector('input[name="product"]').value = product || "Unknown";
// Store webhook dynamically for submission
form.dataset.webhook = config.webhook || "";
// Update display text
modal.querySelector("#productText").textContent = `You will buy ${product} for $${price}.`;
// Add UTM fields
addUtmFields(form);
addUtmFields(form); // Read from cookie or defaults
}
// --- Handle Form Submit ---