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 34916 #787

Merged
merged 12 commits into from
Jan 23, 2023
Merged
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const HEAP_SEGMENT_LIBRARY_NAME = 'destinations-actions'
export const HEAP_SEGMENT_BROWSER_LIBRARY_NAME = 'browser-destination'
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
mockHeapJsHttpRequest,
trackEventSubscription
} from '../../test-utilities'
import { HEAP_SEGMENT_LIBRARY_NAME } from '../../constants'
import { HEAP_SEGMENT_BROWSER_LIBRARY_NAME } from '../../constants'

describe('#trackEvent', () => {
const createHeapDestinationAndSpy = async (): Promise<[Plugin, jest.SpyInstance]> => {
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('#trackEvent', () => {

expect(heapTrackSpy).toHaveBeenCalledWith('hello!', {
banana: '📞',
segment_library: HEAP_SEGMENT_LIBRARY_NAME
segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME
})
})

Expand All @@ -48,7 +48,7 @@ describe('#trackEvent', () => {
)

expect(heapTrackSpy).toHaveBeenCalledWith('hello!', {
segment_library: HEAP_SEGMENT_LIBRARY_NAME
segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME
})
})

Expand All @@ -66,7 +66,7 @@ describe('#trackEvent', () => {
)

expect(heapTrackSpy).toHaveBeenCalledWith('hello!', {
segment_library: segmentLibraryValue
segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BrowserActionDefinition } from '../../../lib/browser-destinations'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { HeapApi } from '../types'
import { HEAP_SEGMENT_LIBRARY_NAME } from '../constants'
import { HEAP_SEGMENT_BROWSER_LIBRARY_NAME } from '../constants'

const action: BrowserActionDefinition<Settings, HeapApi, Payload> = {
title: 'Track Event',
Expand Down Expand Up @@ -30,8 +30,8 @@ const action: BrowserActionDefinition<Settings, HeapApi, Payload> = {
}
},
perform: (heap, event) => {
const defaultEventProperties = { segment_library: HEAP_SEGMENT_LIBRARY_NAME }
const eventProperties = Object.assign(defaultEventProperties, event.payload.properties ?? {})
const eventProperties = Object.assign({}, event.payload.properties ?? {})
eventProperties.segment_library = HEAP_SEGMENT_BROWSER_LIBRARY_NAME
heap.track(event.payload.name, eventProperties)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ Object {
"idempotency_key": "kFJwrNh$DP38FB",
"identity": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "kFJwrNh$DP38FB",
"segment_library": "cloud-mode-destination",
"testType": "kFJwrNh$DP38FB",
},
"session_id": "kFJwrNh$DP38FB",
"timestamp": "2021-02-01T00:00:00.000Z",
}
`;
Expand All @@ -35,7 +36,8 @@ Object {
"idempotency_key": "kFJwrNh$DP38FB",
"identity": "kFJwrNh$DP38FB",
"properties": Object {
"segment_library": "destinations-actions",
"segment_library": "cloud-mode-destination",
},
"session_id": "kFJwrNh$DP38FB",
}
`;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const HEAP_SEGMENT_LIBRARY_NAME = 'destinations-actions'
export const HEAP_SEGMENT_CLOUD_LIBRARY_NAME = 'cloud-mode-destination'
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ Object {
"idempotency_key": "xqYHVWXiU0In",
"identity": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "xqYHVWXiU0In",
"segment_library": "cloud-mode-destination",
"testType": "xqYHVWXiU0In",
},
"session_id": "xqYHVWXiU0In",
"timestamp": "2021-02-01T00:00:00.000Z",
}
`;
Expand All @@ -19,7 +20,8 @@ Object {
"idempotency_key": "xqYHVWXiU0In",
"identity": "xqYHVWXiU0In",
"properties": Object {
"segment_library": "destinations-actions",
"segment_library": "cloud-mode-destination",
},
"session_id": "xqYHVWXiU0In",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import nock from 'nock'
import { createTestEvent, createTestIntegration, JSONValue, SegmentEvent } from '@segment/actions-core'
import Destination from '../../index'
import { flattenObject, embededObject } from '../../__tests__/flat.test'
import { HEAP_SEGMENT_CLOUD_LIBRARY_NAME } from '../../constants'

describe('Heap.trackEvent', () => {
const testDestination = createTestIntegration(Destination)
Expand All @@ -27,7 +28,7 @@ describe('Heap.trackEvent', () => {
event: eventName,
idempotency_key: messageId,
properties: {
segment_library: 'analytics.js'
segment_library: HEAP_SEGMENT_CLOUD_LIBRARY_NAME
},
timestamp
}
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('Heap.trackEvent', () => {

body.user_id = 8325872782136936
body.properties = {
segment_library: 'analytics.js',
segment_library: HEAP_SEGMENT_CLOUD_LIBRARY_NAME,
...flattenObject()
}
nock('https://heapanalytics.com').post('/api/track', body).reply(200, {})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import dayjs from '../../../lib/dayjs'
import { HEAP_SEGMENT_LIBRARY_NAME } from '../constants'
import { HEAP_SEGMENT_CLOUD_LIBRARY_NAME } from '../constants'
import { getHeapUserId } from '../userIdHash'
import { flat } from '../flat'
import { IntegrationError } from '@segment/actions-core'
Expand All @@ -17,6 +17,7 @@ type HeapEvent = {
}
idempotency_key: string
timestamp?: string
session_id?: string
}

const action: ActionDefinition<Settings, Payload> = {
Expand Down Expand Up @@ -77,12 +78,13 @@ const action: ActionDefinition<Settings, Payload> = {
'@path': '$.timestamp'
}
},
library_name: {
label: 'Library Name',
session_id: {
label: 'Session ID',
type: 'string',
description: 'The name of the SDK used to send events',
description:
'A Heap session ID. The session ID can be retrived by calling getSessionId() on the heap api. If a session ID is not provided one will be created.',
default: {
'@path': '$.context.library.name'
'@path': '$.session_id'
}
},
type: {
Expand Down Expand Up @@ -111,31 +113,36 @@ const action: ActionDefinition<Settings, Payload> = {
throw new IntegrationError('Either anonymous user id or identity should be specified.')
}

const defaultEventProperties = { segment_library: payload.library_name || HEAP_SEGMENT_LIBRARY_NAME }
const defaultEventProperties = { segment_library: HEAP_SEGMENT_CLOUD_LIBRARY_NAME }
const flatten = flat(payload.properties || {})
const eventProperties = Object.assign(defaultEventProperties, flatten)
const event: HeapEvent = {

const heapPayload: HeapEvent = {
app_id: settings.appId,
event: getEventName(payload),
properties: eventProperties,
idempotency_key: payload.message_id
}

if (payload.anonymous_id && !payload.identity) {
event.user_id = getHeapUserId(payload.anonymous_id)
heapPayload.user_id = getHeapUserId(payload.anonymous_id)
}

if (payload.identity) {
event.identity = payload.identity
heapPayload.identity = payload.identity
}

if (payload.timestamp && dayjs.utc(payload.timestamp).isValid()) {
event.timestamp = dayjs.utc(payload.timestamp).toISOString()
heapPayload.timestamp = dayjs.utc(payload.timestamp).toISOString()
}

if (payload.session_id) {
heapPayload.session_id = payload.session_id
}

return request('https://heapanalytics.com/api/track', {
method: 'post',
json: event
json: heapPayload
})
}
}
Expand Down