From 20ed4fbbd46f359ca1436b2d3b9d17527c01df54 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 08:19:58 -0800 Subject: First pass on widget version of heap --- packages/instant/src/util/heap.ts | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 packages/instant/src/util/heap.ts (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts new file mode 100644 index 000000000..399ac3f6f --- /dev/null +++ b/packages/instant/src/util/heap.ts @@ -0,0 +1,87 @@ +import { ObjectMap } from '@0x/types'; +import { logUtils } from '@0x/utils'; + +import { HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; + +export interface HeapAnalytics { + loaded: boolean; + identify(id: string, idType: string): void; + track(eventName: string, eventProperties?: ObjectMap): void; + resetIdentity(): void; + addUserProperties(properties: ObjectMap): void; + addEventProperties(properties: ObjectMap): void; + removeEventProperty(property: string): void; + clearEventProperties(): void; +} +interface ModifiedWindow { + heap?: HeapAnalytics; + zeroExInstantLoadedHeap?: boolean; +} +const getWindow = (): ModifiedWindow => { + return window as ModifiedWindow; +}; +// Typescript-compatible version of https://docs.heapanalytics.com/docs/installation +const setupZeroExInstantHeap = () => { + /* tslint:disable */ + ((window as any).heap = (window as any).heap || []), + ((window as any).heap.load = function(e: any, t: any) { + ((window as any).heap.appid = e), ((window as any).heap.config = t = t || {}); + var r = t.forceSSL || 'https:' === (document.location as Location).protocol, + a = document.createElement('script'); + (a.type = 'text/javascript'), + (a.async = !0), + (a.src = (r ? 'https:' : 'http:') + '//cdn.heapanalytics.com/js/heap-' + e + '.js'); + var n = document.getElementsByTagName('script')[0]; + (n.parentNode as Node).insertBefore(a, n); + for ( + var o = function(e: any) { + return function() { + (window as any).heap.push([e].concat(Array.prototype.slice.call(arguments, 0))); + }; + }, + p = [ + 'addEventProperties', + 'addUserProperties', + 'clearEventProperties', + 'identify', + 'resetIdentity', + 'removeEventProperty', + 'setEventProperties', + 'track', + 'unsetEventProperty', + ], + c = 0; + c < p.length; + c++ + ) + (window as any).heap[p[c]] = o(p[c]); + }); + // TODO: use production heap id once environment utils merged + (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; +}; + +export const heapUtil = { + getHeap: (): HeapAnalytics | null => { + 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(); + }, +}; -- cgit v1.2.3 From 61f227e123218ba76a7fdf7fc2ee89171c2bf16c Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 10:43:42 -0800 Subject: feat(instant): Heap middleware and first tracking events --- packages/instant/src/util/analytics.ts | 35 ++++++++++++++++++++++++++++++++++ packages/instant/src/util/heap.ts | 13 ++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 packages/instant/src/util/analytics.ts (limited to 'packages/instant/src/util') 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): void { + console.log('HEAP: tracking', eventName, eventProperties); + this._evaluteHeapCall(heap => heap.track(eventName, eventProperties)); + } + public addUserProperties(properties: ObjectMap): void { + console.log('HEAP: adding user properties', properties); + this._evaluteHeapCall(heap => heap.addUserProperties(properties)); + } + public addEventProperties(properties: ObjectMap): 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(); }, }; -- cgit v1.2.3 From 2e61050a223a52b4b984034463887b0d579ce9b8 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 10:48:20 -0800 Subject: Use pure functions instead of class --- packages/instant/src/util/analytics.ts | 51 ++++++++++++++++------------------ 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 4de3e5eff..3e2a996cf 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -3,33 +3,30 @@ 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): void { - console.log('HEAP: tracking', eventName, eventProperties); - this._evaluteHeapCall(heap => heap.track(eventName, eventProperties)); - } - public addUserProperties(properties: ObjectMap): void { - console.log('HEAP: adding user properties', properties); - this._evaluteHeapCall(heap => heap.addUserProperties(properties)); - } - public addEventProperties(properties: ObjectMap): 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); - } +const 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(); +export const analytics = { + addUserProperties: (properties: ObjectMap): void => { + console.log('HEAP: adding user properties', properties); + evaluteHeapCall(heap => heap.addUserProperties(properties)); + }, + addEventProperties: (properties: ObjectMap): void => { + console.log('HEAP: adding user properties', properties); + evaluteHeapCall(heap => heap.addEventProperties(properties)); + }, + track: (eventName: string, eventProperties?: ObjectMap): void => { + console.log('HEAP: tracking', eventName, eventProperties); + evaluteHeapCall(heap => heap.track(eventName, eventProperties)); + }, +}; -- cgit v1.2.3 From 450814ad80bb22332579e8ba54b58e1c06f34f71 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 11:28:44 -0800 Subject: Introduce ANALYTICS_ENABLED constant --- packages/instant/src/util/analytics.ts | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 3e2a996cf..6b6115abe 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -1,9 +1,15 @@ import { ObjectMap } from '@0x/types'; import { logUtils } from '@0x/utils'; +import { ANALYTICS_ENABLED } from '../constants'; + import { HeapAnalytics, heapUtil } from './heap'; const evaluteHeapCall = (heapFunctionCall: (heap: HeapAnalytics) => void): void => { + if (!ANALYTICS_ENABLED) { + return; + } + const curHeap = heapUtil.getHeap(); if (curHeap) { try { -- cgit v1.2.3 From a8863ac85e49f11b108fe17186728e335a48ec3c Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 11:33:04 -0800 Subject: Take out console.logs --- packages/instant/src/util/analytics.ts | 3 --- 1 file changed, 3 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 6b6115abe..dd595529d 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -24,15 +24,12 @@ const evaluteHeapCall = (heapFunctionCall: (heap: HeapAnalytics) => void): void export const analytics = { addUserProperties: (properties: ObjectMap): void => { - console.log('HEAP: adding user properties', properties); evaluteHeapCall(heap => heap.addUserProperties(properties)); }, addEventProperties: (properties: ObjectMap): void => { - console.log('HEAP: adding user properties', properties); evaluteHeapCall(heap => heap.addEventProperties(properties)); }, track: (eventName: string, eventProperties?: ObjectMap): void => { - console.log('HEAP: tracking', eventName, eventProperties); evaluteHeapCall(heap => heap.track(eventName, eventProperties)); }, }; -- cgit v1.2.3 From 71aeb7cddcd2a1faf7a4dc46d828ad8471019f37 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 15 Nov 2018 11:35:47 -0800 Subject: Linting --- packages/instant/src/util/heap.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 2f2c221b1..6d3c75ea7 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -1,5 +1,4 @@ import { ObjectMap } from '@0x/types'; -import { logUtils } from '@0x/utils'; import { HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; -- cgit v1.2.3 From 37d60dc39ea6476c3185e124175cb02d5e830250 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 08:51:41 -0800 Subject: Typesafe analytic actions --- packages/instant/src/util/analytics.ts | 50 ++++++++++++++++++---------------- packages/instant/src/util/heap.ts | 19 ++++++++++++- 2 files changed, 44 insertions(+), 25 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index dd595529d..30193839e 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -1,35 +1,37 @@ import { ObjectMap } from '@0x/types'; -import { logUtils } from '@0x/utils'; -import { ANALYTICS_ENABLED } from '../constants'; +import { heapUtil } from './heap'; -import { HeapAnalytics, heapUtil } from './heap'; - -const evaluteHeapCall = (heapFunctionCall: (heap: HeapAnalytics) => void): void => { - if (!ANALYTICS_ENABLED) { - return; - } - - 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); - } - } +enum EventNames { + WALLET_OPENED = 'Wallet - Opened', + WALLET_READY = 'Wallet - Ready', + WIDGET_OPENED = 'Widget - Opened', +} +const track = (eventName: EventNames, eventData: ObjectMap = {}): void => { + heapUtil.evaluateHeapCall(heap => heap.track(eventName, eventData)); }; +function trackingEventFnWithoutPayload(eventName: EventNames): () => void { + return () => { + track(eventName); + }; +} +function trackingEventFnWithPayload>( + eventName: EventNames, +): (eventDataProperties: T) => void { + return (eventDataProperties: T) => { + track(eventName, eventDataProperties); + }; +} export const analytics = { + // TODO(sk): make these more specific addUserProperties: (properties: ObjectMap): void => { - evaluteHeapCall(heap => heap.addUserProperties(properties)); + heapUtil.evaluateHeapCall(heap => heap.addUserProperties(properties)); }, addEventProperties: (properties: ObjectMap): void => { - evaluteHeapCall(heap => heap.addEventProperties(properties)); - }, - track: (eventName: string, eventProperties?: ObjectMap): void => { - evaluteHeapCall(heap => heap.track(eventName, eventProperties)); + heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); }, + walletOpened: trackingEventFnWithoutPayload(EventNames.WALLET_OPENED), + walletReady: trackingEventFnWithPayload<{ numAssetsAvailable: number }>(EventNames.WALLET_READY), + widgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), }; diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 6d3c75ea7..5fd61b4c9 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -1,6 +1,7 @@ import { ObjectMap } from '@0x/types'; +import { logUtils } from '@0x/utils'; -import { HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; +import { ANALYTICS_ENABLED, HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; export interface HeapAnalytics { loaded: boolean; @@ -82,4 +83,20 @@ export const heapUtil = { return setupZeroExInstantHeap(); }, + evaluateHeapCall: (heapFunctionCall: (heap: HeapAnalytics) => void): void => { + if (!ANALYTICS_ENABLED) { + return; + } + + 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); + } + } + }, }; -- cgit v1.2.3 From df71dba8edcba5ca5731bed969748bdcc73efe92 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 09:16:20 -0800 Subject: Make user and event properties more specific --- packages/instant/src/util/analytics.ts | 15 ++++++++++++--- packages/instant/src/util/heap.ts | 6 ++++-- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 30193839e..92f717f7b 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -23,12 +23,21 @@ function trackingEventFnWithPayload>( }; } +export interface AnalyticsUserOptions { + ethAddress?: string; + ethBalanceInUnitAmount?: string; +} +export interface AnalyticsEventOptions { + embeddedHost?: string; + embeddedUrl?: string; + networkId: number; + providerName: string; +} export const analytics = { - // TODO(sk): make these more specific - addUserProperties: (properties: ObjectMap): void => { + addUserProperties: (properties: AnalyticsUserOptions): void => { heapUtil.evaluateHeapCall(heap => heap.addUserProperties(properties)); }, - addEventProperties: (properties: ObjectMap): void => { + addEventProperties: (properties: AnalyticsEventOptions): void => { heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); }, walletOpened: trackingEventFnWithoutPayload(EventNames.WALLET_OPENED), diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 5fd61b4c9..e697562e4 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -3,13 +3,15 @@ import { logUtils } from '@0x/utils'; import { ANALYTICS_ENABLED, HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; +import { AnalyticsEventOptions, AnalyticsUserOptions } from './analytics'; + export interface HeapAnalytics { loaded: boolean; identify(id: string, idType: string): void; track(eventName: string, eventProperties?: ObjectMap): void; resetIdentity(): void; - addUserProperties(properties: ObjectMap): void; - addEventProperties(properties: ObjectMap): void; + addUserProperties(properties: AnalyticsUserOptions): void; + addEventProperties(properties: AnalyticsEventOptions): void; removeEventProperty(property: string): void; clearEventProperties(): void; } -- cgit v1.2.3 From 3add465edb779587e2b4bb136d8668b00b58a8fb Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 09:20:44 -0800 Subject: We may not know num available assets available when wallet ready, so not trackin there --- packages/instant/src/util/analytics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 92f717f7b..ab8a86f04 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -41,6 +41,6 @@ export const analytics = { heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); }, walletOpened: trackingEventFnWithoutPayload(EventNames.WALLET_OPENED), - walletReady: trackingEventFnWithPayload<{ numAssetsAvailable: number }>(EventNames.WALLET_READY), + walletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), widgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), }; -- cgit v1.2.3 From db7f74f99f7790297e737165a0fc9742fe3daf06 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:10:55 -0800 Subject: Switch heap id on environment, and make sure app id is what we expect --- packages/instant/src/util/heap.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index e697562e4..88f65c1ab 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -1,12 +1,13 @@ import { ObjectMap } from '@0x/types'; import { logUtils } from '@0x/utils'; -import { ANALYTICS_ENABLED, HEAP_ANALYTICS_DEVELOPMENT_APP_ID } from '../constants'; +import { ANALYTICS_ENABLED, HEAP_ANALYTICS_DEVELOPMENT_APP_ID, HEAP_ANALYTICS_PRODUCTION_APP_ID } from '../constants'; import { AnalyticsEventOptions, AnalyticsUserOptions } from './analytics'; export interface HeapAnalytics { loaded: boolean; + appid: string; identify(id: string, idType: string): void; track(eventName: string, eventProperties?: ObjectMap): void; resetIdentity(): void; @@ -23,6 +24,13 @@ const getWindow = (): ModifiedWindow => { return window as ModifiedWindow; }; +const getHeapAppId = (): string => { + if (process.env.NODE_ENV === 'production') { + return HEAP_ANALYTICS_PRODUCTION_APP_ID; + } + return HEAP_ANALYTICS_DEVELOPMENT_APP_ID; +}; + const setupZeroExInstantHeap = () => { const curWindow = getWindow(); // Set property to specify that this is zeroEx's heap @@ -64,7 +72,7 @@ const setupZeroExInstantHeap = () => { (window as any).heap[p[c]] = o(p[c]); }); // TODO: use production heap id once environment utils merged - (window as any).heap.load(HEAP_ANALYTICS_DEVELOPMENT_APP_ID); + (window as any).heap.load(getHeapAppId()); /* tslint:enable */ return curWindow.heap as HeapAnalytics; @@ -93,6 +101,10 @@ export const heapUtil = { const curHeap = heapUtil.getHeap(); if (curHeap) { try { + if (curHeap.appid !== getHeapAppId()) { + // Integrator has included heap after us and reset the app id + return; + } heapFunctionCall(curHeap); } catch (e) { // We never want analytics to crash our React component -- cgit v1.2.3 From ed62271cdab1157313f9dfe82fb4640291eeb757 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:13:32 -0800 Subject: Take out old TODO --- packages/instant/src/util/heap.ts | 1 - 1 file changed, 1 deletion(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 88f65c1ab..44af5a9b5 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -71,7 +71,6 @@ const setupZeroExInstantHeap = () => { ) (window as any).heap[p[c]] = o(p[c]); }); - // TODO: use production heap id once environment utils merged (window as any).heap.load(getHeapAppId()); /* tslint:enable */ -- cgit v1.2.3 From 988bb398bcab7153d662d301c5ddac929110e014 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:14:00 -0800 Subject: Add initials to TODO note --- packages/instant/src/util/heap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 44af5a9b5..8e9feb2fa 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -107,7 +107,7 @@ export const heapUtil = { heapFunctionCall(curHeap); } catch (e) { // We never want analytics to crash our React component - // TODO: error reporter here + // TODO(sk): error reporter here logUtils.log('Analytics error', e); } } -- cgit v1.2.3 From 85a99203d0e85698aaaee25cdbf516175f1cb6e0 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:14:42 -0800 Subject: null -> undefined --- packages/instant/src/util/heap.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 8e9feb2fa..1871c4abc 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -78,11 +78,11 @@ const setupZeroExInstantHeap = () => { }; export const heapUtil = { - getHeap: (): HeapAnalytics | null => { + getHeap: (): HeapAnalytics | undefined => { const curWindow = getWindow(); const hasOtherExistingHeapIntegration = curWindow.heap && !curWindow.zeroExInstantLoadedHeap; if (hasOtherExistingHeapIntegration) { - return null; + return undefined; } const zeroExInstantHeapIntegration = curWindow.zeroExInstantLoadedHeap && curWindow.heap; -- cgit v1.2.3 From 42565869a452fcf2f498a3ff396f76c43aadfa29 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:19:16 -0800 Subject: Report on git sha and npm version of build --- packages/instant/src/util/analytics.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index ab8a86f04..e7a67bee5 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -30,8 +30,10 @@ export interface AnalyticsUserOptions { export interface AnalyticsEventOptions { embeddedHost?: string; embeddedUrl?: string; - networkId: number; - providerName: string; + networkId?: number; + providerName?: string; + gitSha?: string; + npmVersion?: string; } export const analytics = { addUserProperties: (properties: AnalyticsUserOptions): void => { -- cgit v1.2.3 From cbcb954c3015b7044e0c1f1b594103df8ea4dfa7 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 10:21:51 -0800 Subject: Disable tslint for unused function so we can include this for future tracking events --- packages/instant/src/util/analytics.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index e7a67bee5..e5f3635f2 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -15,6 +15,7 @@ function trackingEventFnWithoutPayload(eventName: EventNames): () => void { track(eventName); }; } +// tslint:disable-next-line:no-unused-variable function trackingEventFnWithPayload>( eventName: EventNames, ): (eventDataProperties: T) => void { -- cgit v1.2.3 From 31ffa65f59fc6d2c6c7f79c1ac0e2abe0c5ed4ab Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 15:06:11 -0800 Subject: Getting rid of unused function, and using track prefix --- packages/instant/src/util/analytics.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index e5f3635f2..b75665ff5 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -3,7 +3,6 @@ import { ObjectMap } from '@0x/types'; import { heapUtil } from './heap'; enum EventNames { - WALLET_OPENED = 'Wallet - Opened', WALLET_READY = 'Wallet - Ready', WIDGET_OPENED = 'Widget - Opened', } @@ -43,7 +42,6 @@ export const analytics = { addEventProperties: (properties: AnalyticsEventOptions): void => { heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); }, - walletOpened: trackingEventFnWithoutPayload(EventNames.WALLET_OPENED), - walletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), - widgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), + trackWalletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), + trackWidgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), }; -- cgit v1.2.3 From e8be70da10c887956d6e200f7d551d3d7bbafb93 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 15:29:07 -0800 Subject: Widget -> Instant --- packages/instant/src/util/analytics.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index b75665ff5..4c229be03 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -3,6 +3,7 @@ import { ObjectMap } from '@0x/types'; import { heapUtil } from './heap'; enum EventNames { + INSTANT_OPENED = 'Instant - Opened', WALLET_READY = 'Wallet - Ready', WIDGET_OPENED = 'Widget - Opened', } @@ -44,4 +45,5 @@ export const analytics = { }, trackWalletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), trackWidgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), + trackInstantOpened: trackingEventFnWithoutPayload(EventNames.INSTANT_OPENED), }; -- cgit v1.2.3 From 83a6d7b97d9bea901c4d02d1a46186e64e3db367 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 15:29:59 -0800 Subject: Remove old function --- packages/instant/src/util/analytics.ts | 2 -- 1 file changed, 2 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 4c229be03..1667c246c 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -5,7 +5,6 @@ import { heapUtil } from './heap'; enum EventNames { INSTANT_OPENED = 'Instant - Opened', WALLET_READY = 'Wallet - Ready', - WIDGET_OPENED = 'Widget - Opened', } const track = (eventName: EventNames, eventData: ObjectMap = {}): void => { heapUtil.evaluateHeapCall(heap => heap.track(eventName, eventData)); @@ -44,6 +43,5 @@ export const analytics = { heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); }, trackWalletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), - trackWidgetOpened: trackingEventFnWithoutPayload(EventNames.WIDGET_OPENED), trackInstantOpened: trackingEventFnWithoutPayload(EventNames.INSTANT_OPENED), }; -- cgit v1.2.3 From 1564415e5dc29cd76ac17f679ed83e0973764153 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 15:41:54 -0800 Subject: Specify fallback web3 provider in analytics --- packages/instant/src/util/env.ts | 2 ++ packages/instant/src/util/provider_factory.ts | 4 ++++ 2 files changed, 6 insertions(+) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/env.ts b/packages/instant/src/util/env.ts index 4a32f9cb1..59b47d1e3 100644 --- a/packages/instant/src/util/env.ts +++ b/packages/instant/src/util/env.ts @@ -52,6 +52,8 @@ export const envUtil = { return ProviderType.CoinbaseWallet; } else if (!_.isUndefined(_.get(window, '__CIPHER__'))) { return ProviderType.Cipher; + } else if ((provider as any).zeroExInstantFallbackEngine) { + return ProviderType.Fallback; } return; }, diff --git a/packages/instant/src/util/provider_factory.ts b/packages/instant/src/util/provider_factory.ts index 603f7674d..d77407521 100644 --- a/packages/instant/src/util/provider_factory.ts +++ b/packages/instant/src/util/provider_factory.ts @@ -29,6 +29,10 @@ export const providerFactory = { providerEngine.addProvider(new RPCSubprovider(rpcUrl)); // // Start the Provider Engine providerEngine.start(); + // This feels a bit dirty, but was the only way I could think of + // checking to see if this engine is our fallback engine, and not + // another Web3Provider engine provided by some dapp browser + (providerEngine as any).zeroExInstantFallbackEngine = true; return providerEngine; }, }; -- cgit v1.2.3 From 097bfe581dff6ff77e3cf19a03138743ae13868e Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 16:21:38 -0800 Subject: Better way of reporting Fallback provider --- packages/instant/src/util/env.ts | 2 -- packages/instant/src/util/provider_factory.ts | 4 ---- packages/instant/src/util/provider_state_factory.ts | 2 +- 3 files changed, 1 insertion(+), 7 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/env.ts b/packages/instant/src/util/env.ts index 59b47d1e3..4a32f9cb1 100644 --- a/packages/instant/src/util/env.ts +++ b/packages/instant/src/util/env.ts @@ -52,8 +52,6 @@ export const envUtil = { return ProviderType.CoinbaseWallet; } else if (!_.isUndefined(_.get(window, '__CIPHER__'))) { return ProviderType.Cipher; - } else if ((provider as any).zeroExInstantFallbackEngine) { - return ProviderType.Fallback; } return; }, diff --git a/packages/instant/src/util/provider_factory.ts b/packages/instant/src/util/provider_factory.ts index d77407521..603f7674d 100644 --- a/packages/instant/src/util/provider_factory.ts +++ b/packages/instant/src/util/provider_factory.ts @@ -29,10 +29,6 @@ export const providerFactory = { providerEngine.addProvider(new RPCSubprovider(rpcUrl)); // // Start the Provider Engine providerEngine.start(); - // This feels a bit dirty, but was the only way I could think of - // checking to see if this engine is our fallback engine, and not - // another Web3Provider engine provided by some dapp browser - (providerEngine as any).zeroExInstantFallbackEngine = true; return providerEngine; }, }; diff --git a/packages/instant/src/util/provider_state_factory.ts b/packages/instant/src/util/provider_state_factory.ts index 452a71460..7c788dff2 100644 --- a/packages/instant/src/util/provider_state_factory.ts +++ b/packages/instant/src/util/provider_state_factory.ts @@ -56,7 +56,7 @@ export const providerStateFactory = { getInitialProviderStateFallback: (orderSource: OrderSource, network: Network): ProviderState => { const provider = providerFactory.getFallbackNoSigningProvider(network); const providerState: ProviderState = { - name: envUtil.getProviderName(provider), + name: 'Fallback', provider, web3Wrapper: new Web3Wrapper(provider), assetBuyer: assetBuyerFactory.getAssetBuyer(provider, orderSource, network), -- cgit v1.2.3 From 3f0d94c83872ae80d6377fd4f712efa4c174a284 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 16 Nov 2018 16:44:23 -0800 Subject: Add way to disable via props --- packages/instant/src/util/analytics.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 1667c246c..ce40d08e7 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -2,12 +2,25 @@ import { ObjectMap } from '@0x/types'; import { heapUtil } from './heap'; +let disabled = false; +export const disableAnalytics = () => { + disabled = true; +}; +export const evaluateIfEnabled = (fnCall: () => void) => { + if (disabled) { + return; + } + fnCall(); +}; + enum EventNames { INSTANT_OPENED = 'Instant - Opened', WALLET_READY = 'Wallet - Ready', } const track = (eventName: EventNames, eventData: ObjectMap = {}): void => { - heapUtil.evaluateHeapCall(heap => heap.track(eventName, eventData)); + evaluateIfEnabled(() => { + heapUtil.evaluateHeapCall(heap => heap.track(eventName, eventData)); + }); }; function trackingEventFnWithoutPayload(eventName: EventNames): () => void { return () => { @@ -37,10 +50,14 @@ export interface AnalyticsEventOptions { } export const analytics = { addUserProperties: (properties: AnalyticsUserOptions): void => { - heapUtil.evaluateHeapCall(heap => heap.addUserProperties(properties)); + evaluateIfEnabled(() => { + heapUtil.evaluateHeapCall(heap => heap.addUserProperties(properties)); + }); }, addEventProperties: (properties: AnalyticsEventOptions): void => { - heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); + evaluateIfEnabled(() => { + heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties)); + }); }, trackWalletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY), trackInstantOpened: trackingEventFnWithoutPayload(EventNames.INSTANT_OPENED), -- cgit v1.2.3 From 2bfd03e64f20905b0526d65813f78eff3e924727 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 19 Nov 2018 09:54:59 -0800 Subject: Change disabled analytics name, add assertion, and always set --- packages/instant/src/util/analytics.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index ce40d08e7..2ffaac1dd 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -2,12 +2,12 @@ import { ObjectMap } from '@0x/types'; import { heapUtil } from './heap'; -let disabled = false; -export const disableAnalytics = () => { - disabled = true; +let isDisabled = false; +export const disableAnalytics = (shouldDisableAnalytics: boolean) => { + isDisabled = shouldDisableAnalytics; }; export const evaluateIfEnabled = (fnCall: () => void) => { - if (disabled) { + if (isDisabled) { return; } fnCall(); -- cgit v1.2.3 From 8772d916993d754f784d7435dbbfb60c9a6f9205 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 19 Nov 2018 12:02:31 -0800 Subject: Get heap analytics id from ENV variable --- packages/instant/src/util/heap.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/heap.ts b/packages/instant/src/util/heap.ts index 1871c4abc..78ec3b3cc 100644 --- a/packages/instant/src/util/heap.ts +++ b/packages/instant/src/util/heap.ts @@ -1,7 +1,8 @@ import { ObjectMap } from '@0x/types'; import { logUtils } from '@0x/utils'; +import * as _ from 'lodash'; -import { ANALYTICS_ENABLED, HEAP_ANALYTICS_DEVELOPMENT_APP_ID, HEAP_ANALYTICS_PRODUCTION_APP_ID } from '../constants'; +import { HEAP_ANALYTICS_ID } from '../constants'; import { AnalyticsEventOptions, AnalyticsUserOptions } from './analytics'; @@ -24,14 +25,11 @@ const getWindow = (): ModifiedWindow => { return window as ModifiedWindow; }; -const getHeapAppId = (): string => { - if (process.env.NODE_ENV === 'production') { - return HEAP_ANALYTICS_PRODUCTION_APP_ID; +const setupZeroExInstantHeap = () => { + if (_.isUndefined(HEAP_ANALYTICS_ID)) { + return; } - return HEAP_ANALYTICS_DEVELOPMENT_APP_ID; -}; -const setupZeroExInstantHeap = () => { const curWindow = getWindow(); // Set property to specify that this is zeroEx's heap curWindow.zeroExInstantLoadedHeap = true; @@ -71,7 +69,7 @@ const setupZeroExInstantHeap = () => { ) (window as any).heap[p[c]] = o(p[c]); }); - (window as any).heap.load(getHeapAppId()); + (window as any).heap.load(HEAP_ANALYTICS_ID); /* tslint:enable */ return curWindow.heap as HeapAnalytics; @@ -93,14 +91,14 @@ export const heapUtil = { return setupZeroExInstantHeap(); }, evaluateHeapCall: (heapFunctionCall: (heap: HeapAnalytics) => void): void => { - if (!ANALYTICS_ENABLED) { + if (_.isUndefined(HEAP_ANALYTICS_ID)) { return; } const curHeap = heapUtil.getHeap(); if (curHeap) { try { - if (curHeap.appid !== getHeapAppId()) { + if (curHeap.appid !== HEAP_ANALYTICS_ID) { // Integrator has included heap after us and reset the app id return; } -- cgit v1.2.3