You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2022. It is now read-only.
A Segment analytics library using the HTTP api directly. The reason for this library is to delegate the responsibility of distributing data across integrations to Segment itself after having countless issues with data inconsistencies, because the official Segment way is to let the browser itself take care of all the integrations. That of course creates problems with ad-blockers.
3
+
This is an unofficial alternative to [analytics.js](https://segment.com/docs/sources/website/analytics.js/) by [segment.io](https://segment.io).
4
+
Unlike original implementation this library speaks with Segment's API directly and delegates responsibility of data distribution to back-end (as official Ruby, Java, Clojure, Python and many others do).
5
+
This helps you to prevent many issues with data inconsistency between various back-ends and optimize number of HTTP payloads required for tracks.
6
+
This library also comes with few other improvements over official one namely it uses **batch api** to prevent issues with [rate limits](https://segment.com/docs/sources/server/http/#rate-limits),
7
+
prevents tracks without `userId` by using internal queue, has **Promise** API to make easy to reason about async actions and **comes with test mocks** you can use in your test suit.
8
+
9
+
## What if I actually want to track event before calling identify?
10
+
11
+
No problem with that. Just use `anonymousTrack` or `anonymousPage`.
12
+
13
+
## Demo
14
+
15
+

4
16
5
17
## Usage
6
18
7
-
### Installation
19
+
At first install this using npm:
8
20
9
21
```
10
22
$ npm install gwi-segment --save
11
23
```
12
24
13
-
### Client initialization
25
+
### The Most Simple Use-Case
26
+
27
+
In most cases you want to just initialize library and store instance to global singleton object like this:
No `#track` nor `#page` call is proceed before `#identify` is called. This is to prevent events with missing `userId` to go through the system.
162
+
All events happened before `#identify` are added to queue and are waiting for userId. Once `#identify` is called `userId` from this call is used
163
+
for all waiting events which are waiting. Events are then tracked in order they were added to queue.
164
+
165
+
## How Batching Works
166
+
167
+
By default there is `100`ms timeout for collecting all tracks into single [batch](https://segment.com/docs/sources/server/http/#batch) request. This means events are not sent immediately
168
+
but are collected into one single request. This helps you to overcome issue with [rate limits](https://segment.com/docs/sources/server/http/#rate-limits) and optimize requests from app or website.
169
+
You can change default timeout using `options.timeout` or disable it completely by passing `-1` as timeout value.
170
+
171
+
## Test Mocking
172
+
173
+
Similarly to main client you can initialize test mock instead. This is done using `getTestMockClient()` static function and returns instance implementing default API.
174
+
However this client doesn't really speaks to API but instead pushes events into internal stack. Also this client doesn't perform any merging to batch API
175
+
but rather keeps all events isolated to make it easier to use in test suit. Public api still uses promises as production one but in fact works synchronously to make your tests simpler and faster.
176
+
It also contains extra `inspect` name-space for checking state of tracks.
177
+
178
+
### #inspect.allEvents
179
+
180
+
Returns array of all events tracked (including identify and page).
181
+
182
+
*no arguments*
183
+
184
+
### #inspect.lastEvent
185
+
186
+
Returns last event tracked (including identify and page).
0 commit comments