Skip to content

Commit

Permalink
add additional fields like context and timestamp to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 23, 2022
1 parent 1ff800a commit 3e5d822
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/node/src/app/analytics-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ import { DispatchAndEmitCallback, dispatchAndEmit } from './dispatch-emit'
// create a derived class since we may want to add node specific things to Context later
export class Context extends CoreContext {}

type Timestamp = string | Date

/**
* An ID associated with the user. Note: at least one of userId or anonymousId must be included.
**/
type IdentityOptions =
| { userId: string; anonymousId?: string }
| { userId?: string; anonymousId: string }

/** Events from CoreOptions */
interface SegmentEventExtraContext extends Record<string, any> {}
/**
* A dictionary of extra context to attach to the call.
* Note: context differs from traits because it is not attributes of the user itself.
*/
type AdditionalContext = Record<string, any>

/**
* Map of emitter event names to method args.
Expand Down Expand Up @@ -158,17 +163,20 @@ export class Analytics
userId,
previousId,
context,
timestamp,
}: {
/* The new user id you want to associate with the user. */
userId: string
/* The previous id that the user was recognized by (this can be either a userId or an anonymousId). */
previousId: string
context?: SegmentEventExtraContext
context?: AdditionalContext
timestamp?: Timestamp
},
callback?: DispatchAndEmitCallback
): void {
const segmentEvent = this._eventFactory.alias(userId, previousId, {
context,
timestamp,
})
this._dispatch(segmentEvent, callback)
}
Expand All @@ -186,10 +194,10 @@ export class Analytics
traits = {},
context,
}: IdentityOptions & {
timestamp: string
groupId: string
traits?: Traits
context?: SegmentEventExtraContext
context?: AdditionalContext
timestamp?: Timestamp
},
callback?: DispatchAndEmitCallback
): void {
Expand All @@ -215,7 +223,7 @@ export class Analytics
context,
}: IdentityOptions & {
traits?: Traits
context?: SegmentEventExtraContext
context?: AdditionalContext
},
callback?: DispatchAndEmitCallback
): void {
Expand Down Expand Up @@ -247,8 +255,8 @@ export class Analytics
name?: string
/* A dictionary of properties of the page. */
properties?: EventProperties
timestamp?: string | Date
context?: SegmentEventExtraContext
timestamp?: Timestamp
context?: AdditionalContext
},
callback?: DispatchAndEmitCallback
): void {
Expand Down Expand Up @@ -300,17 +308,20 @@ export class Analytics
event,
properties,
context,
timestamp,
}: IdentityOptions & {
event: string
properties?: EventProperties
context?: SegmentEventExtraContext
context?: AdditionalContext
timestamp?: Timestamp
},
callback?: DispatchAndEmitCallback
): void {
const segmentEvent = this._eventFactory.track(event, properties, {
context,
userId,
anonymousId,
timestamp,
})

this._dispatch(segmentEvent, callback)
Expand Down

0 comments on commit 3e5d822

Please sign in to comment.