forked from lytics/pathforajs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback-post.js
99 lines (94 loc) · 2.8 KB
/
callback-post.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
(function(window) {
var customTrackingWidget = window.pathfora.Form({
id: "custom-tracking-widget",
layout: "slideout",
position: "bottom-left",
className: "custom-tracking-widget",
headline: "Sign up for our Newsletter",
formElements: [
{
"type": "text",
"required": true,
"label": "Email Address",
"name": "email"
},
{
"type": "checkbox-group",
"required": true,
"label": "Which feeds would you like to subscribe to?",
"name": "subscription_feeds",
"values": [
{
"label": "Beauty & Perfumes",
"value": "beauty"
},
{
"label": "Electronics",
"value": "electronics"
},
{
"label": "Fashion",
"value": "fashion"
}
]
}
],
okMessage: "Subscribe",
formStates: {
success: {
headline: "Success",
msg: "Thanks for signing up, you can expect to receive updates in your inbox soon.",
delay: 0
},
error: {
headline: "Error",
msg: "There was an issue submitting your subscription. Please try again or <a href=\"/contact\">contact us</a> if the issue persists."
}
},
confirmAction: {
waitForAsyncResponse: true,
callback: function (event, payload, cb) {
// validate the data exists
if (!payload.data) {
return;
}
// reformat the payload for the request
var output = payload.data.reduce(function(acc, field) {
// multiple value - store as an array
if (acc.hasOwnProperty(field.name)) {
// make an array with the existing and current values
var existingValue = acc[field.name];
var arr = Array.isArray(existingValue) ? existingValue : [existingValue];
arr.push(field.value);
acc[field.name] = arr;
// single value
} else {
acc[field.name] = field.value;
}
return acc;
}, {});
// make the request to the third party
var xhr = new XMLHttpRequest();
xhr.onload = function () {
// if successful load the form success state
if (this.status === 200) {
cb(true);
// otherwise trigger the form error state
} else {
cb(false);
}
};
xhr.open("POST", "http://yourwebsite.com/custom/endpoint", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(output));
}
}
});
var modules = {
target: [{
segment: "high_intensity",
widgets: [customTrackingWidget]
}]
};
window.pathfora.initializeWidgets(modules);
}(window));