-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleadTimestamp.js
More file actions
107 lines (92 loc) · 4.35 KB
/
leadTimestamp.js
File metadata and controls
107 lines (92 loc) · 4.35 KB
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
<script>
// PLEASE ONLY USE JAVASCRIPT!
// document.querySelector('yourElement');
// generates a timestamp if one isn't available
// adds timestamp ( unique ID ) to hidden form field
// option to generate hidden form field if one does not exist
// option to append generated form field to existing element
function timestampFormFill(
formFieldSelector, // the form field you want to insert the TID into
appendSelector, // optional ( only works if failsafe is TRUE )
failSafeSetting, // True / False
appendSetting // True / False
){
// ------------------------------------------- function
var failSafe = failSafeSetting; // will generate a form field if one isn't available
var append = appendSetting; // choose wether the formfield should be appended to a master form
// ============= COOKIES ============= //
function getCookie(cname) {
// ------------------------ getcookie
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
// ------------------------ getcookie
}
if(getCookie("leadTimestamp").length <= 0){
// ---------------------------------------- timestamp
// Generate a timestamp id for this form page
var leadTimestamp = new Date().getTime();
// Set a cookie to hold the leadTimestamp value so we can retrieve on Thank You page
var now = new Date();
var time = now.getTime();
var expireTime = time + 1000*60*15;
now.setTime(expireTime);
document.cookie="leadTimestamp="+leadTimestamp+';expires='+now.toGMTString()+';path=/';
console.log("leadTimestamp generated");
// ---------------------------------------- timestamp
}
var cookieTimestamp = getCookie("leadTimestamp");
// ============= FORM FILL ============= //
if( formFieldSelector != null ){
// ------------------------------------------------------------ check selector
// if selector exists
// if timestamp does not exist in form field
if(formFieldSelector.value.length <= 0){
// --------------------------------------------- formfill
// add timestamp to selector from cookie
formFieldSelector.value = cookieTimestamp;
console.log("leadTimestamp added to form");
// --------------------------------------------- formfill
} else if(formFieldSelector.value.length > 0){
// --------------------------------------------- value exists
console.log("leadTimestamp exists in form")
// --------------------------------------------- value exists
}
// ------------------------------------------------------------ check selector
} else if( failSafe == true ){
// ---------------------- failSafe
if( formFieldSelector == null ){
// --------------------------------------------------------- check selector
// if selector does not exist
if( append == true ){
// ------------------ append
var inputLEAD = document.createElement("INPUT");
appendSelector.appendChild(inputLEAD);
inputLEAD.setAttribute("id", "field_transaction_id");
inputLEAD.setAttribute("type", "hidden");
console.log("hidden form field appended to form");
console.log("leadTimestamp added to form");
// ------------------ append
} else {
// ----- create in body
var inputLEAD = document.createElement("INPUT");
document.body.appendChild(inputLEAD);
inputLEAD.setAttribute("id", "field_transaction_id");
inputLEAD.setAttribute("type", "hidden");
console.log("hidden form field appended to body");
console.log("leadTimestamp added to form");
// ----- create in body
}
document.getElementById('field_transaction_id').value = cookieTimestamp;
// --------------------------------------------------------- check selector
}
// ---------------------- failSafe
}
// ------------------------------------------- function
}
</script>