repos
/ analytics-django master

analytics-django

mirror archived upstream

Self-hostable website analytics on Django: a straightforward collector API, dashboards, a world map, and PDF reports.

analyticsdjangodockerhandcodedpythonself-hostedsqliteviteweb-analytics

1.2 KB · 37 lines · JavaScript Raw History
 1document.addEventListener("DOMContentLoaded", function () {
 2  /**
 3   * If any of the checkboxes in the form "#custom-card-form" are changed then
 4   * get the data from the form in the format:
 5   * [{"event": "<field_name>", "value": "<field_value>"}]
 6   * and send it to the server in the body in JSON format to the URL:
 7   * const url = new URL(window.location.href) + /custom-cards/
 8   */
 9  const form = document.getElementById("custom-card-form");
10  if (!form) { return; }
11  form.addEventListener("change", function () {
12    const formData = new FormData(form);
13    const data = [];
14    for (const [key, value] of formData.entries()) {
15      if (key === "csrfmiddlewaretoken") {
16        continue;
17      }
18      data.push({
19        event: key,
20        value: value === "on" ? true : false,
21      });
22    }
23    let url = new URL(window.location.href);
24    url = url.origin + url.pathname + "cards/";
25    fetch(url, {
26      method: "POST",
27      body: JSON.stringify(data),
28      headers: {
29        "Content-Type": "application/json",
30        "X-CSRFToken": form.querySelector("input[name=csrfmiddlewaretoken]").value,
31      },
32    }).then(function () {
33      window.location.reload();
34    });
35  });
36});