Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
522 B
1
Indexable
Never
function trackEvent(event) {
  var trkUrl = "/track"
  var data = window.btoa(JSON.stringify(event));

  // Creating a 1x1 image is a method for sending GET requests supported by all browsers:
  var img = document.createElement("img");
  img.width = 1;
  img.height = 1;
  img.src = trkUrl + "?data=" + data;

  return img;
}

trackEvent({ pageUrl: window.location.href, type: "pageview" });

document.addEventListener("submit", function(e) {
  trackEvent({ type: "form-submit", formId: e.target.getAttribute("id") });
});