loadEventTracking({ origins: ["act.vot-er.org", "vot-er.org/act", "vot-er.org/vota"], sites: [ { href: "https://voter.turbovote.org", param: "r" }, { href: "https://act.vot-er.org", param: "orgId" }, { href: "https://www.vote411.org", param: "r" }, { href: "https://www.patientvoting.com", param: "r" }, ], }); function loadEventTracking(options) { options = options || {}; const { origins, sites, eventsURL } = options; // Grab the ref and organization ID params from the URL const params = new URLSearchParams(window.location.search); const urlRef = params.get("ref"); const urlOrganizationId = params.get("organizationId"); const urlUserId = params.get("userId"); const urlSrc = params.get("src"); // Save them to LocalStorage in case they go to another page (e.g., changing language) if (urlRef) localStorage.setItem("ref", urlRef); if (urlOrganizationId) localStorage.setItem("organizationId", urlOrganizationId); if (urlUserId) localStorage.setItem("userId", urlUserId); if (urlSrc) localStorage.setItem("src", urlSrc); // Grab them from LocalStorage in case they were set from another page const ref = localStorage.getItem("ref"); const organizationId = localStorage.getItem("organizationId"); const userId = localStorage.getItem("userId"); const src = localStorage.getItem("src"); // Create a reusable function for sending tracking events const track = function (data) { return fetch(eventsURL || "https://events.vot-er.org/", { method: "POST", mode: "no-cors", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); }; if (origins && !origins.some((url) => window.location.href.includes(url))) return; // Track the visit to this page track({ type: "visit", destination: window.location.href, ref: ref, }); // Track visits to other pages sites.forEach(function (site) { const siteHref = site.href; const links = Array.from(document.querySelectorAll("a")).filter((link) => link.href.includes(siteHref) ); links.forEach(function (link) { // Add the organization ID to URL if applicable if (site.param && (organizationId || userId || src)) { const url = new URL(link); const linkParams = new URLSearchParams(url.search); linkParams.set(site.param, [organizationId, userId, src].join(",")); link.href = `${url.origin}${url.pathname}?${linkParams.toString()}`; } // Track clicks link.addEventListener("click", function (event) { event.preventDefault(); event.stopPropagation(); track({ type: "link", destination: link.href, ref: ref, }) .catch((err) => { console.log("Error: ", err); }) .finally(() => { window.location = link.href; }); }); }); }); }
top of page
bottom of page