nano-analytics
Version:
A lightweight and framework-agnostic analytics component to track user events and sessions in your web applications.
119 lines (117 loc) • 4.29 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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
var index_exports = {};
__export(index_exports, {
NanoAnalytics: () => NanoAnalytics
});
module.exports = __toCommonJS(index_exports);
var import_lit = require("lit");
var import_decorators = require("lit/decorators.js");
var NanoAnalytics = class extends import_lit.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([
(0, import_decorators.property)({ type: String })
], NanoAnalytics.prototype, "projectKey", 2);
__decorateClass([
(0, import_decorators.property)({ type: String })
], NanoAnalytics.prototype, "userId", 2);
NanoAnalytics = __decorateClass([
(0, import_decorators.customElement)("nano-analytics")
], NanoAnalytics);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
NanoAnalytics
});