aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/utils/analytics.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-07-17 18:06:35 +0800
committerFabio Berger <me@fabioberger.com>2018-07-17 18:06:35 +0800
commit03a6a088c5f1a086c1f3f02a0a70edaea3603860 (patch)
treebbe67afd2ad3d902444da65964c7401c9d38d63d /packages/website/ts/utils/analytics.ts
parent1e787a764615bd3df839f2cb117b711be297b9d5 (diff)
parenta9038f2afc974cecf567a9aef50267d29a995e02 (diff)
downloaddexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar.gz
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar.bz2
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar.lz
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar.xz
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.tar.zst
dexon-sol-tools-03a6a088c5f1a086c1f3f02a0a70edaea3603860.zip
Merge branch 'v2-prototype' into fix-order-watcher
* v2-prototype: (39 commits) Add chris to website Fix ocean link Move update onboarding step tracking to onboarding flow code Bump npm-run-all from 4.1.2 to 4.1.3 Move format to helper function Fix linter Add assertion to make sure caller to fetchAsync isn't trying to set timeout in a context-specific way Fix linter issues Remove unused import Switch conditional Update yarn.lock and artifact Fix abi-gen tests to not rely on sleep, since it causes intermittent failures Add missing assertion Move type defs to typescript-typingsd Fix linter Make createFinalPayload protected Replace process.browser with detect-node library Export Web3ProviderEngine and RPCSubprovider from 0x.js Export Web3ProviderEngine from subproviders package Update deps ...
Diffstat (limited to 'packages/website/ts/utils/analytics.ts')
-rw-r--r--packages/website/ts/utils/analytics.ts104
1 files changed, 80 insertions, 24 deletions
diff --git a/packages/website/ts/utils/analytics.ts b/packages/website/ts/utils/analytics.ts
index f4bfa083f..e5a1ddfa4 100644
--- a/packages/website/ts/utils/analytics.ts
+++ b/packages/website/ts/utils/analytics.ts
@@ -1,27 +1,83 @@
import * as _ from 'lodash';
-import * as ReactGA from 'react-ga';
-import { InjectedWeb3 } from 'ts/types';
-import { configs } from 'ts/utils/configs';
+import { ObjectMap, Order } from 'ts/types';
import { utils } from 'ts/utils/utils';
-export const analytics = {
- init(): void {
- ReactGA.initialize(configs.GOOGLE_ANALYTICS_ID);
- },
- logEvent(category: string, action: string, label: string, value?: any): void {
- ReactGA.event({
- category,
- action,
- label,
- value,
- });
- },
- async logProviderAsync(web3IfExists: InjectedWeb3): Promise<void> {
- await utils.onPageLoadAsync();
- const providerType =
- !_.isUndefined(web3IfExists) && !_.isUndefined(web3IfExists.currentProvider)
- ? utils.getProviderType(web3IfExists.currentProvider)
- : 'NONE';
- ReactGA.ga('set', 'dimension1', providerType);
- },
-};
+export interface HeapAnalytics {
+ loaded: boolean;
+ identify(id: string, idType: string): void;
+ track(eventName: string, eventProperties?: ObjectMap<string | number>): void;
+ resetIdentity(): void;
+ addUserProperties(properties: ObjectMap<string | number>): void;
+ addEventProperties(properties: ObjectMap<string | number>): void;
+ removeEventProperty(property: string): void;
+ clearEventProperties(): void;
+}
+export class Analytics {
+ private _heap: HeapAnalytics;
+ public static init(): Analytics {
+ return new Analytics(Analytics.getHeap());
+ }
+ public static getHeap(): HeapAnalytics {
+ const heap = (window as any).heap;
+ if (!_.isUndefined(heap)) {
+ return heap;
+ } else {
+ throw new Error('Could not find the Heap SDK on the page.');
+ }
+ }
+ constructor(heap: HeapAnalytics) {
+ this._heap = heap;
+ }
+ // tslint:disable:no-floating-promises
+ // HeapAnalytics Wrappers
+ public identify(id: string, idType: string): void {
+ this._heapLoadedGuardAsync(() => this._heap.identify(id, idType));
+ }
+ public track(eventName: string, eventProperties?: ObjectMap<string | number>): void {
+ this._heapLoadedGuardAsync(() => this._heap.track(eventName, eventProperties));
+ }
+ public resetIdentity(): void {
+ this._heapLoadedGuardAsync(() => this._heap.resetIdentity());
+ }
+ public addUserProperties(properties: ObjectMap<string | number>): void {
+ this._heapLoadedGuardAsync(() => this._heap.addUserProperties(properties));
+ }
+ public addEventProperties(properties: ObjectMap<string | number>): void {
+ this._heapLoadedGuardAsync(() => this._heap.addEventProperties(properties));
+ }
+ public removeEventProperty(property: string): void {
+ this._heapLoadedGuardAsync(() => this._heap.removeEventProperty(property));
+ }
+ public clearEventProperties(): void {
+ this._heapLoadedGuardAsync(() => this._heap.clearEventProperties());
+ }
+ // tslint:enable:no-floating-promises
+ // Custom methods
+ public trackOrderEvent(eventName: string, order: Order): void {
+ const orderLoggingData = {
+ takerTokenAmount: order.signedOrder.takerTokenAmount,
+ makeTokenAmount: order.signedOrder.makerTokenAmount,
+ takerToken: order.metadata.takerToken.symbol,
+ makerToken: order.metadata.makerToken.symbol,
+ };
+ this.track(eventName, orderLoggingData);
+ }
+ /**
+ * Heap is not available as a UMD module, and additionally has the strange property of replacing itself with
+ * a different object once it's loaded.
+ * Instead of having an await call before every analytics use, we opt to have the awaiting logic in here by
+ * guarding every API call with the guard below.
+ */
+ private async _heapLoadedGuardAsync(callback: () => void): Promise<void> {
+ if (this._heap.loaded) {
+ callback();
+ return undefined;
+ }
+ await utils.onPageLoadPromise;
+ // HACK: Reset heap to loaded heap
+ this._heap = (window as any).heap;
+ callback();
+ }
+}
+
+export const analytics = Analytics.init();