From 978e281c11fa96ffe71429276085ad88242a12b8 Mon Sep 17 00:00:00 2001 From: Paresh Sharma Date: Fri, 6 Sep 2024 11:02:56 -0500 Subject: [PATCH] Fixed issue with calling key value pair function --- script.js | 122 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/script.js b/script.js index 3f1938d..f3e2502 100644 --- a/script.js +++ b/script.js @@ -1,73 +1,77 @@ -function observeGoogleAnalytics() { - const observer = new MutationObserver(() => { - const googleAnalyticsId = findKeyValueIterative(removeCircularReferences(window), 'client_id'); - if (googleAnalyticsId) { - observer.disconnect(); // Stop observing once the client_id is found - applicationCode(); // Run the main code - } - }); - - // Observe changes in the document and its subtree - observer.observe(document, { childList: true, subtree: true }); -} - -function applicationCode() { - function findKeyValueIterative(obj, key) { - if (obj === null || typeof obj !== 'object') { - return undefined; - } +function findKeyValueIterative(obj, key) { + if (obj === null || typeof obj !== 'object') { + return undefined; + } - const stack = [obj]; + const stack = [obj]; - while (stack.length > 0) { - const current = stack.pop(); + while (stack.length > 0) { + const current = stack.pop(); - if (current !== null && typeof current === 'object') { - if (key in current) { - return current[key]; - } + if (current !== null && typeof current === 'object') { + if (key in current) { + return current[key]; + } - for (let k in current) { - if (current.hasOwnProperty(k)) { - stack.push(current[k]); - } - } + for (let k in current) { + if (Object.prototype.hasOwnProperty.call(current, k)) { + stack.push(current[k]); } + } } - - return undefined; // Return undefined if the key is not found } - function removeCircularReferences(e, r = new WeakSet()) { - if (e !== null && typeof e === 'object') { - if (r.has(e)) return; + return undefined; // Return undefined if the key is not found +} + +function removeCircularReferences(e, r = new WeakSet()) { + if (e !== null && typeof e === 'object') { + if (r.has(e)) return; - r.add(e); + r.add(e); - if (Array.isArray(e)) { - return e.map(item => removeCircularReferences(item, r)); - } else { - const result = {}; - for (const key in e) { - if (Object.prototype.hasOwnProperty.call(e, key)) { - const value = e[key]; - if (typeof value === 'function') { - // Optionally exclude functions - continue; - } - result[key] = removeCircularReferences(value, r); - } - } - return result; + if (Array.isArray(e)) { + return e.map(item => removeCircularReferences(item, r)); + } else { + const result = {}; + for (const key in e) { + if (Object.prototype.hasOwnProperty.call(e, key)) { + const value = e[key]; + if (typeof value === 'function') { + // Optionally exclude functions + continue; + } + result[key] = removeCircularReferences(value, r); } + } + return result; } - return e; } + return e; +} - const applicantFormId = document.querySelector('input[name="application[applicant][id]"]')?.value - const applicationFormId = document.querySelector('input[name="application[application][id]"]')?.value - const googleAnalyticsId = findKeyValueIterative(removeCircularReferences(window), 'client_id') - const applicationEntrataId = typeof dataLayer !== 'undefined' && findKeyValueIterative(dataLayer, 'entrata_user_id') +function observeGoogleAnalytics() { + const googleAnalyticsId = findKeyValueIterative(removeCircularReferences(window), 'client_id'); + if (googleAnalyticsId) { + applicationCode(googleAnalyticsId); + } else { + const observer = new MutationObserver(() => { + const googleAnalyticsId = findKeyValueIterative(removeCircularReferences(window), 'client_id'); + if (googleAnalyticsId) { + observer.disconnect(); // Stop observing once the client_id is found + applicationCode(googleAnalyticsId); // Run the main code + } + }); + + // Observe changes in the document and its subtree + observer.observe(document, { childList: true, subtree: true }); + } +} + +function applicationCode(googleAnalyticsId) { + const applicantFormId = document.querySelector('input[name="application[applicant][id]"]')?.value; + const applicationFormId = document.querySelector('input[name="application[application][id]"]')?.value; + const applicationEntrataId = typeof dataLayer !== 'undefined' && findKeyValueIterative(dataLayer, 'entrata_user_id'); const data = { applicant_id_from_form: applicantFormId, @@ -86,15 +90,15 @@ function applicationCode() { mode: "cors", body: JSON.stringify(data), headers: { - "Content-Type": "application/json; charset=UTF-8", + "Content-Type": "application/json; charset=UTF-8", } }) - .then(response => console.log("Response:", response.json())) + .then(response => response.json()) .then(data => console.log("Success:", data)) .catch(error => { console.error("Error:", error); }); } -}; +} setTimeout(observeGoogleAnalytics, 3000); \ No newline at end of file