📈 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
Implemented some tracking metrics methods
Blaumausc04d995775d1
diff --git a/src/Lib.ts b/src/Lib.tsindex 2cc7450ee..e26205f32 100644--- a/src/Lib.ts+++ b/src/Lib.ts@@ -1,3 +1,22 @@+import {+ isInBrowser,+ isLocalhost,+ isAutomated,+ getLocale,+ getTimezone,+ getRefferer,+ getUTMCampaign,+ getUTMMedium,+ getUTMSource,+} from './utils'++/**+ * A map of key / value pairs.+ */+interface Map<T> {+ [key: string]: T+}+export interface LibOptions {// When set to `true`, all libraries logs (including possible errors) will be// printed to console and localhost events will be sent to server.@@ -13,13 +32,19 @@ export interface LibOptions {}export interface TrackEventOptions {+ // Unique ID for the event+ id: string+ // Parameters for the event, e.g. { ref: 'duckduckgo', plan: 'gold+' }+ params?: Map<string>}export interface PageViewsOptions {}+const host = 'http://localhost:5000/log'+const defaultOptions: LibOptions = {debug: false,disabled: false,@@ -27,19 +52,82 @@ const defaultOptions: LibOptions = {}export class Lib {- constructor(private pid: string, private options?: LibOptions) {+ constructor(private projectID: string, private options?: LibOptions) {}// Tracks a custom eventtrack(event: TrackEventOptions) {+ if (!this.canTrack()) {+ return+ }+ const data = {+ pid: this.projectID,+ ev: event.id,+ params: event.params,+ }+ this.submitData(data)}// Tracks page viewstrackPageViews(options?: PageViewsOptions) {+ if (!this.canTrack()) {+ return+ }++ }++ private trackPage() {+ const data = {+ lc: getLocale(),+ tz: getTimezone(),+ ref: getRefferer(),+ so: getUTMSource(),+ me: getUTMMedium(),+ ca: getUTMCampaign(),+ }}+ private debug(message: string) {+ if (this.options?.debug) {+ console.log('[Analytics] ', message)+ }+ }+ private canTrack() {+ if (!isInBrowser()) {+ this.debug('Tracking disabled: script does not run in browser environment.')+ return false+ }++ if (this.options?.respectDNT && window.navigator?.doNotTrack === '1') {+ this.debug('Tracking disabled: respecting user\'s \'Do Not Track\' preference.')+ return false+ }++ if (!this.options?.debug && isLocalhost()) {+ this.debug('Tracking disabled: running on localhost.')+ return false+ }++ if (isAutomated()) {+ this.debug('Tracking disabled: navigation is automated by WebDriver.')+ return false+ }++ return true+ }++ private submitData(body: object) {+ return fetch(host, {+ method: 'post',+ headers: {+ 'Accept': 'application/json',+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify(body),+ })+ }}\ No newline at end of filediff --git a/src/utils.ts b/src/utils.tsnew file mode 100644index 000000000..ef3c83e35--- /dev/null+++ b/src/utils.ts@@ -0,0 +1,44 @@+const ua = navigator.userAgent || ''++const findInSearch = (exp: RegExp): string | null => {+ const res = location.search.match(exp)+ return res && res[2]+}++const utmSourceRegex = /[?&](ref|source|utm_source)=([^?&]+)/+const utmCampaignRegex = /[?&](utm_campaign)=([^?&]+)/+const utmMediumRegex = /[?&](utm_medium)=([^?&]+)/++export const isInBrowser = () => {+ return typeof window !== 'undefined'+}++export const isLocalhost = () => {+ return location?.hostname === 'localhost' || location?.hostname === '127.0.0.1' || location?.hostname === ''+}++export const isAutomated = () => {+ return navigator?.webdriver+}++export const getLocale = () => {+ return typeof navigator.languages !== 'undefined' ? navigator.languages[0] : navigator.language+}++export const getTimezone = () => {+ try {+ return Intl.DateTimeFormat().resolvedOptions().timeZone+ } catch (e) {+ return+ }+}++export const getRefferer = () => {+ return document.referrer+}++export const getUTMSource = () => findInSearch(utmSourceRegex)++export const getUTMMedium = () => findInSearch(utmMediumRegex)++export const getUTMCampaign = () => findInSearch(utmCampaignRegex)