Skip to content

Commit

Permalink
Fix #191 - improve node-client example
Browse files Browse the repository at this point in the history
  • Loading branch information
boly38 committed Oct 16, 2024
1 parent c6f1000 commit 68f1e33
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/content/docs/api/node-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,58 @@ The Umami node client allows you to send data to Umami on the server side.
npm install @umami/node
```

This command will install api client [npm package](https://www.npmjs.com/package/@umami/node).

Source of this package is located in [umami-software/node](https://github.com/umami-software/node) GitHub repository.

## Usage

```js
import umami from '@umami/node';

umami.init({
//~ init
let umamiClient = new umami.Umami({
websiteId: '50429a93-8479-4073-be80-d5d29c09c2ec', // Your website id
hostUrl: 'https://umami.mywebsite.com', // URL to your Umami instance
hostUrl: 'https://umami.mywebsite.com' // URL to your Umami instance
// ,userAgent // (optional) agent specifications ( OS / Browser / Device )
});

umami.track({ url: '/home' });
//~ (optional) identify : update with you own session attributes
const sessionId = Date.now();
const identifyOptions = {
"attribute": "11.23",
"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)}`);
```

If using Umami Cloud, you can use `https://cloud.umami.is` as the host URL.
If you're using Umami Cloud, then you can use `https://cloud.umami.is` as `hostUrl`.

The properties you can send using the `.track` function are:
As `.track` function event argument, the properties you can send are:

- **hostname**: Hostname of server
- **language**: Client language (eg. en-US)
- **referrer**: Page referrer
- **screen**: Screen dimensions (eg. 1920x1080)
- **title**: Page title
- **url**: Page url
- **name**: Event name (for custom events)
- **data**: Event data properties

And to track event:
- **name**: Event name
- **data**: Event data custom properties

## See also
- [umami doc - node-client](https://umami.is/docs/api/node-client) (from [umami website](https://github.com/umami-software/website))

0 comments on commit 68f1e33

Please sign in to comment.