-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailSafe.js
118 lines (102 loc) · 4.14 KB
/
failSafe.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
let leadTrackingId = getFromLS('lead_tracking_id');
async function trackLead() {
// check to see if there is
if (leadTrackingId) {
// need to parse the leadTrackingId, becasue getting it from LS, will be a string
if (isNaN(parseInt(leadTrackingId))) {
// this means that the lead_tracking_id isn't valid
// delete it
clearFromLS('lead_tracking_id');
// recurse this function to get a new lead_tracking_id
return trackLead();
}
} else {
// need to generate an id for the lead
// first, get ipv4, zip, country, city, state, lat, lng
let userData = await fetch('https://geolocation-db.com/json/', { method: 'GET' })
.then(res => res.json()).then(res => res)
.catch(err => console.log(err));
// get the funnel url
let funnelUrl = window.location.href;
// getting the userAgent info
let userAgent = window.navigator.userAgent;
// is the user on mobile or not
let isMobile = userDeviceIsMobile(userAgent) ? 1 : 0;
// get the params from the url
const urlParams = new URLSearchParams(window.location.search);
// now we need to write this lead to the db, should return back an id
const data = {
user_data: userData ? JSON.stringify(userData) : null,
calendar_url: funnelUrl,
user_agent: userAgent ? userAgent : null,
mobile: isMobile,
fbcid: urlParams.get('fbcid'),
gclid: urlParams.get('gclid'),
attr_fp: urlParams.get('attr_fp'),
utm_source: urlParams.get('utm_source'),
utm_medium: urlParams.get('utm_medium'),
utm_campaign: urlParams.get('utm_campaign'),
utm_term: urlParams.get('utm_term'),
utm_content: urlParams.get('utm_content')
};
// archive the lead in the db
let response = await archiveLead(data, false);
if (!response.lead) return;
// set the id into localStorage, just in case the user refreshes or revisits the funnel
saveToLS('lead_tracking_id', response.lead);
// save the tracking id to global scope
leadTrackingId = getFromLS('lead_tracking_id');
return
}
}
async function updateLead(lastUpdate = false, questionAnswerObj) {
console.log(questionAnswerObj)
// if no inputvalues, retrun
if (!questionAnswerObj) return;
// if no lead tracking id, just return
if (!leadTrackingId) return;
// make sure the input_values is stringified
const inputValues = typeof questionAnswerObj == 'string' ? questionAnswerObj : JSON.stringify(questionAnswerObj);
// prep the data to be sent to the leads_backup db
const data = { id: +leadTrackingId, input_values: inputValues };
// send data to leads_backup db
archiveLead(data, true);
// clear localStorage if there are no more updates, HINT the last slide
if (lastUpdate === true) {
clearFromLS('lead_tracking_id');
}
return;
}
async function archiveLead(data, isUpdate) {
try {
// data should look like
// { id: 44555, input_values: JSON.stringify({ questionLabel: leadAnswer, etc... }) }
let config = {
method: "POST",
headers: {"Content-Type": "application/json", "Authorization":"Basic U2hpbmVBZG1pbjIwMjIhOlNoaW5lTm93MjAyMiE"},
body: JSON.stringify(data)
}
let res = await fetch('https://us-west2-adams-website-backend.cloudfunctions.net/archive', config)
.then(res => !isUpdate && res.json())
return res;
} catch (err) {
console.log(err);
}
}
function userDeviceIsMobile(userAgent) {
const toMatch = [ /Android/i, /webOS/i, /iPhone/i, /iPad/i, /iPod/i, /BlackBerry/i,/Windows Phone/i ];
return toMatch.some((toMatchItem) => {
return userAgent.match(toMatchItem);
});
}
function saveToLS(key, value) {
return localStorage.setItem(key, value);
}
function getFromLS(key) {
return localStorage.getItem(key);
}
function clearFromLS(key) {
return localStorage.removeItem(key);
}
// initialize fail safe
trackLead();