Skip to content

Commit 03e6383

Browse files
authored
node: add context, timestamp, integration options (#694)
1 parent 8dfad6e commit 03e6383

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

packages/node/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,29 @@ export const lowercase: Plugin = {
277277
analytics.register(lowercase)
278278

279279
```
280+
281+
## Selecting Destinations
282+
The alias, group, identify, page and track calls can all be passed an object of integrations that lets you turn certain destinations on or off. By default all destinations are enabled.
283+
284+
Here’s an example with the integrations object shown:
285+
```ts
286+
analytics.track({
287+
event: 'Membership Upgraded',
288+
userId: '97234974',
289+
integrations: {
290+
'All': false,
291+
'Vero': true,
292+
'Google Analytics': false
293+
}
294+
})
295+
```
296+
297+
In this case, we’re specifying that we want this track to only go to Vero. All: false says that no destination should be enabled unless otherwise specified. Vero: true turns on Vero, etc.
298+
299+
Destination flags are case sensitive and match the [destination’s name in the docs](https://segment.com/docs/connections/destinations) (i.e. “AdLearn Open Platform”, “awe.sm”, “MailChimp”, etc.). In some cases, there may be several names for a destination; if that happens you’ll see a “Adding (destination name) to the Integrations Object” section in the destination’s doc page with a list of valid names.
300+
301+
Note:
302+
303+
- Available at the business level, filtering track calls can be done right from the Segment UI on your source schema page. We recommend using the UI if possible since it’s a much simpler way of managing your filters and can be updated with no code changes on your side.
304+
305+
- If you are on a grandfathered plan, events sent server-side that are filtered through the Segment dashboard will still count towards your API usage.

packages/node/src/app/analytics-node.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
CorePlugin,
77
EventFactory,
88
EventQueue,
9-
CoreOptions,
109
CoreSegmentEvent,
1110
bindAll,
1211
PriorityQueue,
1312
pTimeout,
13+
Integrations,
1414
} from '@segment/analytics-core'
1515
import { AnalyticsSettings, validateSettings } from './settings'
1616
import { version } from '../../package.json'
@@ -23,6 +23,7 @@ import { NodeEmitter } from './emitter'
2323
export class Context extends CoreContext {}
2424

2525
export interface Plugin extends CorePlugin {}
26+
type Timestamp = string | Date
2627

2728
/**
2829
* An ID associated with the user. Note: at least one of userId or anonymousId must be included.
@@ -31,11 +32,11 @@ type IdentityOptions =
3132
| { userId: string; anonymousId?: string }
3233
| { userId?: string; anonymousId: string }
3334

34-
/** Events from CoreOptions */
35-
export interface SegmentEventOptions {
36-
context?: Context
37-
timestamp?: CoreOptions['timestamp']
38-
}
35+
/**
36+
* A dictionary of extra context to attach to the call.
37+
* Note: context differs from traits because it is not attributes of the user itself.
38+
*/
39+
type AdditionalContext = Record<string, any>
3940

4041
class NodePriorityQueue extends PriorityQueue<Context> {
4142
constructor() {
@@ -55,7 +56,6 @@ type SegmentEventType = 'track' | 'page' | 'identify' | 'alias' | 'screen'
5556

5657
export interface SegmentEvent extends CoreSegmentEvent {
5758
type: SegmentEventType
58-
options?: SegmentEventOptions
5959
}
6060

6161
export class Analytics extends NodeEmitter implements CoreAnalytics {
@@ -148,17 +148,25 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
148148
{
149149
userId,
150150
previousId,
151-
options,
151+
context,
152+
timestamp,
153+
integrations,
152154
}: {
153155
/* The new user id you want to associate with the user. */
154156
userId: string
155157
/* The previous id that the user was recognized by (this can be either a userId or an anonymousId). */
156158
previousId: string
157-
options?: SegmentEventOptions
159+
context?: AdditionalContext
160+
timestamp?: Timestamp
161+
integrations?: Integrations
158162
},
159163
callback?: Callback
160164
): void {
161-
const segmentEvent = this._eventFactory.alias(userId, previousId, options)
165+
const segmentEvent = this._eventFactory.alias(userId, previousId, {
166+
context,
167+
integrations,
168+
timestamp,
169+
})
162170
this._dispatch(segmentEvent, callback)
163171
}
164172

@@ -168,22 +176,28 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
168176
*/
169177
group(
170178
{
179+
timestamp,
171180
groupId,
172181
userId,
173182
anonymousId,
174183
traits = {},
175-
options = {},
184+
context,
185+
integrations,
176186
}: IdentityOptions & {
177187
groupId: string
178188
traits?: Traits
179-
options?: SegmentEventOptions
189+
context?: AdditionalContext
190+
timestamp?: Timestamp
191+
integrations?: Integrations
180192
},
181193
callback?: Callback
182194
): void {
183195
const segmentEvent = this._eventFactory.group(groupId, traits, {
184-
...options,
196+
context,
185197
anonymousId,
186198
userId,
199+
timestamp,
200+
integrations,
187201
})
188202

189203
this._dispatch(segmentEvent, callback)
@@ -198,17 +212,20 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
198212
userId,
199213
anonymousId,
200214
traits = {},
201-
options,
215+
context,
216+
integrations,
202217
}: IdentityOptions & {
203218
traits?: Traits
204-
options?: SegmentEventOptions
219+
context?: AdditionalContext
220+
integrations?: Integrations
205221
},
206222
callback?: Callback
207223
): void {
208224
const segmentEvent = this._eventFactory.identify(userId, traits, {
209-
...options,
225+
context,
210226
anonymousId,
211227
userId,
228+
integrations,
212229
})
213230
this._dispatch(segmentEvent, callback)
214231
}
@@ -224,25 +241,27 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
224241
category,
225242
name,
226243
properties,
227-
options,
244+
context,
228245
timestamp,
246+
integrations,
229247
}: IdentityOptions & {
230248
/* The category of the page. Useful for cases like ecommerce where many pages might live under a single category. */
231249
category?: string
232250
/* The name of the page.*/
233251
name?: string
234252
/* A dictionary of properties of the page. */
235253
properties?: EventProperties
236-
timestamp?: string | Date
237-
options?: SegmentEventOptions
254+
timestamp?: Timestamp
255+
context?: AdditionalContext
256+
integrations?: Integrations
238257
},
239258
callback?: Callback
240259
): void {
241260
const segmentEvent = this._eventFactory.page(
242261
category ?? null,
243262
name ?? null,
244263
properties,
245-
{ ...options, anonymousId, userId, timestamp }
264+
{ context, anonymousId, userId, timestamp, integrations }
246265
)
247266
this._dispatch(segmentEvent, callback)
248267
}
@@ -260,16 +279,17 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
260279
category,
261280
name,
262281
properties,
263-
options,
282+
context,
264283
timestamp,
284+
integrations,
265285
}: Parameters<Analytics['page']>[0],
266286
callback?: Callback
267287
): void {
268288
const segmentEvent = this._eventFactory.screen(
269289
category ?? null,
270290
name ?? null,
271291
properties,
272-
{ ...options, anonymousId, userId, timestamp }
292+
{ context, anonymousId, userId, timestamp, integrations }
273293
)
274294

275295
this._dispatch(segmentEvent, callback)
@@ -285,18 +305,24 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
285305
anonymousId,
286306
event,
287307
properties,
288-
options,
308+
context,
309+
timestamp,
310+
integrations,
289311
}: IdentityOptions & {
290312
event: string
291313
properties?: EventProperties
292-
options?: SegmentEventOptions
314+
context?: AdditionalContext
315+
timestamp?: Timestamp
316+
integrations?: Integrations
293317
},
294318
callback?: Callback
295319
): void {
296320
const segmentEvent = this._eventFactory.track(event, properties, {
297-
...options,
321+
context,
298322
userId,
299323
anonymousId,
324+
timestamp,
325+
integrations,
300326
})
301327

302328
this._dispatch(segmentEvent, callback)

0 commit comments

Comments
 (0)