diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-16 02:43:42 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-16 02:43:42 +0800 |
commit | 61f227e123218ba76a7fdf7fc2ee89171c2bf16c (patch) | |
tree | d821f01c41dd58e03b093a40930748dac51c2148 /packages/instant/src/util | |
parent | 20ed4fbbd46f359ca1436b2d3b9d17527c01df54 (diff) | |
download | dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar.gz dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar.bz2 dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar.lz dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar.xz dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.tar.zst dexon-sol-tools-61f227e123218ba76a7fdf7fc2ee89171c2bf16c.zip |
feat(instant): Heap middleware and first tracking events
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/analytics.ts | 35 | ||||
-rw-r--r-- | packages/instant/src/util/heap.ts | 13 |
2 files changed, 41 insertions, 7 deletions
diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts new file mode 100644 index 000000000..4de3e5eff --- /dev/null +++ b/packages/instant/src/util/analytics.ts @@ -0,0 +1,35 @@ +import { ObjectMap } from '@0x/types'; +import { logUtils } from '@0x/utils'; + +import { HeapAnalytics, heapUtil } from './heap'; + +export class Analytics { + public static init(): Analytics { + return new Analytics(); + } + public track(eventName: string, eventProperties?: ObjectMap<string | number>): void { + console.log('HEAP: tracking', eventName, eventProperties); + this._evaluteHeapCall(heap => heap.track(eventName, eventProperties)); + } + public addUserProperties(properties: ObjectMap<string | number>): void { + console.log('HEAP: adding user properties', properties); + this._evaluteHeapCall(heap => heap.addUserProperties(properties)); + } + public addEventProperties(properties: ObjectMap<string | number>): void { + this._evaluteHeapCall(heap => heap.addEventProperties(properties)); + } + private _evaluteHeapCall(heapFunctionCall: (heap: HeapAnalytics) => void): void { + const curHeap = heapUtil.getHeap(); + if (curHeap) { + try { + heapFunctionCall(curHeap); + } catch (e) { + // We never want analytics to crash our React component + // TODO: error reporter here + logUtils.log('Analytics error', e); + } + } + } +} + +export const analytics = Analytics.init(); diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 399ac3f6f..2f2c221b1 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -20,8 +20,13 @@ interface ModifiedWindow { const getWindow = (): ModifiedWindow => { return window as ModifiedWindow; }; -// Typescript-compatible version of https://docs.heapanalytics.com/docs/installation + const setupZeroExInstantHeap = () => { + const curWindow = getWindow(); + // Set property to specify that this is zeroEx's heap + curWindow.zeroExInstantLoadedHeap = true; + + // Typescript-compatible version of https://docs.heapanalytics.com/docs/installation /* tslint:disable */ ((window as any).heap = (window as any).heap || []), ((window as any).heap.load = function(e: any, t: any) { @@ -60,9 +65,6 @@ const setupZeroExInstantHeap = () => { (window as any).heap.load(HEAP_ANALYTICS_DEVELOPMENT_APP_ID); /* tslint:enable */ - const curWindow = getWindow(); - // Set property to specify that this is zeroEx's heap - curWindow.zeroExInstantLoadedHeap = true; return curWindow.heap as HeapAnalytics; }; @@ -71,17 +73,14 @@ export const heapUtil = { const curWindow = getWindow(); const hasOtherExistingHeapIntegration = curWindow.heap && !curWindow.zeroExInstantLoadedHeap; if (hasOtherExistingHeapIntegration) { - logUtils.log('Heap integration already exists'); return null; } const zeroExInstantHeapIntegration = curWindow.zeroExInstantLoadedHeap && curWindow.heap; if (zeroExInstantHeapIntegration) { - logUtils.log('Using existing 0x instant heap'); return zeroExInstantHeapIntegration; } - logUtils.log('Setting up heap'); return setupZeroExInstantHeap(); }, }; |