diff --git a/packages/browser-destinations/src/destinations/heap/constants.ts b/packages/browser-destinations/src/destinations/heap/constants.ts index cbfd822ad5..efaffb18ad 100644 --- a/packages/browser-destinations/src/destinations/heap/constants.ts +++ b/packages/browser-destinations/src/destinations/heap/constants.ts @@ -1 +1 @@ -export const HEAP_SEGMENT_LIBRARY_NAME = 'destinations-actions' +export const HEAP_SEGMENT_BROWSER_LIBRARY_NAME = 'browser-destination' diff --git a/packages/browser-destinations/src/destinations/heap/trackEvent/__tests__/index.test.ts b/packages/browser-destinations/src/destinations/heap/trackEvent/__tests__/index.test.ts index cd2ac3f1f6..09019708ad 100644 --- a/packages/browser-destinations/src/destinations/heap/trackEvent/__tests__/index.test.ts +++ b/packages/browser-destinations/src/destinations/heap/trackEvent/__tests__/index.test.ts @@ -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]> => { @@ -34,7 +34,7 @@ describe('#trackEvent', () => { expect(heapTrackSpy).toHaveBeenCalledWith('hello!', { banana: '📞', - segment_library: HEAP_SEGMENT_LIBRARY_NAME + segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME }) }) @@ -48,7 +48,7 @@ describe('#trackEvent', () => { ) expect(heapTrackSpy).toHaveBeenCalledWith('hello!', { - segment_library: HEAP_SEGMENT_LIBRARY_NAME + segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME }) }) @@ -66,7 +66,7 @@ describe('#trackEvent', () => { ) expect(heapTrackSpy).toHaveBeenCalledWith('hello!', { - segment_library: segmentLibraryValue + segment_library: HEAP_SEGMENT_BROWSER_LIBRARY_NAME }) }) }) diff --git a/packages/browser-destinations/src/destinations/heap/trackEvent/index.ts b/packages/browser-destinations/src/destinations/heap/trackEvent/index.ts index 74c2df64e8..79c672e473 100644 --- a/packages/browser-destinations/src/destinations/heap/trackEvent/index.ts +++ b/packages/browser-destinations/src/destinations/heap/trackEvent/index.ts @@ -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 = { title: 'Track Event', @@ -30,8 +30,8 @@ const action: BrowserActionDefinition = { } }, 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) } } diff --git a/packages/destination-actions/src/destinations/heap/__tests__/__snapshots__/snapshot.test.ts.snap b/packages/destination-actions/src/destinations/heap/__tests__/__snapshots__/snapshot.test.ts.snap index 3c20756a85..e1c3bdb32b 100644 --- a/packages/destination-actions/src/destinations/heap/__tests__/__snapshots__/snapshot.test.ts.snap +++ b/packages/destination-actions/src/destinations/heap/__tests__/__snapshots__/snapshot.test.ts.snap @@ -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", } `; @@ -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", } `; diff --git a/packages/destination-actions/src/destinations/heap/constants.ts b/packages/destination-actions/src/destinations/heap/constants.ts index cbfd822ad5..411c4aaf1e 100644 --- a/packages/destination-actions/src/destinations/heap/constants.ts +++ b/packages/destination-actions/src/destinations/heap/constants.ts @@ -1 +1 @@ -export const HEAP_SEGMENT_LIBRARY_NAME = 'destinations-actions' +export const HEAP_SEGMENT_CLOUD_LIBRARY_NAME = 'cloud-mode-destination' diff --git a/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/__snapshots__/snapshot.test.ts.snap b/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/__snapshots__/snapshot.test.ts.snap index b806d0d773..be3d81250b 100644 --- a/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/__snapshots__/snapshot.test.ts.snap +++ b/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/__snapshots__/snapshot.test.ts.snap @@ -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", } `; @@ -19,7 +20,8 @@ Object { "idempotency_key": "xqYHVWXiU0In", "identity": "xqYHVWXiU0In", "properties": Object { - "segment_library": "destinations-actions", + "segment_library": "cloud-mode-destination", }, + "session_id": "xqYHVWXiU0In", } `; diff --git a/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/index.test.ts b/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/index.test.ts index 1a93a4f2bf..e42bdac598 100644 --- a/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/index.test.ts +++ b/packages/destination-actions/src/destinations/heap/trackEvent/__tests__/index.test.ts @@ -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) @@ -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 } @@ -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, {}) diff --git a/packages/destination-actions/src/destinations/heap/trackEvent/generated-types.ts b/packages/destination-actions/src/destinations/heap/trackEvent/generated-types.ts index 1e3986ca46..ac84e4fc93 100644 --- a/packages/destination-actions/src/destinations/heap/trackEvent/generated-types.ts +++ b/packages/destination-actions/src/destinations/heap/trackEvent/generated-types.ts @@ -28,9 +28,9 @@ export interface Payload { */ timestamp?: string | number /** - * The name of the SDK used to send events + * 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. */ - library_name?: string + session_id?: string /** * The type of call. Can be track, page, or screen. */ diff --git a/packages/destination-actions/src/destinations/heap/trackEvent/index.ts b/packages/destination-actions/src/destinations/heap/trackEvent/index.ts index ceda53c52d..b82b51c327 100644 --- a/packages/destination-actions/src/destinations/heap/trackEvent/index.ts +++ b/packages/destination-actions/src/destinations/heap/trackEvent/index.ts @@ -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' @@ -17,6 +17,7 @@ type HeapEvent = { } idempotency_key: string timestamp?: string + session_id?: string } const action: ActionDefinition = { @@ -77,12 +78,13 @@ const action: ActionDefinition = { '@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: { @@ -111,10 +113,11 @@ const action: ActionDefinition = { 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, @@ -122,20 +125,24 @@ const action: ActionDefinition = { } 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 }) } }