Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HEAP-38485] Move to the integrations endpoint #1016

Merged
merged 24 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,42 @@ Object {
exports[`Testing snapshot for actions-heap destination: trackEvent action - all fields 1`] = `
Object {
"app_id": "kFJwrNh$DP38FB",
"event": "track",
"idempotency_key": "kFJwrNh$DP38FB",
"identity": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "cloud-mode-destination",
"testType": "kFJwrNh$DP38FB",
},
"session_id": "kFJwrNh$DP38FB",
"timestamp": "2021-02-01T00:00:00.000Z",
"events": Array [
Object {
"custom_properties": Object {
"testType": "kFJwrNh$DP38FB",
},
"event": "track",
"idempotency_key": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"timestamp": "2021-02-01T00:00:00.000Z",
"user_identifier": Object {
"identity": "kFJwrNh$DP38FB",
},
},
],
"library": "Segment",
}
`;

exports[`Testing snapshot for actions-heap destination: trackEvent action - required fields 1`] = `
Object {
"app_id": "kFJwrNh$DP38FB",
"event": "track",
"idempotency_key": "kFJwrNh$DP38FB",
"identity": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"session_id": "kFJwrNh$DP38FB",
"events": Array [
Object {
"custom_properties": Object {},
"event": "track",
"idempotency_key": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"user_identifier": Object {
"identity": "kFJwrNh$DP38FB",
},
},
],
"library": "Segment",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,70 @@ export const embededObject = () => ({
})

export const flattenObject = () => ({
firstName: 'John',
middleName: '',
lastName: 'Green',
'car.make': 'Honda',
'car.model': 'Civic',
'car.revisions.0.changes': 0,
'car.revisions.0.miles': '10150',
'car.revisions.0.code': 'REV01',
'car.revisions.0.miles': 10150,
'car.revisions.0.firstTime': true,
'car.revisions.1.changes.0.desc': 'Left tire cap',
'car.revisions.1.changes.0.price': 123.45,
'car.revisions.0.changes': '0',
'car.revisions.0.firstTime': 'true',
'car.revisions.1.miles': '20021',
'car.revisions.1.code': 'REV02',
'car.revisions.1.firstTime': 'false',
'car.revisions.1.changes.0.type': 'asthetic',
'car.revisions.1.changes.1.desc': 'Engine pressure regulator',
'car.revisions.1.changes.1.engineer': null,
'car.revisions.1.changes.0.desc': 'Left tire cap',
'car.revisions.1.changes.0.price': '123.45',
'car.revisions.1.changes.1.type': 'mechanic',
'car.revisions.1.firstTime': false,
'car.revisions.1.code': 'REV02',
'car.revisions.1.miles': 20021,
firstName: 'John',
lastName: 'Green',
middleName: '',
'car.revisions.1.changes.1.desc': 'Engine pressure regulator',
'car.revisions.1.changes.1.engineer': 'null',
'visits.0.date': '2015-01-01',
'visits.0.dealer': 'DEAL-001',
'visits.0.useCoupon': true,
'visits.0.useCoupon': 'true',
'visits.1.date': '2015-03-01',
'visits.1.dealer': 'DEAL-002'
})

describe('flattenObj for ', () => {
describe('a flat kvp where the value is a ', () => {
it('undefined', () => {
expect(flat({ myUndefined: undefined } as Properties)).toEqual({})
expect(flat({ myUndefined: undefined } as Properties)).toEqual({ myUndefined: undefined })
})

it('null', () => {
expect(flat({ myNull: null })).toEqual({ myNull: null })
expect(flat({ myNull: null })).toEqual({ myNull: 'null' })
})

it('number', () => {
expect(flat({ myNumber: 1 })).toEqual({ myNumber: 1 })
expect(flat({ myNumber: 1 })).toEqual({ myNumber: '1' })
})

it('string', () => {
expect(flat({ myString: '1' })).toEqual({ myString: '1' })
})

it('boolean', () => {
expect(flat({ myBool: true })).toEqual({ myBool: true })
expect(flat({ myBool: true })).toEqual({ myBool: 'true' })
})
})

describe('array of ', () => {
it('nulls:', () => {
expect(flat({ myNulls: [null, 1, null, 3] })).toEqual({
'myNulls.0': null,
'myNulls.1': 1,
'myNulls.2': null,
'myNulls.3': 3
'myNulls.0': 'null',
'myNulls.1': '1',
'myNulls.2': 'null',
'myNulls.3': '3'
})
})

it('numbers:', () => {
expect(flat({ myNumbers: [1, 2, 3, 4] })).toEqual({
'myNumbers.0': 1,
'myNumbers.1': 2,
'myNumbers.2': 3,
'myNumbers.3': 4
'myNumbers.0': '1',
'myNumbers.1': '2',
'myNumbers.2': '3',
'myNumbers.3': '4'
})
})

Expand All @@ -106,16 +106,18 @@ describe('flattenObj for ', () => {

it('booleans:', () => {
expect(flat({ myBools: [true, false, true, false] })).toEqual({
'myBools.0': true,
'myBools.1': false,
'myBools.2': true,
'myBools.3': false
'myBools.0': 'true',
'myBools.1': 'false',
'myBools.2': 'true',
'myBools.3': 'false'
})
})
})

it('Embedded object', () => {
expect(flat(embededObject())).toEqual(flattenObject())
const props = embededObject()
delete props.car.year
expect(flat(props)).toEqual(flattenObject())
})

it('primitive', () => {
Expand Down
17 changes: 13 additions & 4 deletions packages/destination-actions/src/destinations/heap/flat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { JSONValue } from '@segment/actions-core'

export type Properties = {
[k: string]: unknown
}

type FlattenProperties = object & {
[k: string]: JSONValue
[k: string]: string
}

export function flat(data: Properties, prefix = ''): FlattenProperties {
Expand All @@ -15,8 +13,19 @@ export function flat(data: Properties, prefix = ''): FlattenProperties {
const flatten = flat(data[key] as Properties, prefix + '.' + key)
result = { ...result, ...flatten }
} else {
result[(prefix + '.' + key).replace(/^\./, '')] = data[key] as JSONValue
const stringifiedValue = stringify(data[key])
result[(prefix + '.' + key).replace(/^\./, '')] = stringifiedValue
}
}
return result
}

function stringify(value: unknown): string {
if (typeof value === 'string') {
return value
}
if (typeof value === 'number' || typeof value === 'boolean') {
return value.toString()
}
return JSON.stringify(value)
}
46 changes: 46 additions & 0 deletions packages/destination-actions/src/destinations/heap/heapUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IntegrationError } from '@segment/actions-core'
import { Payload } from './trackEvent/generated-types'

export const getUserIdentifier = ({
identity,
anonymous_id
}: {
identity?: string | null
anonymous_id?: string | null
}): { [k: string]: string } => {
if (identity) {
return {
identity
}
}

if (anonymous_id) {
return {
anonymous_id
}
}
throw new IntegrationError('Either identity or anonymous id are required.')
}

export const getEventName = (payload: Payload) => {
let eventName: string | undefined
switch (payload.type) {
case 'track':
eventName = payload.event
break
case 'page':
eventName = payload.name ? `${payload.name} page viewed` : 'Page viewed'
break
case 'screen':
eventName = payload.name ? `${payload.name} screen viewed` : 'Screen viewed'
break
default:
eventName = 'track'
break
}

if (!eventName) {
return 'track'
}
return eventName
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@
exports[`Testing snapshot for Heap's trackEvent destination action: all fields 1`] = `
Object {
"app_id": "xqYHVWXiU0In",
"event": "track",
"idempotency_key": "xqYHVWXiU0In",
"identity": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "cloud-mode-destination",
"testType": "xqYHVWXiU0In",
},
"session_id": "xqYHVWXiU0In",
"timestamp": "2021-02-01T00:00:00.000Z",
"events": Array [
Object {
"custom_properties": Object {
"testType": "xqYHVWXiU0In",
},
"event": "track",
"idempotency_key": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"timestamp": "2021-02-01T00:00:00.000Z",
"user_identifier": Object {
"identity": "xqYHVWXiU0In",
},
},
],
"library": "Segment",
}
`;

exports[`Testing snapshot for Heap's trackEvent destination action: required fields 1`] = `
Object {
"app_id": "xqYHVWXiU0In",
"event": "track",
"idempotency_key": "xqYHVWXiU0In",
"identity": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"session_id": "xqYHVWXiU0In",
"events": Array [
Object {
"custom_properties": Object {},
"event": "track",
"idempotency_key": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "cloud-mode-destination",
},
"user_identifier": Object {
"identity": "xqYHVWXiU0In",
},
},
],
"library": "Segment",
}
`;
Loading