📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
error tracking, pageview metadata, dev mode, etc.
Blue Mouse8a3d52a124ce
diff --git a/src/index.ts b/src/index.tsindex ff81e5cd3..429ec493f 100644--- a/src/index.ts+++ b/src/index.ts@@ -3,9 +3,9 @@ import { isInBrowser } from './utils'export interface LibOptions {/**- * When set to `true`, all tracking logs will be printed to console and localhost events will be sent to server.+ * When set to `true`, all tracking logs will be printed to console.*/- debug?: boolean+ devMode?: boolean/*** When set to `true`, the tracking library won't send any data to server.@@ -23,12 +23,6 @@ export interface LibOptions {* This param is useful when tracking single-page landing websites.*/unique?: boolean-- /** A list of Regular Expressions or string pathes to ignore. */- ignore?: Array<string | RegExp>-- /** Do not send paths from ignore list to API. If set to `false`, the page view information will be sent to the Swetrix API, but the page will be displayed as a 'Redacted page' in the dashboard. */- doNotAnonymise?: boolean}export interface TrackEventOptions {@@ -60,6 +54,11 @@ export interface TrackEventOptions {/** A campaign of the event (e.g. utm_campaign GET parameter) */ca?: string++ /** Event-related metadata object with string values. */+ meta?: {+ [key: string]: string+ }}export interface PerformanceMetrics {@@ -120,13 +119,45 @@ export interface TrackPageViewOptions {/** An object with performance metrics related to the page load. See Performance metrics for more details */perf?: PerformanceMetrics++ /** Pageview-related metadata object with string values. */+ meta?: {+ [key: string]: string+ }+}++export interface TrackErrorOptions {+ /**+ * Error name (e.g. ParseError).+ */+ name: string++ /**+ * Error message (e.g. Malformed input).+ */+ message: string | null | undefined++ /**+ * On what line in your code did the error occur (e.g. 1520)+ */+ lineno: number | null | undefined++ /**+ * On what column in your code did the error occur (e.g. 26)+ */+ colno: number | null | undefined++ /**+ * In what file did the error occur (e.g. https://example.com/broken.js)+ */+ filename: string | null | undefined}const DEFAULT_API_HOST = 'https://api.swetrix.com/log'/*** Server-side implementation of Swetrix tracking library.- *+ ** @param projectID Your project ID (you can find it in the project settings)* @param options LibOptions*/@@ -137,7 +168,7 @@ export class Swetrix {/*** This function is used to send custom events (implements https://docs.swetrix.com/events-api#post-loghb).- *+ ** @param ip IP address of the visitor* @param userAgent User agent of the visitor* @param event TrackEventOptions@@ -157,7 +188,7 @@ export class Swetrix {/*** This function is used to send pageview events (implements https://docs.swetrix.com/events-api#post-loghb).- *+ ** @param ip IP address of the visitor* @param userAgent User agent of the visitor* @param pageview TrackPageViewOptions@@ -168,18 +199,38 @@ export class Swetrix {return}- const { pg } = pageview || {}+ const data = {+ pid: this.projectID,+ ...(pageview || {}),+ }- const shouldIgnore = this.checkIgnore(pg)+ this.sendRequest('', ip, userAgent, data)+ }- if (shouldIgnore && this.options?.doNotAnonymise) return+ /**+ * This function is used to track an error event.+ * It's useful if you want to track specific errors in your application.+ *+ * @param ip IP address of the visitor+ * @param userAgent User agent of the visitor+ * @param error TrackErrorOptions | Pick<TrackPageViewOptions, 'tz' | 'lc' | 'pg'>+ * @returns void+ */+ public trackError(+ ip: string,+ userAgent: string,+ error?: TrackErrorOptions | Pick<TrackPageViewOptions, 'tz' | 'lc' | 'pg'>,+ ) {+ if (!this.canTrack()) {+ return+ }const data = {pid: this.projectID,- ...(pageview || {}),+ ...(error || {}),}- this.sendRequest('', ip, userAgent, data)+ this.sendRequest('error', ip, userAgent, data)}/**@@ -188,7 +239,7 @@ export class Swetrix {* This allows you to see the 'Live Visitors' counter in the Dashboard panel.* It's recommended to send heartbeat events every 30 seconds.* We also extend the session lifetime after receiving a pageview or custom event.- *+ ** @param ip IP address of the visitor* @param userAgent User agent of the visitor* @returns void@@ -204,28 +255,15 @@ export class Swetrix {this.sendRequest('hb', ip, userAgent, data)}- private checkIgnore(path?: string): boolean {- const ignore = this.options?.ignore-- if (Array.isArray(ignore)) {- for (let i = 0; i < ignore.length; ++i) {- if (ignore[i] === path) return true- // @ts-ignore- if (ignore[i] instanceof RegExp && ignore[i].test(path)) return true- }- }- return false- }-private debug(message: string, force?: boolean): void {- if (this.options?.debug || force) {+ if (this.options?.devMode || force) {console.log('[Swetrix]', message)}}private canTrack(): boolean {if (this.options?.disabled) {- this.debug('Tracking disabled: the \'disabled\' setting is set to true.')+ this.debug("Tracking disabled: the 'disabled' setting is set to true.")return false}