Skip to content

Commit

Permalink
Fixed issue with calling key value pair function
Browse files Browse the repository at this point in the history
  • Loading branch information
Paresh10 committed Sep 6, 2024
1 parent 762adb1 commit 978e281
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions script.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);

0 comments on commit 978e281

Please sign in to comment.