Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send thumbsUp/thumbsDown ratings to Grist telemetry, and refactor. #303

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions overrides/base.html
Original file line number Diff line number Diff line change
@@ -123,49 +123,60 @@
{%- endif %}
</div>

<div class="feedback-box">
<div class="feedback-box-title">Give Feedback</div>
<div class="feedback-box-question">Was this article helpful?</div>
<div class="feedback-box-buttons">
<button class="feedback-box-button" data-rating="thumbsUp">&#x1F44D;</button>
<button class="feedback-box-button" data-rating="thumbsDown">&#x1F44E;</button>
</div>
<div class="feedback-box-response">
Thank you for letting us know!
</div>
</div>

<script>
function giveFeedback(event) {
document.getElementsByClassName('feedback-box-buttons')[0].style.display = 'none';
document.getElementsByClassName('feedback-box-response')[0].style.display = 'block';
const page = window.location.href;

// No formal telemetry endpoint yet, so use /api/log for practice.
// Results can still be queried just less systematically.
const url = new URL('https://docs.getgrist.com/api/log');
// Duplicate info in query parameters so it is logged with URL in case
// body isn't logged (hard to test until live on *.getgrist.com).
url.searchParams.set('event', event);
url.searchParams.set('page', page);

fetch(url.href, {
function initFeedbackButtons() {
const serverUrl = "https://docs.getgrist.com";
const feedbackBox = document.querySelector('.feedback-box');
const buttons = document.querySelectorAll('.feedback-box-button');
document.querySelector('.feedback-box-buttons').addEventListener('click', giveFeedback);

async function giveFeedback(event) {
const rating = event.target.dataset.rating;
if (!rating) { return; }
buttons.forEach(b => { b.disabled = true; });
try {
console.log("Sending rating to " + serverUrl + '/api/telemetry');
const resp = await window.fetch(serverUrl + '/api/telemetry', makePayload(rating));
if (!resp.ok) { throw new Error(resp.statusText); }
} catch (err) {
console.warn('Failed to report feedback', err);
document.querySelector('.feedback-box-response').textContent = String(err);
} finally {
buttons.forEach(b => { b.disabled = false; });
feedbackBox.classList.add('feedback-submitted');
}
}

function makePayload(rating) {
const url = window.location.href;
return {
method: 'POST',
body: JSON.stringify({
event,
docId: null,
page,
source: 'support',
event: 'ratedHelpCenterArticle',
metadata: {full: {url, rating}},
}),
credentials: 'include',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
}
}).catch(e => {
console.warn('Failed to report feedback', e);
});
},
};
}
}
initFeedbackButtons();
</script>
<div class="feedback-box">
<div class="feedback-box-title">Give Feedback</div>
<div class="feedback-box-question">Was this article helpful?</div>
<div class="feedback-box-buttons">
<div class="feedback-box-button" onclick="javascript:giveFeedback('thumbsUp')">&#x1F44D;</div>
<div class="feedback-box-button" onclick="javascript:giveFeedback('thumbsDown')">&#x1F44E;</div>
</div>
<div class="feedback-box-response" style="display: none;">
Thank you for letting us know!
</div>
</div>

{%- endif -%}

16 changes: 14 additions & 2 deletions overrides/css/base.css
Original file line number Diff line number Diff line change
@@ -458,22 +458,34 @@ html {
border-radius: 5px;
padding: 5px 10px;
cursor: pointer;
background-color: transparent;
}

.feedback-box-button:hover {
.feedback-box-button:disabled {
opacity: 0.5;
}

.feedback-box-button:hover:enabled {
background-color: #ccc;
}

.feedback-box-button:active {
.feedback-box-button:active:enabled {
background-color: #aaa;
}

.feedback-box-response {
display: none;
font-size: 150%;
color: #888;
margin: 10px;
}

.feedback-submitted .feedback-box-buttons {
display: none;
}
.feedback-submitted .feedback-box-response {
display: block;
}

/***********************************************************************
* The rest is taken from base.css from mkdocs.