Skip to content

Instantly share code, notes, and snippets.

@alecbw
Last active September 24, 2020 06:37
Show Gist options
  • Save alecbw/2db7be664a567ee6cc50b892a05d1e1f to your computer and use it in GitHub Desktop.
Save alecbw/2db7be664a567ee6cc50b892a05d1e1f to your computer and use it in GitHub Desktop.
A JS script that listens for a series of buttons and makes an API call to increment a counter
feedbackButtons = document.getElementsByClassName("feedbackButton")
for (i = 0; i < feedbackButtons.length; i++) {
feedbackButtons[i].addEventListener("click", trackClick);
}
function trackClick(event) {
var page = window.location.pathname
var button = event.target.id
fetch('https://foobar.execute-api.us-west-1.amazonaws.com/prod/feedback?page=' + page + '&button=' + button)
}
// example button:
// <button onclick="location.href = 'http://yoursite.com/followup-questions';" id="good" class="feedbackButton" >Good</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment