Skip to content

Commit

Permalink
feat: unload analytics script on fail, use id appropriate to environment
Browse files Browse the repository at this point in the history
  • Loading branch information
wdoconnell committed Oct 1, 2024
1 parent 36adbc8 commit d0c6f11
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {AppState} from 'src/types'
// Utils
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
import {CLOUD} from 'src/shared/constants'
import {executeVWO, executeHeap} from 'src/utils/analyticsTools'
import {executeVWO, executeHeap, unloadHeap} from 'src/utils/analyticsTools'

// Providers
import {UserAccountProvider} from 'src/accounts/context/userAccount'
Expand Down Expand Up @@ -108,6 +108,7 @@ const App: FC = () => {
try {
executeHeap()
} catch (err) {
unloadHeap()
reportErrorThroughHoneyBadger(err, {
name: 'Unable to load Heap Analytics',
})
Expand Down
44 changes: 40 additions & 4 deletions src/utils/analyticsTools.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// this code is related to implementing the A/B testing tool VMO: https://vwo.com/
// Whenever this script is updated, ensure that any changes are reflected in the
// error handling in App.tsx, especially for any elements that change opacity of the page.
/* eslint-disable */
// @ts-nocheck

import { getFlagValue } from 'src/shared/utils/featureFlag'

/* This file is exclusively for third-party analytics tools used by the Cloud2 UI. */

/* This code implements the A/B testing tool VWO: https://vwo.com/
The copy is largely copy-pasted from VWO instructions and is not maintained by InfluxData.
Whenever this script is updated, ensure that any changes are reflected in the
error handling in App.tsx, especially for any elements that change opacity of the page. */
export const executeVWO = () => {
window._vwo_code =
window._vwo_code ||
Expand Down Expand Up @@ -117,7 +123,23 @@ export const executeVWO = () => {
})()
}

const HEAP_API_SCRIPT_SRC = 'heap-api.com'

/*
The JS code in this function is copied from the installation instructions
at https://developers.heap.io/docs/web and is not maintained by InfluxData.
Semicolons have been added to avoid ASI issues because this script uses IIFEs.
// Example: https://circleci.com/blog/ci-cd-for-js-iifes/
*/
export const executeHeap = () => {
// Retrieve the heap analytics id appropriate to the environment from ConfigCat.
const heapId = getFlagValue('heapAnalyticsId')?.toString()
if (!heapId) {
return
}

// This block is imported from Heap.
;(window.heapReadyCb = window.heapReadyCb || []),
(window.heap = window.heap || []),
(heap.load = function (e, t) {
Expand Down Expand Up @@ -167,5 +189,19 @@ export const executeHeap = () => {
}
for (var p = 0; p < n.length; p++) heap[n[p]] = i(n[p])
})
heap.load('1919519062')

heap.load(heapId)
}

// This unloads all artifacts from the Heap script if an error is encountered.
export const unloadHeap = () => {
delete window.heap
delete window.heapReadyCb

const scripts = document.getElementsByTagName('script')
for (let s of scripts) {
if (s.src.includes(HEAP_API_SCRIPT_SRC)) {
s.remove()
}
}
}

0 comments on commit d0c6f11

Please sign in to comment.