On click does not bind to right click

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the javascript category.

Last Updated: 2021-05-15

In order to combat fraud on a website, we tracked when certain digital assets were downloaded. I used the following code, but discovered that it was under-reporting by a large margin:

const downloadLinks = document.querySelectorAll(
  "a.download_link_for_tracking"
)

downloadLinks.forEach(downloadLink => {
  downloadLink.addEventListener("click", trackDownload)
})

The issue was that there are two ways to download files:

How to fix? An approximate (but incomplete solution) is to add an additional event listener on contextmenu:

downloadLinks.forEach(downloadLink => {
  downloadLink.addEventListener("click", trackDownload)
  downloadLink.addEventListener("contextmenu", trackDownload)
})

Lesson

Click events are not triggered by right clicks.