Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Updated .track method functionality.

Mike Cao71fcad26a503
diff --git a/package.json b/package.json
index 914c8acf6..d2e30ec0b 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "umami",
- "version": "2.0.2",
+ "version": "2.1.0",
"description": "A simple, fast, privacy-focused alternative to Google Analytics.",
"author": "Mike Cao <[email protected]>",
"license": "MIT",
diff --git a/tracker/index.js b/tracker/index.js
index 63ed6fdd9..dfdb08b1b 100644
--- a/tracker/index.js
+++ b/tracker/index.js
@@ -169,7 +169,7 @@
const send = payload => {
if (trackingDisabled()) return;
const headers = {
- 'Content-Type': 'application/json'
+ 'Content-Type': 'application/json',
};
if (typeof cache !== 'undefined') {
headers['x-umami-cache'] = cache;
@@ -177,17 +177,23 @@
return fetch(endpoint, {
method: 'POST',
body: JSON.stringify({ type: 'event', payload }),
- headers: headers
+ headers,
})
.then(res => res.text())
.then(text => (cache = text));
};
- const track = (name = {}, data = {}) => {
- if (typeof name === 'string') {
- return send({ ...getPayload(), ...data, name });
- } else if (typeof name === 'object') {
- return send({ ...getPayload(), ...name });
+ const track = (obj, data) => {
+ if (typeof obj === 'string') {
+ return send({
+ ...getPayload(),
+ name: obj,
+ data: typeof data === 'object' ? data : undefined,
+ });
+ } else if (typeof obj === 'object') {
+ return send(obj);
+ } else if (typeof obj === 'function') {
+ return send(obj(getPayload()));
}
return Promise.reject();
};