-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HEAP] update browser destination code to load heap js v5 script (#2665)
* feat(heap): update browser destination code to load heap js v5 script * remove try/catch fallback for self-hosted * update tests * fix tests * apply deprecated trackingServer config value for backward compatibility * remove non-functional script loading mock * better backward compatibility with hjsv4 * assert heap.envId, not heap.appid
- Loading branch information
Showing
8 changed files
with
213 additions
and
59 deletions.
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
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.