Send events to Segment from your Node JS backend. This package is built on top of the offical analytics-node package. It gives you TypeScript type definitions for all exported functions and will validate the incoming data so it complies with the rules of the Segment API.
yarn add segment-service-js
Basic usage involves adding the AnalyticsService
to your setup. IMPORTANT You need to fetch your Write Key from your Segment destination and feed it to AnalyticsService
(as seen below).
import { AnalyticsService } from 'segment-service-js'
const segmentWriteKey = 'your_segment_write_key'
// Initialize the AnalyticsService
const analytics = new AnalyticsService({
writeKey: segmentWriteKey,
})
Example
analytics.identify({
userId: userId,
traits: {
id: userId,
username: user.name,
email: user.email,
createdAt: new Date(user.createdAt),
},
})
Accepts
- userId (
string
) - traits (
object
) - timestamp (
string
) - context (
object
) - anonymousId (
string
)
Mandatory
userId
ORanonymousId
Example
analytics.track({
userId: userId,
event: 'user_logged_in',
properties: {
name: user.name,
email: user.email,
},
})
Accepts
- event (
string
) - userId (
string
) - timestamp (
string
) - properties (
object
) - context (
object
) - anonymousId (
string
)
Mandatory
userId
ORanonymousId
event
Example
analytics.group({
userId: `${userId}`,
groupId: `${companyId}`,
traits: {
name: company.name,
},
})
Accepts
- userId (
string
) - groupId (
string
) - traits (
object
) - timestamp (
string
) - context (
object
) - anonymousId (
string
)
Mandatory
userId
ORanonymousId
groupId