Skip to content

Commit

Permalink
better backward compatibility with hjsv4
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsoinos committed Jan 3, 2025
1 parent 4a69775 commit a947036
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Analytics, Context } from '@segment/analytics-next'
import heapDestination, { destination } from '../index'
import { HEAP_TEST_ENV_ID } from '../test-utilities'
import { loadScript } from '@segment/browser-destination-runtime/load-script'
import { resolveWhen } from '@segment/browser-destination-runtime/resolve-when'

jest.mock('@segment/browser-destination-runtime/load-script')
jest.mock('@segment/browser-destination-runtime/resolve-when')

const subscriptions = [
{
Expand All @@ -13,20 +18,34 @@ const subscriptions = [
]

describe('Heap', () => {
beforeAll(() => {
;(loadScript as jest.Mock).mockResolvedValue(true)
;(resolveWhen as jest.Mock).mockResolvedValue(true)
})

beforeEach(() => {
// Reset global state
// @ts-expect-error
delete window.heap
})

afterEach(async () => {
jest.clearAllMocks()
})

test('loading', async () => {
jest.spyOn(destination, 'initialize')

const [event] = await heapDestination({ appId: HEAP_TEST_ENV_ID, subscriptions })

await event.load(Context.system(), {} as Analytics)

expect(destination.initialize).toHaveBeenCalled()
expect(window.heap.appid).toEqual(HEAP_TEST_ENV_ID)

const scripts = window.document.querySelectorAll('script')
expect(scripts).toMatchSnapshot()
expect(loadScript).toHaveBeenCalledWith(`https://cdn.us.heap-api.com/config/${HEAP_TEST_ENV_ID}/heap_config.js`)
})

test('loading with cdn', async () => {
test('loading classic SDK with custom hostname', async () => {
jest.spyOn(destination, 'initialize')

const [event] = await heapDestination({
Expand All @@ -37,7 +56,25 @@ describe('Heap', () => {
})

await event.load(Context.system(), {} as Analytics)

expect(destination.initialize).toHaveBeenCalled()
expect(window.heap.appid).toEqual(HEAP_TEST_ENV_ID)
expect(loadScript).toHaveBeenCalledWith(`https://cdn.heapanalytics.com/js/heap-${HEAP_TEST_ENV_ID}.js`)
})

test('loading latest SDK with custom hostname', async () => {
jest.spyOn(destination, 'initialize')

const [event] = await heapDestination({
appId: HEAP_TEST_ENV_ID,
subscriptions,
hostname: 'cdn.heapanalytics.com'
})

await event.load(Context.system(), {} as Analytics)

expect(destination.initialize).toHaveBeenCalled()
expect(window.heap.appid).toEqual(HEAP_TEST_ENV_ID)
expect(loadScript).toHaveBeenCalledWith(`https://cdn.heapanalytics.com/config/${HEAP_TEST_ENV_ID}/heap_config.js`)
})
})

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

21 changes: 14 additions & 7 deletions packages/browser-destinations/destinations/heap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,35 @@ export const destination: BrowserDestinationDefinition<Settings, HeapApi> = {
disableTextCapture: {
label: 'Global data redaction via Disabling Text Capture',
description:
'Setting to true will redact all target text on your website. For more information visit the heap [docs page](https://developers.heap.io/docs/web#global-data-redaction-via-disabling-text-capture).',
'Setting to true will redact all target text on your website. For more information visit the Heap [docs page](https://developers.heap.io/docs/web#global-data-redaction-via-disabling-text-capture).',
type: 'boolean',
required: false
},
secureCookie: {
label: 'Secure Cookie',
description:
'This option is turned off by default to accommodate websites not served over HTTPS. If your application uses HTTPS, we recommend enabling secure cookies to prevent Heap cookies from being observed by unauthorized parties. For more information visit the heap [docs page](https://developers.heap.io/docs/web#securecookie).',
'This option is turned off by default to accommodate websites not served over HTTPS. If your application uses HTTPS, we recommend enabling secure cookies to prevent Heap cookies from being observed by unauthorized parties. For more information visit the Heap [docs page](https://developers.heap.io/docs/web#securecookie).',
type: 'boolean',
required: false
},
trackingServer: {
label: 'Tracking Server (deprecated)',
description:
'This is an optional setting. This is used to set up first-party data collection. For most cased this should not be set. For more information visit the heap [docs page](https://developers.heap.io/docs/set-up-first-party-data-collection-in-heap). This field is deprecated in favor of "ingestServer".',
'This is an optional setting. This is used to set up first-party data collection. For most cased this should not be set. For more information visit the Heap [docs page](https://developers.heap.io/docs/set-up-first-party-data-collection-in-heap). This field is deprecated in favor of `ingestServer`. If `trackingServer` is set and `ingestServer` is not set, then the Classic SDK will be loaded. If both are set, `ingestServer` will take precedence, and the latest stable version of the Heap SDK will be loaded.',
type: 'string',
required: false
},
ingestServer: {
label: 'Ingest Server',
description:
'This is an optional setting. This is used to set up first-party data collection. For most cased this should not be set. For more information visit the heap [docs page](https://developers.heap.io/docs/web#ingestserver).',
'This is an optional setting. This is used to set up first-party data collection. For most cased this should not be set. For more information visit the Heap [docs page](https://developers.heap.io/docs/web#ingestserver).',
type: 'string',
required: false
},
hostname: {
label: 'Hostname',
description:
'This is an optional setting used to set the host that loads heap-js. This setting is used when heapJS is self-hosted. In most cased this should be left unset. The hostname should not contain https or app id it will be populated like so: https://${hostname}/js/heap-${appId}.js. For more information visit the heap [docs page](https://developers.heap.io/docs/self-hosting-heapjs).',
'This is an optional setting used to set the host that loads the Heap SDK. This setting is used when the Heap SDK is self-hosted. In most cased this should be left unset. The hostname should not contain https or app id. When _both_ `hostname` and `trackingServer` are set, the Classic SDK will be loaded via: `https://${hostname}/js/heap-${appId}.js`. If `hostname` is set and `trackingServer` is not set, then the latest version of the Heap SDK will be loaded via: `https://${settings.hostname}/config/${settings.appId}/heap_config.js`. For more information visit the Heap [docs page](https://developers.heap.io/docs/self-hosting-heapjs).',
type: 'string',
required: false
},
Expand Down Expand Up @@ -104,9 +104,16 @@ export const destination: BrowserDestinationDefinition<Settings, HeapApi> = {

initScript(settings.appId, config)

if (isDefined(settings.hostname)) {
// if both hostname and trackingServer are set, load classic SDK from hostname
if (isDefined(settings.hostname) && isDefined(settings.trackingServer)) {
await deps.loadScript(`https://${settings.hostname}/js/heap-${settings.appId}.js`)
}
// if only hostname is set, load latest version of SDK from hostname
else if (isDefined(settings.hostname) && !isDefined(settings.trackingServer)) {
await deps.loadScript(`https://${settings.hostname}/config/${settings.appId}/heap_config.js`)
} else {
}
// default to loading latest version of SDK from heap CDN
else {
await deps.loadScript(`https://cdn.us.heap-api.com/config/${settings.appId}/heap_config.js`)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const initScript = (envId: string, config: UserConfig) => {
envId: string,
clientConfig: UserConfig = { disableTextCapture: false, secureCookie: false }
): void {
window.heap.appid = envId
window.heap.envId = envId
window.heap.clientConfig = clientConfig
window.heap.clientConfig.shouldFetchServerConfig = false
Expand Down

0 comments on commit a947036

Please sign in to comment.