Skip to content

Commit

Permalink
Fix eventprops trait (#579)
Browse files Browse the repository at this point in the history
* fix event-properties and traits type

* undefined properties

Co-authored-by: Seth Silesky <silesky@users.noreply.github.com>
  • Loading branch information
silesky and silesky authored Aug 11, 2022
1 parent 8d48bdc commit 98504e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-beds-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fix SegmentEvent and EventProperties and add tests
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,40 @@ export default {
assertIs<Promise<User>>(grpResult)
}
},
'Identify should work with spread objects ': () => {
const user = {
name: 'john',
id: 12345,
}
const { id, ...traits } = user
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', traits)
},
'Track should work with spread objects': () => {
const user = {
name: 'john',
id: 12345,
}
const { id, ...traits } = user
void AnalyticsBrowser.load({ writeKey: 'foo' }).track('foo', traits)
},
'Identify should work with generic objects ': () => {
const user = {
name: 'john',
id: 12345,
}
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', user)
},
'Context should have a key allowing arbitrary properties': async () => {
const [_, ctx] = await AnalyticsBrowser.load({ writeKey: 'foo' })
const properties = ctx.event.properties!

properties.category.baz = 'hello'
},
'Track should allow undefined properties': () => {
type User = {
name?: string
thing: 123
}
void AnalyticsBrowser.load({ writeKey: 'foo' }).track('foo', {} as User)
},
}
39 changes: 18 additions & 21 deletions packages/browser/src/core/events/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,10 @@ interface AnalyticsContext {
[key: string]: any
}

// "object" is not ideal, but with JSONObject, we ran into at least one bad edge case around index sig.
// This is not ideal, but it works with all the edge cases
export type Traits = Record<string, any>

/**
* An object literal representing traits
* - identify: https://segment.com/docs/connections/spec/identify/#traits
* - group: https://segment.com/docs/connections/spec/group/#traits
* @example
* { name: "john", age: 25 }
*/
export type Traits = object & JSONObject // intersection adds an index signature

/**
* An object literal representing Segment event properties
* - track: https://segment.com/docs/connections/spec/track/#properties
* - page: https://segment.com/docs/connections/spec/page/#properties
* - screen: https://segment.com/docs/connections/spec/screen/#properties
* @example
* { artistID: 2435325, songID: 13532532 }
*/
export type EventProperties = object & JSONObject // intersection adds an index signature
export type EventProperties = Record<string, any>

export interface SegmentEvent {
messageId?: string
Expand All @@ -129,9 +113,22 @@ export interface SegmentEvent {
// page specific
category?: string
name?: string

/**
* An object literal representing Segment event properties
* - track: https://segment.com/docs/connections/spec/track/#properties
* - page: https://segment.com/docs/connections/spec/page/#properties
* - screen: https://segment.com/docs/connections/spec/screen/#properties
* @example
* { artistID: 2435325, songID: 13532532 }
*/
properties?: EventProperties

/**
* An object literal representing traits
* - identify: https://segment.com/docs/connections/spec/identify/#traits
* - group: https://segment.com/docs/connections/spec/group/#traits
* @example
* { name: "john", age: 25 }
*/
traits?: Traits

integrations?: Integrations
Expand Down

0 comments on commit 98504e2

Please sign in to comment.