Last active
October 16, 2024 18:43
-
-
Save boly38/081318e620b6625467fef75bcb91b618 to your computer and use it in GitHub Desktop.
Umami - Node.Js library sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/umami-software/website/issues/191 | |
// https://umami.is/docs/api/node-client | |
import umami from "@umami/node"; // https://www.npmjs.com/package/@umami/node | |
const hostUrl = "https://cloud.umami.is"; | |
const websiteId = process.env.UMAMI_WEBSITE_ID || false; // umami instance id | |
const websiteDomain = process.env.UMAMI_WEBSITE_DOMAIN || "myapp.example.com"; | |
if (!websiteId) { | |
throw new Error('Please set UMAMI_WEBSITE_ID env'); | |
} | |
//~ init | |
let umamiClient = new umami.Umami({ | |
websiteId,// '50429a93-8479-4073-be80-d5d29c09c2ec', // Your website id : check umami settings to retrieve it | |
hostUrl // umami server or cloud host | |
// ,userAgent // (optional) agent specifications ( OS / Browser / Device ) | |
}); | |
//~ identify : update with you own identification | |
const sessionId = Date.now(); | |
const identifyOptions = { | |
"appName": "myApp", | |
"appVersion": "v1.2.3", | |
"sessionId": sessionId | |
} | |
await umamiClient.identify(identifyOptions); | |
//~ track a page | |
const url = `/home`; | |
const title = "title of /home"; | |
let event = {url, title} | |
umamiClient.track(event); | |
console.log(`✮ Page ${JSON.stringify(event)}`); | |
//~ track an event - an event has a *name* | |
const data = {"color": "red"}; | |
event = {url, title, "name": "button-click", data}; | |
umamiClient.track(event); | |
console.log(`✮ Event ${JSON.stringify(event)}`); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://umami.is/docs/api/node-client | |
import umami from "@umami/node"; // https://www.npmjs.com/package/@umami/node | |
//~ cloud side constants | |
const hostUrl = "https://cloud.umami.is";// UMAMI_CLOUD_HOST_URL | |
//~ client side constants | |
const websiteId = process.env.UMAMI_WEBSITE_ID || false; // umami instance id | |
const UMAMI_DOMAIN = "myapp.local";// umami cloud | |
const APP_NAME = "myApp"; | |
const APP_VERSION = "myVERSION"; | |
const sessionId = Date.now(); | |
async function umamiIdentity() { | |
const identifyOptions = { | |
"appName": APP_NAME, | |
"appVersion": APP_VERSION, | |
sessionId | |
} | |
await umamiClient.identify(identifyOptions); | |
console.log(`Umami identify done using ${JSON.stringify(identifyOptions)}`); | |
} | |
async function trackPageSample(url, title) { | |
const event = {url, title} | |
await umamiClient.track(event);// https://umami.is/docs/tracker-functions | |
console.log(`✮ Page ${JSON.stringify(event)}`); | |
} | |
/** | |
* please notice that "name" switch from page to event | |
*/ | |
async function trackEventSample(url, title, name, data) { | |
const event = {url, title, name, data}; | |
await umamiClient.track(event); | |
console.log(`✮ Event ${JSON.stringify(event)}`); | |
} | |
// ~ main | |
if (!websiteId) { | |
throw new Error('Please set UMAMI_WEBSITE_ID env'); | |
} | |
// ~ user agent | |
// MSDN : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent | |
// \- example : https://deviceatlas.com/blog/list-of-user-agent-strings#consoles | |
const agentPlatform = "Windows NT 10.0; Win64; x64";// umami: Windows 10/11 | |
const agentDevice = "Xbox; Xbox Series X";// umami ignore it | |
const browserAgent = "Chrome/129.0.0.0 Safari/537.36";// umami: Chrome | |
const appAgentVersion = "SeaMonkey/2.7.1";// umami ignore it | |
const userAgent = `Mozilla/5.0 (${agentPlatform}; ${agentDevice}) ${browserAgent} ${appAgentVersion}`; | |
/// question: is there a way to inject custom app and/or device that umami can parse??? | |
//~ init | |
let umamiClient = new umami.Umami({ | |
websiteId,// umami website id : check umami settings to retrieve website id | |
hostUrl, // umami server or cloud host | |
userAgent // (optional) agent specifications ( OS / Browser / Device ) | |
}); | |
console.log(`new Umami using websiteId,[hostUrl:${hostUrl}][userAgent:${userAgent}]`) | |
//~ track | |
let url = `/home`; | |
let urlTitle = "title of /home"; | |
// with no page before | |
await trackEventSample(url, urlTitle, "refresh-event0", {"beforePage": true, "pre-identify": false}); | |
await trackPageSample(url, urlTitle); | |
await trackEventSample(url, urlTitle, "refresh-event1", {"color": "blue2", "pre-identify": false}); | |
await umamiIdentity(); | |
url = `/homeWithIdentify`; | |
urlTitle = "title of /homeWithIdentify"; | |
await trackPageSample(url, urlTitle); | |
await trackEventSample(url, urlTitle, "refresh-event2", {"color": "red2", "pre-identify": true}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment