THE MAINTENANCE IS MOVED TO OFFICIAL REPO:
https://github.com/sensorsdata/sa-sdk-node
Sensors Analytics
This is the home-brewed version of Node SDK for Sensors Analytics.
Install
Install using npm.
$ npm install sa-sdk-node --save
Attention
In the latest version(v1.0.8), we added a option named allowReNameOption
valued true
by default.
In this case, we change the property value and the keys to snake style with '_'.
eg:
// when 'allowReNameOption' is truesa // then we get the data...'event': 'user_happy''properties': '$app_version': '1.0.0' 'order_id': '123'...
You can use sa.disableReNameOption()
to set allowReNameOption
to be false
.
In this case, when you set default property, you must use like $app_version
, $appVersion
style could be error. Refer to Data schema for more detail.
Property name like orderId
will be kept.
Usage
Basic Usage
const sa = sa // Super Properties that assigned to every event trackingsa // Track eventsa // Track event with custom propertiessa // Track Signupsa // Track Signup with custom propertiessa // Manipuate user projectsasasasasa
For more detailed information about each api, checkout Sensors Analytics manual
Override event time
By default, the library uses current time as the time when event occurs,
but the behavior can be overrode by $time
property.
- Both
track
andtrackSignup
support this feature. $time
can beDate
,number
,string
,Moment
instance
sasasasa sasasasa
Parse Geolocation
SensorsData support parsing user geo location from IP address.
- Both
track
andtrackSignup
support this feature.
router
Parse User Agent
Node SDK supports parsing client OS
, OS version
, Browser
, Browser version
, Browser Engine
, Model
from client's User Agent
- Both
track
andtrackSignup
support this feature.
router
Config Submitter
By default, submitter can be created with server url
const url = 'http://xxx.cloud.sensorsdata.cn:8006/sa?token=xxx' const sa = const submitter = sa
But it also can be created with explicit config
const url = 'http://xxx.cloud.sensorsdata.cn:8006/sa?token=xxx' const sa = const submitter = sa// gzip: whether enable gzip, default to true// mode: could be 'track' (production use), 'debug' (diagnosis data), 'dryRun' (diagnosis with no data recorded),// also supports the values that aligned to other SDKs: debug_off, debug_and_track and debug_only,// default to track// mode:// timeout: Http timeout in ms, default to 10s
Submitter can be create manually and attach to SensorsAnalytics
manually
Created with url with default config
const url = 'http://xxx.cloud.sensorsdata.cn:8006/sa?token=xxx' const sa = const submitter = url sa
Or with explicit config
const url = 'http://xxx.cloud.sensorsdata.cn:8006/sa?token=xxx' const sa = const submitter = url gzip: true mode: 'track' timeout: 10 * 1000 sa
Network error handling
submitter
Batch Submit
WARN Batch submit is not supported by debug
or dryRun
mode. It causes 400 bad-request error
Suppose
const url = 'http://xxx.cloud.sensorsdata.cn:8006/sa?token=xxx' const sa =
Batch with count
// Submit when 20 events are trackedsa
Batch with time
// Submit when every 5 secondssa
Batch with time or count
// Submit every 5 seconds, but also submit immediately if 20 events trackedsa
Create batch manually
Batch
can be created manually if needed, which can be subscribed with submitter
later
const batch = sa batch
Advanced Usage
This library is powered by Microsoft's RxJS.
SensorsAnalytics
is an Observable, which yields tracking data.
Submitter
is an Observer, which consume the tracking data.
Submitter
is also an Observable, which yields next when submitted succeeded, and yields Error
when network errors.
Ideally, you can use all RxJS tricks with this library
Filtering
// All the event that raised by debug build app won't be submittedsa
Debounce
// Useful while tracking user input or other case// The event won't be tracked unless user has stopped typing for 500mssa textInput
More Detail
For more detail, checkout Microsoft's Rx documentation
License
MIT