Skip to content

Commit a2cea56

Browse files
peterbarnett03Peter Barnettwdoconnell
authored
chore: add Heap analytics (#6951)
* chore: add Heap analytics * feat: unload analytics script on fail, use id appropriate to environment * fix: correct capitalization of heapanalyticsid --------- Co-authored-by: Peter Barnett <[email protected]> Co-authored-by: Bill OConnell <[email protected]>
1 parent ff83a1e commit a2cea56

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

src/App.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {AppState} from 'src/types'
3838
// Utils
3939
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
4040
import {CLOUD} from 'src/shared/constants'
41-
import {executeVWO} from 'src/utils/vwo'
41+
import {executeVWO, executeHeap, unloadHeap} from 'src/utils/analyticsTools'
4242

4343
// Providers
4444
import {UserAccountProvider} from 'src/accounts/context/userAccount'
@@ -57,14 +57,14 @@ const App: FC = () => {
5757
if (CLOUD && isFlagEnabled('rudderstackReporting')) {
5858
try {
5959
load(RUDDERSTACK_WRITE_KEY, RUDDERSTACK_DATA_PLANE_URL)
60-
} catch (error) {
60+
} catch (err) {
6161
console.error(
6262
'Error loading Rudderstack with wk: ',
6363
RUDDERSTACK_WRITE_KEY,
6464
' at: ',
6565
RUDDERSTACK_DATA_PLANE_URL
6666
)
67-
reportErrorThroughHoneyBadger(error, {
67+
reportErrorThroughHoneyBadger(err, {
6868
name: 'Rudderstack Loading Function',
6969
})
7070
}
@@ -104,6 +104,17 @@ const App: FC = () => {
104104
}
105105
}
106106

107+
if (CLOUD && isFlagEnabled('heapAnalytics')) {
108+
try {
109+
executeHeap()
110+
} catch (err) {
111+
unloadHeap()
112+
reportErrorThroughHoneyBadger(err, {
113+
name: 'Unable to load Heap Analytics',
114+
})
115+
}
116+
}
117+
107118
setAutoFreeze(false)
108119
}, [])
109120

src/utils/vwo.ts src/utils/analyticsTools.ts

+92-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
// this code is related to implementing the A/B testing tool VMO: https://vwo.com/
2-
// Whenever this script is updated, ensure that any changes are reflected in the
3-
// error handling in App.tsx, especially for any elements that change opacity of the page.
41
/* eslint-disable */
52
// @ts-nocheck
3+
4+
import {getFlagValue} from 'src/shared/utils/featureFlag'
5+
6+
/* This file is exclusively for third-party analytics tools used by the Cloud2 UI. */
7+
8+
/* This code implements the A/B testing tool VWO: https://vwo.com/
9+
The copy is largely copy-pasted from VWO instructions and is not maintained by InfluxData.
10+
Whenever this script is updated, ensure that any changes are reflected in the
11+
error handling in App.tsx, especially for any elements that change opacity of the page. */
612
export const executeVWO = () => {
713
window._vwo_code =
814
window._vwo_code ||
@@ -116,3 +122,86 @@ export const executeVWO = () => {
116122
return code
117123
})()
118124
}
125+
126+
const HEAP_API_SCRIPT_SRC = 'heap-api.com'
127+
128+
/*
129+
The JS code in this function is copied from the installation instructions
130+
at https://developers.heap.io/docs/web and is not maintained by InfluxData.
131+
132+
Semicolons have been added to avoid ASI issues because this script uses IIFEs.
133+
// Example: https://circleci.com/blog/ci-cd-for-js-iifes/
134+
*/
135+
export const executeHeap = () => {
136+
// Retrieve the heap analytics id appropriate to the environment from ConfigCat.
137+
const heapId = getFlagValue('heapanalyticsid')
138+
if (!heapId) {
139+
return
140+
}
141+
142+
// This block is imported from Heap.
143+
;(window.heapReadyCb = window.heapReadyCb || []),
144+
(window.heap = window.heap || []),
145+
(heap.load = function (e, t) {
146+
;(window.heap.envId = e),
147+
(window.heap.clientConfig = t = t || {}),
148+
(window.heap.clientConfig.shouldFetchServerConfig = !1)
149+
var a = document.createElement('script')
150+
;(a.type = 'text/javascript'),
151+
(a.async = !0),
152+
(a.src = 'https://cdn.us.heap-api.com/config/' + e + '/heap_config.js')
153+
var r = document.getElementsByTagName('script')[0]
154+
r.parentNode.insertBefore(a, r)
155+
var n = [
156+
'init',
157+
'startTracking',
158+
'stopTracking',
159+
'track',
160+
'resetIdentity',
161+
'identify',
162+
'getSessionId',
163+
'getUserId',
164+
'getIdentity',
165+
'addUserProperties',
166+
'addEventProperties',
167+
'removeEventProperty',
168+
'clearEventProperties',
169+
'addAccountProperties',
170+
'addAdapter',
171+
'addTransformer',
172+
'addTransformerFn',
173+
'onReady',
174+
'addPageviewProperties',
175+
'removePageviewProperty',
176+
'clearPageviewProperties',
177+
'trackPageview',
178+
],
179+
i = function (e) {
180+
return function () {
181+
var t = Array.prototype.slice.call(arguments, 0)
182+
window.heapReadyCb.push({
183+
name: e,
184+
fn: function () {
185+
heap[e] && heap[e].apply(heap, t)
186+
},
187+
})
188+
}
189+
}
190+
for (var p = 0; p < n.length; p++) heap[n[p]] = i(n[p])
191+
})
192+
193+
heap.load(heapId.toString())
194+
}
195+
196+
// This unloads all artifacts from the Heap script if an error is encountered.
197+
export const unloadHeap = () => {
198+
delete window.heap
199+
delete window.heapReadyCb
200+
201+
const scripts = document.getElementsByTagName('script')
202+
for (let s of scripts) {
203+
if (s.src.includes(HEAP_API_SCRIPT_SRC)) {
204+
s.remove()
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)