-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (89 loc) · 3.03 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function findKeyValueIterative(obj, key) {
if (obj === null || typeof obj !== 'object') {
return undefined;
}
const stack = [obj];
while (stack.length > 0) {
const current = stack.pop();
if (current !== null && typeof current === 'object') {
if (key in current) {
return current[key];
}
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;
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;
}
}
return e;
}
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,
application_id_from_form: applicationFormId,
google_analytics_id: googleAnalyticsId,
application_id_from_dataLayer: applicationEntrataId,
origin_url: window.location.origin,
timestamp: new Date().toISOString(),
};
if (applicantFormId || applicationFormId || applicationEntrataId) {
const proxyUrl = "https://scion-proxy-server-f85cd32d7f03.herokuapp.com/proxy/";
fetch(proxyUrl, {
method: "POST",
mode: "cors",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF-8",
}
})
.then(response => response.json())
.then(data => console.log("Success:", data))
.catch(error => {
console.error("Error:", error);
});
}
}
setTimeout(observeGoogleAnalytics, 3000);