UNPKG

trakker-analytics

Version:

Trakker is a tool to measure and analyze website's statistics

38 lines (37 loc) 1.27 kB
import axios from "axios"; const ENDPOINT = "https://trakker-analytics.vercel.app/api/event"; export default class Trakker { #apiKey; constructor(api_key) { this.#apiKey = api_key; } initialize() { const _this = this; const apiCall = (e) => { axios.post(ENDPOINT, { api_key: _this.#apiKey, event_type: e, }); }; window.addEventListener("load", (e) => { if (!localStorage.getItem("visited")) { localStorage.setItem("visited", "true"); apiCall("first_visit"); } if (!document.referrer.includes(location.origin)) apiCall("session_start"); apiCall("page_view"); }); window.addEventListener("error", () => { apiCall("error"); }); const allButtons = document.querySelectorAll("button"); allButtons.forEach((b) => b.addEventListener("click", () => { apiCall("click"); })); const allAnchors = document.querySelectorAll("a[href]"); allAnchors.forEach((b) => b.addEventListener("click", () => { apiCall("link_click"); })); } }