umami-software / node

Umami Node.js library
11 stars 4 forks source link

Adding event name in tracking payload overrides all other properties #4

Open Sov3rain opened 1 month ago

Sov3rain commented 1 month ago

Hi there!

Using this package with Fastify server in Node@20. I'm building all the parameters for tracking my user like this:

const umami = new Umami();

umami.init({
  hostUrl: process.env.UMAMI_HOST,
  websiteId: ...,
  userAgent: req.userAgent.source,
});

const url = new URL(req.url, `${req.protocol}://${req.hostname}`);
const { hostname, pathname } = url;
const language = req.headers["accept-language"];
const { referrer } = req.headers;

umami.track({
  hostname,
  screen: "1920x1080",
  language,
  title: `/Home`,
  url: pathname,
  referrer,
});

This is working just fine, I'm getting all the information in my dashboard. Unfortunately when I add a custom event name, suddenly all the other information are missing, and I get only the event tracking:

umami.track({
  hostname,
  screen: "1920x1080",
  language,
  title: `/Home`,
  url: pathname,
  referrer,
+ name: "My event"
});

I worked around this issue by calling track() twice: once with the generic tracking information and once with my custom event only:

umami.track({
  hostname,
  screen: "1920x1080",
  language,
  title: `/Home`,
  url: pathname,
  referrer,
  name: "Button clicked"
});
umami.track("My event");

I should not be forced to do that, right? Thanks!

NOTE: I'm importing the Umami class instead of the default import because it doesn't work, I can open another issue if you wish.

boly38 commented 5 days ago

I encounter some trouble using node client + cloud umami website entry too


I noticed that to retrieve event custom data, the track event must include "data" attribute like:

const data = {"color": "red"};
event = {url, title, "name": "button-click", data};
await umamiClient.track(event);

(see also gist)


suggestion: I would expect a set of tests that ensure node client library quality: text context) given a special umami website id (could be provided as action secret), some test could be done for: identify, track, trackEvent with verify of expected result from umami management api. this to ensure industrialized product and follow-up of node client compatibility with umami standalone server and/or cloud evolutions. 1) build client 2) send page hit w/ userAgent using node client : verify it using umami management api 3) send event hit w/ userAgent & w/ custom data using node client : verify it using umami management api 4) repeat 2) and 3) adding identify call as pre-condition

(lets do 1..4 for umami hosted version + 1..4 for umami cloud version)

Sov3rain commented 3 days ago

After further investigation (I needed to write a Umami client compatible with Unity 3D, so in C#), I found that the function track() is very misnamed. In fact, internally this function is responsible for 2 different things:

This is very un-intuitive and this function should be split into two functions instead:

The documentation should be updated with this informations and some clarification should be made on whether or not a page view tracking should precede a event tracking.