All files / lib/services umamiService.js

96.92% Statements 63/65
33.33% Branches 4/12
100% Functions 1/1
96.92% Lines 63/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 651x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 7x 6x 6x 6x 7x 7x 7x 7x     7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x
import UmamiClient from 'umami-api-client'
import {isSet, isVerbose, rethrow} from "./util.js";
 
const verbose = isVerbose;
 
export default class UmamiService {
 
    static async fetchUmamiData({
                                    cloudApiKey, server, username, password, domain,
                                    period, unit, timezone,
                                    reportContent
                                }) {
        //~ Get datas from umami
        const umamiClient = new UmamiClient({
            cloudApiKey, server
        });
        if (umamiClient.isCloudMode()) {
            await umamiClient.me();
        } else {
            await umamiClient.login(username, password)
                .catch(err => rethrow(err, `Unable to login`));
        }
        const sites = await umamiClient.websites()
            .catch(err => rethrow(err, `Unable to get websites`));
        const site = umamiClient.selectSiteByDomain(sites, domain);
        if (!isSet(site)) {
            throw new Error(`unable to select site having domain:${domain}`);
        }
        verbose && console.log(`site: ${JSON.stringify(site, null, 2)}`);
 
        const siteStats = await umamiClient.websiteStats(site.id, period)
            .catch(err => rethrow(err, `Unable to get websiteStats`));
        verbose && console.log(`siteStats: ${JSON.stringify(siteStats, null, 2)}`);
 
        const reportContents = reportContent?.length > 0 ? reportContent.split('|') : [];
        let sitePageViews, siteEvents, siteSessions, siteMetricsUrl;
 
        if (reportContents.includes('pageviews')) {
            sitePageViews = await umamiClient.websitePageViews(site.id, period, {unit, timezone})
                .catch(err => rethrow(err, `Unable to get websitePageViews`));
            verbose && console.log(`sitePageViews: ${JSON.stringify(sitePageViews, null, 2)}`);
        }
 
        let type = "event";
        if (reportContents.includes('events')) {
            siteEvents = await umamiClient.websiteMetrics(site.id, period, {unit, timezone, type})
                .catch(err => rethrow(err, `Unable to get websiteMetrics type ${type}`));
            verbose && console.log(`siteEvents: ${JSON.stringify(siteEvents, null, 2)}`);
        }
        if (reportContents.includes('sessions')) {
            siteSessions = await umamiClient.websiteSessions(site.id, period, {pageSize: 5, orderBy: "-views"})
                .catch(err => rethrow(err, `Unable to get websiteSessions`));
            verbose && console.log(`siteSessions: ${JSON.stringify(siteSessions, null, 2)}`);
        }
        if (reportContents.includes('urls')) {
            type = "url";
            siteMetricsUrl = await umamiClient.websiteMetrics(site.id, period, {unit, timezone, type})
                .catch(err => rethrow(err, `Unable to getMetrics type ${type}`));
            verbose && console.log(`siteMetricsUrl: ${JSON.stringify(siteMetricsUrl, null, 2)}`);
        }
 
        return {site, siteStats, sitePageViews, siteEvents, siteSessions, siteMetricsUrl};
    }
 
}