From 36adbc86f5e84ac3e8f7d6e9fc8d620d0052c7b0 Mon Sep 17 00:00:00 2001 From: Peter Barnett Date: Tue, 17 Sep 2024 17:26:42 -0400 Subject: [PATCH] chore: add Heap analytics --- src/App.tsx | 16 ++++++-- src/utils/{vwo.ts => analyticsTools.ts} | 53 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) rename src/utils/{vwo.ts => analyticsTools.ts} (70%) diff --git a/src/App.tsx b/src/App.tsx index 12870e1e25..c3b634c780 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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} from 'src/utils/vwo' +import {executeVWO, executeHeap} from 'src/utils/analyticsTools' // Providers import {UserAccountProvider} from 'src/accounts/context/userAccount' @@ -57,14 +57,14 @@ const App: FC = () => { if (CLOUD && isFlagEnabled('rudderstackReporting')) { try { load(RUDDERSTACK_WRITE_KEY, RUDDERSTACK_DATA_PLANE_URL) - } catch (error) { + } catch (err) { console.error( 'Error loading Rudderstack with wk: ', RUDDERSTACK_WRITE_KEY, ' at: ', RUDDERSTACK_DATA_PLANE_URL ) - reportErrorThroughHoneyBadger(error, { + reportErrorThroughHoneyBadger(err, { name: 'Rudderstack Loading Function', }) } @@ -104,6 +104,16 @@ const App: FC = () => { } } + if (CLOUD && isFlagEnabled('heapAnalytics')) { + try { + executeHeap() + } catch (err) { + reportErrorThroughHoneyBadger(err, { + name: 'Unable to load Heap Analytics', + }) + } + } + setAutoFreeze(false) }, []) diff --git a/src/utils/vwo.ts b/src/utils/analyticsTools.ts similarity index 70% rename from src/utils/vwo.ts rename to src/utils/analyticsTools.ts index 6862953df0..9630b23c8e 100644 --- a/src/utils/vwo.ts +++ b/src/utils/analyticsTools.ts @@ -116,3 +116,56 @@ export const executeVWO = () => { return code })() } + +export const executeHeap = () => { + ;(window.heapReadyCb = window.heapReadyCb || []), + (window.heap = window.heap || []), + (heap.load = function (e, t) { + ;(window.heap.envId = e), + (window.heap.clientConfig = t = t || {}), + (window.heap.clientConfig.shouldFetchServerConfig = !1) + var a = document.createElement('script') + ;(a.type = 'text/javascript'), + (a.async = !0), + (a.src = 'https://cdn.us.heap-api.com/config/' + e + '/heap_config.js') + var r = document.getElementsByTagName('script')[0] + r.parentNode.insertBefore(a, r) + var n = [ + 'init', + 'startTracking', + 'stopTracking', + 'track', + 'resetIdentity', + 'identify', + 'getSessionId', + 'getUserId', + 'getIdentity', + 'addUserProperties', + 'addEventProperties', + 'removeEventProperty', + 'clearEventProperties', + 'addAccountProperties', + 'addAdapter', + 'addTransformer', + 'addTransformerFn', + 'onReady', + 'addPageviewProperties', + 'removePageviewProperty', + 'clearPageviewProperties', + 'trackPageview', + ], + i = function (e) { + return function () { + var t = Array.prototype.slice.call(arguments, 0) + window.heapReadyCb.push({ + name: e, + fn: function () { + heap[e] && heap[e].apply(heap, t) + }, + }) + } + } + for (var p = 0; p < n.length; p++) heap[n[p]] = i(n[p]) + }) + heap.load('1919519062') +}