userTrack
Search…
Feedback form (polls)
A basic example of a poll form with a question and a simple Yes/No answer. The response is stored as a userTrack tag attached to the current user. You can also modify the code to use events instead and store more data (like the current page the user is on, the question text, username, etc.).

Example

A simple poll functionality implemented using userTrack
You can test in this JSFiddle (note that this fiddle does not actually store data in userTrack).

HTML:

Add this before the end of the </body> tag.
1
<div id="ust-poll">
2
<div id="ust-poll__question">
3
Is this page useful?
4
</div>
5
6
<div id="ust-poll__answers">
7
<button class="ust-poll__answer" value="no">No</button>
8
<button class="ust-poll__answer" value="yes">Yes</button>
9
</div>
10
11
<div id="ust-poll__close">X</div>
12
</div>
Copied!

JavaScript:

Add this in a <script> tag or include it as an external JavaScript file somewhere after the userTrack tracker is included.
1
(function() {
2
if (localStorage) {
3
if (localStorage['ust-poll-closed']) return;
4
}
5
var prevOnLoaded = UST.onLoaded;
6
UST.onLoaded = function () {
7
prevOnLoaded && prevOnLoaded();
8
var poll = document.querySelector('#ust-poll');
9
10
poll.style.display = 'inline-block';
11
poll.style.transform = 'translateX(-50%) scaleY(1)';
12
13
function close() {
14
poll.style.display = 'none';
15
localStorage && (localStorage['ust-poll-closed'] = true);
16
}
17
18
document.querySelector('#ust-poll__close').addEventListener('click',close);
19
20
function clickedAnswer(e) {
21
close();
22
// Add a tag to the user like `poll_no` or `poll_yes`
23
// You could also use addEvent instead to store more data
24
UST.addTag('poll_' + e.currentTarget.value);
25
}
26
27
var answers = document.querySelectorAll('.ust-poll__answer');
28
for (var i = 0; i < answers.length; ++i) {
29
answers[i].addEventListener('click', clickedAnswer);
30
}
31
}
32
})();
Copied!

CSS:

Include this in a <style> tag or as an external CSS file.
1
#ust-poll {
2
display: inline-block;
3
background: #1289A9;
4
padding: 1rem 2rem;
5
font-size: 1rem;
6
font-weight: bold;
7
color: white;
8
text-align: center;
9
border-radius: 10px;
10
max-width: 400px;
11
position: fixed;
12
bottom: 1rem;
13
left: 50%;
14
transform-origin: bottom;
15
transform: translateX(-50%) scaleY(0);
16
transition: transform 0.2s ease-in-out;
17
pointer-events: visible;
18
}
19
​
20
#ust-poll__question {
21
padding-bottom: 1rem;
22
}
23
​
24
#ust-poll__answers {
25
display: flex;
26
justify-content: space-between;
27
}
28
​
29
.ust-poll__answer {
30
background: none;
31
border: 2px solid white;
32
padding: 0.5rem 1rem;
33
color: white;
34
width: 45%;
35
cursor: pointer;
36
}
37
​
38
.ust-poll__answer:hover {
39
background-color: rgba(255,255,255,0.1);
40
}
41
​
42
#ust-poll__close {
43
position: absolute;
44
right: 0.5rem;
45
top: 0.5rem;
46
font-family: Helvetica, sans-serif;
47
font-weight: normal;
48
cursor: pointer;
49
font-size: 0.8rem;
50
opacity: 0.5;
51
}
Copied!

JavaScript one-liner

Alternatively, you can combine the HTML, CSS and JS in a single JS snippet and include it:
1
(function(){
2
var question='Is this page useful?';
3
if(localStorage){if(localStorage['ust-poll-closed']){return}}var sheet=document.createElement('style');sheet.innerHTML='#ust-poll{display:inline-block;background:#1289A9;padding:1rem 2rem;font-size:1rem;font-weight:700;color:#fff;text-align:center;border-radius:10px;max-width:400px;position:fixed;bottom:1rem;left:50%;transform-origin:bottom;transform:translateX(-50%) scaleY(0);transition:transform .2s ease-in-out;pointer-events:visible}#ust-poll__question{padding-bottom:1rem}#ust-poll__answers{display:flex;justify-content:space-between}.ust-poll__answer{background:none;border:2px solid #fff;padding:.5rem 1rem;color:#fff;width:45%;cursor:pointer}.ust-poll__answer:hover{background-color:rgba(255,255,255,0.1)}#ust-poll__close{position:absolute;right:.5rem;top:.5rem;font-family:Helvetica,sans-serif;font-weight:400;cursor:pointer;font-size:.8rem;opacity:.5}';document.body.appendChild(sheet);var div=document.createElement('div');div.innerHTML='<div id=ust-poll><div id=ust-poll__question>'+question+'</div><div id=ust-poll__answers><button class=ust-poll__answer value=no>No</button> <button class=ust-poll__answer value=yes>Yes</button></div><div id=ust-poll__close>X</div></div>';document.body.appendChild(div.firstChild);var prevOnLoaded=UST.onLoaded;UST.onLoaded=function(){prevOnLoaded&&prevOnLoaded();var poll=document.querySelector('#ust-poll');poll.style.display='inline-block';poll.style.transform='translateX(-50%) scaleY(1)';function close(){poll.style.display='none';localStorage&&(localStorage['ust-poll-closed']=true)}document.querySelector('#ust-poll__close').addEventListener('click',close);function clickedAnswer(e){close();UST.addTag('poll_'+e.currentTarget.value)}var answers=document.querySelectorAll('.ust-poll__answer');for(var i=0;i<answers.length;i+=1){answers[i].addEventListener('click',clickedAnswer)}}
4
})();
Copied!
Last modified 3mo ago