Skip to content

Commit

Permalink
Add docs and migration guide for JS tracker v4 (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein authored Oct 28, 2024
1 parent 0b47c93 commit 9b52101
Show file tree
Hide file tree
Showing 184 changed files with 12,712 additions and 6,061 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: "Migration guides"
date: "2021-04-12"
sidebar_position: 4800
---

```mdx-code-block
import DocCardList from '@theme/DocCardList';
<DocCardList />
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: "Migrating from v3 to v4"
sidebar_position: 1100
---

```mdx-code-block
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
```

Version 4 is a major release with several breaking changes to defaults and behavior.

## Behavior changes

### Removed the use of the got library in favor of fetch

The tracker no longer needs the got library for making HTTP requests to the collector.
This has been replaced with the [fetch API](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html).
The change has an effect on the public API of the tracker which had to previously accommodate the got library – the following change results from this.

### Removed `tracker` and `emitter` functions in favor or `newTracker`

The initialization of a tracker instance has changed – instead of initializing an emitter (using `gotEmitter`) and tracker (using `tracker`) separately, the SDK handles initialization of both using the `newTracker` call.
The `newTracker` call now accepts configuration objects for the tracker and emitter as arguments.

[See the new initialization options here.](/docs/collecting-data/collecting-from-own-applications/javascript-trackers/node-js-tracker/node-js-tracker-v4/initialization/)

### Dropped support for older Node.JS

The support for the following Node.JS versions has been dropped:

* Drop Node.js <18 support
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "Node.js Tracker v4"
date: "2021-03-24"
sidebar_position: 200
---

```mdx-code-block
import Badges from '@site/src/components/Badges';
<Badges badgeType="Actively Maintained"></Badges>
```

The Snowplow Node.js Tracker allows you to track Snowplow events from your Node.js applications.

The tracker should be straightforward to use if you are comfortable with JavaScript development; any prior experience with other Snowplow trackers is helpful but not necessary.

```mdx-code-block
import DocCardList from '@theme/DocCardList';
<DocCardList />
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: "Initialization"
date: "2021-04-07"
sidebar_position: 2000
---

Assuming you have completed the [Node.js Tracker Setup](/docs/collecting-data/collecting-from-own-applications/javascript-trackers/node-js-tracker/node-js-tracker-v4/setup/index.md) for your project, you are now ready to initialize the Tracker.

Require the Node.js Tracker module into your code like so:

```javascript
const snowplow = require('@snowplow/node-tracker');
const tracker = snowplow.newTracker( /* ... */ );
```

or, if using ES Modules, you can import the module like so:

```javascript
import { newTracker } from '@snowplow/node-tracker';
```

The `newTracker` call takes 2 arguments – the tracker and emitter configuration. The tracker configuration specifies the tracker namespace and app name. The emitter configuration specifies how events are queued locally and sent to the Snowplow collector.

```javascript
const tracker = newTracker({
namespace: "my-tracker", // Namespace to identify the tracker instance. It will be attached to all events which the tracker fires.
appId: "my-app", // Application identifier
encodeBase64: false, // Whether to use base64 encoding for the self-describing JSON. Defaults to true.
}, {
endpoint: "https://collector.mydomain.net", // Collector endpoint
eventMethod: "post", // Method - defaults to POST
bufferSize: 1, // Only send events once n are buffered. Defaults to 1 for GET requests and 10 for POST requests.
});
```

There are a number of additional parameters that may optionally be configured.
Please refer to the following API docs for the full list:

1. [Tracker configuration options](https://snowplow.github.io/snowplow-javascript-tracker/docs/node-tracker/markdown/node-tracker.trackerconfiguration).
2. [Emitter configuration options](https://snowplow.github.io/snowplow-javascript-tracker/docs/node-tracker/markdown/node-tracker.emitterconfigurationbase).

### Using multiple Emitters

In case you want to send the events to multiple Snowplow collectors, you can provide a list of emitter configurations in the `newTracker` call – one for each collector.
This may look as follows.

```javascript
const tracker = newTracker({
namespace: "my-tracker", // Namespace to identify the tracker instance. It will be attached to all events which the tracker fires.
appId: "my-app", // Application identifier
encodeBase64: false, // Whether to use base64 encoding for the self-describing JSON. Defaults to true.
}, [
{
endpoint: "https://collector1.mydomain.net", // First collector endpoint
},
{
endpoint: "https://collector2.mydomain.net", // Second collector endpoint
},
]);
```

### Create your own Emitter

Emitter is an object responsible for queuing tracked events and making requests to the Snowplow collector.

By default, the Node tracker makes use of the [fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to make the HTTP requests.
If you want to use a different library or logic for queuing and sending events, you can provide a custom `Emitter` implementation.

Emitters must conform to an [`Emitter` interface](https://github.com/snowplow/snowplow-javascript-tracker/blob/master/trackers/node-tracker/docs/markdown/node-tracker.emitter.md), which looks like:

```typescript
/**
* Emitter is responsible for sending events to the collector.
* It manages the event queue and sends events in batches depending on configuration.
*/
interface Emitter {
/**
* Forces the emitter to send all events in the event store to the collector.
* @returns A Promise that resolves when all events have been sent to the collector.
*/
flush: () => Promise<void>;
/**
* Adds a payload to the event store or sends it to the collector.
* @param payload - A payload to be sent to the collector
* @returns Promise that resolves when the payload has been added to the event store or sent to the collector
*/
input: (payload: Payload) => Promise<void>;
/**
* Updates the collector URL to which events will be sent.
* @param url - New collector URL
*/
setCollectorUrl: (url: string) => void;
/**
* Sets the server anonymization flag.
*/
setAnonymousTracking: (anonymous: boolean) => void;
/**
* Updates the buffer size of the emitter.
*/
setBufferSize: (bufferSize: number) => void;
}
```

Once you implement your own `Emitter`, you can pass it to the `newTracker` call as follows.

```ts
export const expressTracker = newTracker(
{
namespace: "my-tracker",
appId: "my-app",
},
{
customEmitter: () => ({
input: (payload) => {
return Promise.resolve();
},
flush: () => {
return Promise.resolve();
},
setCollectorUrl: (url) => {},
setAnonymousTracking: (anonymous) => {},
setBufferSize: (bufferSize) => {},
}),
}
);

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "Setup"
date: "2021-03-31"
sidebar_position: 1000
---

## Compatibility

The Snowplow Node.js Tracker is tested and compatible with Node.js versions from 18 to 20. Installation requires [npm](https://www.npmjs.org/), [pnpm](https://pnpm.js.org/) or [yarn](https://yarnpkg.com/).

## Installation

Setting up the tracker should be straightforward if you are familiar with npm:

```bash
npm install @snowplow/node-tracker
```

alternative you can use pnpm or yarn:

```bash
pnpm add @snowplow/node-tracker
```

```bash
yarn add @snowplow/node-tracker
```

The Snowplow Node.js Tracker is also **bundled with TypeScript types and interfaces** so will integrate easily with TypeScript applications.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
type: link
title: API reference
sidebar_position: 5000
href: https://snowplow.github.io/snowplow-javascript-tracker/docs/node-tracker/node-tracker.api
---
Loading

0 comments on commit 9b52101

Please sign in to comment.