UNPKG

nano-analytics

Version:

A lightweight and framework-agnostic analytics component to track user events and sessions in your web applications.

98 lines (96 loc) 3.35 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; // src/index.ts import { LitElement } from "lit"; import { property, customElement } from "lit/decorators.js"; var NanoAnalytics = class extends LitElement { constructor() { super(); this.projectKey = ""; this.userId = ""; this.trackPageView = () => { this.sendToApi({ eventType: "page_view", page_title: document.title, page_location: window.location.href, page_path: window.location.pathname }); }; this.trackEvent = (e) => { const customEvent = e; this.sendToApi({ eventType: customEvent.detail.name, event_data: customEvent.detail.data }); }; this.sessionId = typeof localStorage !== "undefined" && localStorage.getItem("nanoAnalyticsSessionId") || typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID() || Math.random().toString(36).slice(2); if (typeof localStorage !== "undefined") { localStorage.setItem("nanoAnalyticsSessionId", this.sessionId); } } connectedCallback() { super.connectedCallback(); this.trackPageView(); window.addEventListener("popstate", this.trackPageView.bind(this)); window.addEventListener( "nanoAnalyticsEvent", this.trackEvent.bind(this) ); } disconnectedCallback() { super.disconnectedCallback(); window.removeEventListener("popstate", this.trackPageView.bind(this)); window.removeEventListener( "nanoAnalyticsEvent", this.trackEvent.bind(this) ); } sendToApi(data) { fetch("https://www.nanosights.dev/api/tags/analytics", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(__spreadValues({ projectKey: this.projectKey, sessionId: this.sessionId, userId: this.userId, userAgent: navigator.userAgent, referrer: document.referrer }, data)) }); } }; __decorateClass([ property({ type: String }) ], NanoAnalytics.prototype, "projectKey", 2); __decorateClass([ property({ type: String }) ], NanoAnalytics.prototype, "userId", 2); NanoAnalytics = __decorateClass([ customElement("nano-analytics") ], NanoAnalytics); export { NanoAnalytics };