Skip to content

Instantly share code, notes, and snippets.

@nithinkashyapn
Forked from joshinat0r/index.js
Created July 11, 2021 18:03
Show Gist options
  • Select an option

  • Save nithinkashyapn/5b6ee1acafa33b555e2150a2a3ddc8bf to your computer and use it in GitHub Desktop.

Select an option

Save nithinkashyapn/5b6ee1acafa33b555e2150a2a3ddc8bf to your computer and use it in GitHub Desktop.
Umami API wrapper
const umami = require('./umami.js');
const api = await umami({
url: 'https://app.umami.is',
username: 'username',
password: 'password',
website: 1, // internal website-id
})
const stats = await api.getStats()
const views = await api.getUrls()
const chartData = await api.last90Days().resolutionMonth().getChartPageviews() // pageviews for the last 90 days grouped by month
const fetch = require('node-fetch')
function parseCookies(response) {
const raw = response.headers.raw()['set-cookie'];
return raw.map((entry) => {
const parts = entry.split(';');
const cookiePart = parts[0];
return cookiePart;
}).join(';');
}
class Umami {
constructor(config) {
this.username = config.username
this.password = config.password
this.website = config.website
this.url = config.url
this.resolution = 'day'
this.cookies = ''
const today = new Date()
this.endAt = today.getTime()
this.startAt = today.setDate(today.getDate() - 30)
}
async auth() {
const req = await fetch(`${this.url}/api/auth/login`, {
'headers': {
'accept': 'application/json',
'content-type': 'application/json; charset=UTF-8',
'cookie': '',
},
'body': JSON.stringify({
username: this.username,
password: this.password
}),
'method': 'POST',
});
this.cookies = parseCookies(req)
return this
}
async request(url) {
const divider = url.includes('?') ? '&' : '?'
const stats = await fetch(`${url}${divider}start_at=${this.startAt}&end_at=${this.endAt}`, {
'headers': {
'accept': '*/*',
'cookie': this.cookies,
},
'method': 'GET',
})
.then(res => res.json())
.catch(err => console.log(err))
return stats
}
setTimerange(days) {
const today = new Date()
this.endAt = today.getTime()
this.startAt = today.setDate(today.getDate() - days)
return this
}
resolutionDay() { // umami is using this for the charts, groups the dataset
this.resolution = 'day'
return this
}
resolutionMonth() { // umami is using this for the charts, groups the dataset
this.resolution = 'month'
return this
}
last90Days() {
this.setTimerange(90)
return this
}
last30Days() {
this.setTimerange(30)
return this
}
last7Days() {
this.setTimerange(7)
return this
}
customRange(startAt, endAt) {
this.startAt = startAt
this.endAt = endAt
return this
}
setWebsite(website) {
this.website = website
return this
}
async getStats() {
return await this.request(`${this.url}/api/website/${this.website}/stats`)
}
async getChartPageviews() {
return await this.request(`${this.url}/api/website/${this.website}/pageviews?unit=${this.resolution}&tz=Etc%2FUTC`)
}
async getChartEvents() {
return await this.request(`${this.url}/api/website/${this.website}/events?unit=${this.resolution}&tz=Etc%2FUTC`)
}
async getEvents() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=event&tz=Etc%2FUTC`)
}
async getUrls() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=url&tz=Etc%2FUTC`)
}
async getReferrers() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=referrer&tz=Etc%2FUTC`)
}
async getBrowsers() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=browser&tz=Etc%2FUTC`)
}
async getOses() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=os&tz=Etc%2FUTC`)
}
async getDevices() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=device&tz=Etc%2FUTC`)
}
async getCountries() {
return await this.request(`${this.url}/api/website/${this.website}/metrics?type=country&tz=Etc%2FUTC`)
}
}
async function umamiClient(config) {
const instance = new Umami(config);
await instance.auth();
return instance;
}
module.exports = umamiClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment