-
Notifications
You must be signed in to change notification settings - Fork 254
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] update browser destination code to load heap js v5 script #2665
Open
cpsoinos
wants to merge
8
commits into
segmentio:main
Choose a base branch
from
heap:PA-4600_segment-actions-hjsv5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a582fc
feat(heap): update browser destination code to load heap js v5 script
cpsoinos 94674eb
remove try/catch fallback for self-hosted
cpsoinos a66aaa3
update tests
cpsoinos de3accc
fix tests
cpsoinos 0eb2f6a
apply deprecated trackingServer config value for backward compatibility
cpsoinos 4a69775
remove non-functional script loading mock
cpsoinos a947036
better backward compatibility with hjsv4
cpsoinos 220f853
assert heap.envId, not heap.appid
cpsoinos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
packages/browser-destinations/destinations/heap/src/generated-types.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import type { Settings } from './generated-types' | ||
import type { BrowserDestinationDefinition } from '@segment/browser-destination-runtime/types' | ||
import { browserDestination } from '@segment/browser-destination-runtime/shim' | ||
import { HeapApi, UserConfig } from './types' | ||
import type { HeapApi, HeapMethods, UserConfig } from './types' | ||
import { defaultValues } from '@segment/actions-core' | ||
import trackEvent from './trackEvent' | ||
import identifyUser from './identifyUser' | ||
import { initScript } from './init-script' | ||
import { isDefined } from './utils' | ||
|
||
declare global { | ||
interface Window { | ||
heapReadyCb: Array<{ name: HeapMethods; fn: () => void }> | ||
heap: HeapApi | ||
} | ||
} | ||
|
@@ -46,28 +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', | ||
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 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: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: adding a new 'optional' setting is OK. |
||
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).', | ||
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 | ||
}, | ||
|
@@ -87,26 +96,29 @@ export const destination: BrowserDestinationDefinition<Settings, HeapApi> = { | |
|
||
const config: UserConfig = { | ||
disableTextCapture: settings.disableTextCapture || false, | ||
secureCookie: settings.secureCookie || false | ||
secureCookie: settings.secureCookie || false, | ||
...(settings.ingestServer && { ingestServer: settings.ingestServer }), | ||
// For backward compatibility. See https://developers.heap.io/docs/web#ingestserver | ||
...(settings.trackingServer && { trackingServer: settings.trackingServer }) | ||
} | ||
|
||
if (settings.trackingServer) { | ||
config.trackingServer = settings.trackingServer | ||
} | ||
|
||
// heap.appid and heap.config must be set before loading heap.js. | ||
window.heap = window.heap || [] | ||
window.heap.appid = settings.appId | ||
window.heap.config = config | ||
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`) | ||
} else { | ||
await deps.loadScript(`https://cdn.heapanalytics.com/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`) | ||
} | ||
// 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`) | ||
} | ||
|
||
// Explained here: https://stackoverflow.com/questions/14859058/why-does-the-segment-io-loader-script-push-method-names-args-onto-a-queue-which | ||
await deps.resolveWhen(() => Object.prototype.hasOwnProperty.call(window, 'heap'), 100) | ||
await deps.resolveWhen(() => window.heap.loaded === true, 300) | ||
|
||
return window.heap | ||
}, | ||
|
69 changes: 69 additions & 0 deletions
69
packages/browser-destinations/destinations/heap/src/init-script.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { HeapMethods, UserConfig } from './types' | ||
|
||
/** | ||
* Initialize the Heap script with the provided environment ID and configuration. | ||
*/ | ||
export const initScript = (envId: string, config: UserConfig) => { | ||
// Ensure heapReadyCb exists on the window object | ||
window.heapReadyCb = window.heapReadyCb || [] | ||
|
||
// Ensure heap exists on the window object | ||
window.heap = window.heap || ({} as any) | ||
|
||
window.heap.load = function ( | ||
envId: string, | ||
clientConfig: UserConfig = { disableTextCapture: false, secureCookie: false } | ||
): void { | ||
window.heap.envId = envId | ||
window.heap.clientConfig = clientConfig | ||
window.heap.clientConfig.shouldFetchServerConfig = false | ||
|
||
// Define all Heap API methods and add them to the heap object | ||
const methods: HeapMethods[] = [ | ||
'init', | ||
'startTracking', | ||
'stopTracking', | ||
'track', | ||
'resetIdentity', | ||
'identify', | ||
'identifyHashed', | ||
'getSessionId', | ||
'getUserId', | ||
'getIdentity', | ||
'addUserProperties', | ||
'addEventProperties', | ||
'removeEventProperty', | ||
'clearEventProperties', | ||
'addAccountProperties', | ||
'addAdapter', | ||
'addTransformer', | ||
'addTransformerFn', | ||
'onReady', | ||
'addPageviewProperties', | ||
'removePageviewProperty', | ||
'clearPageviewProperties', | ||
'trackPageview' | ||
] | ||
|
||
const createMethodProxy = (methodName: HeapMethods) => { | ||
return function (...args: any[]) { | ||
// Push method calls to heapReadyCb until the script is fully loaded | ||
window.heapReadyCb.push({ | ||
name: methodName, | ||
fn: () => { | ||
if (window.heap[methodName]) { | ||
window.heap[methodName](...args) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Proxy all methods to heap | ||
for (const method of methods) { | ||
window.heap[method] = createMethodProxy(method) | ||
} | ||
} | ||
|
||
window.heap.load(envId, config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @cpsoinos looks like you have labeled this as deprecated. Is it still referenced anywhere in code though? i.e is it actually used at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @joe-ayoub-segment, I updated my changes so that this setting is still used, though it is still deprecated. Customers that wish to self-host will have set both
trackingServer
andhostname
. When both of these settings are set, Segment will load the Classic SDK (retaining current functionality).