aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/analytics.ts
blob: d790ec1e74e8470c6b2145442d81f0f99bcdec6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { BasicOrderProvider, StandardRelayerAPIOrderProvider } from '@0x/asset-buyer';
import { ObjectMap } from '@0x/types';

import { State } from '../redux/reducer';

import { heapUtil } from './heap';

let isDisabled = false;
export const disableAnalytics = (shouldDisableAnalytics: boolean) => {
    isDisabled = shouldDisableAnalytics;
};
export const evaluateIfEnabled = (fnCall: () => void) => {
    if (isDisabled) {
        return;
    }
    fnCall();
};

enum EventNames {
    INSTANT_OPENED = 'Instant - Opened',
    WALLET_READY = 'Wallet - Ready',
}
const track = (eventName: EventNames, eventData: ObjectMap<string | number> = {}): void => {
    evaluateIfEnabled(() => {
        heapUtil.evaluateHeapCall(heap => heap.track(eventName, eventData));
    });
};
function trackingEventFnWithoutPayload(eventName: EventNames): () => void {
    return () => {
        track(eventName);
    };
}
// tslint:disable-next-line:no-unused-variable
function trackingEventFnWithPayload<T extends ObjectMap<string | number>>(
    eventName: EventNames,
): (eventDataProperties: T) => void {
    return (eventDataProperties: T) => {
        track(eventName, eventDataProperties);
    };
}

export interface AnalyticsUserOptions {
    ethAddress?: string;
    ethBalanceInUnitAmount?: string;
}
export interface AnalyticsEventOptions {
    embeddedHost?: string;
    embeddedUrl?: string;
    networkId?: number;
    providerName?: string;
    gitSha?: string;
    npmVersion?: string;
    orderSource?: string;
}
export const analytics = {
    addUserProperties: (properties: AnalyticsUserOptions): void => {
        evaluateIfEnabled(() => {
            heapUtil.evaluateHeapCall(heap => heap.addUserProperties(properties));
        });
    },
    addEventProperties: (properties: AnalyticsEventOptions): void => {
        evaluateIfEnabled(() => {
            heapUtil.evaluateHeapCall(heap => heap.addEventProperties(properties));
        });
    },
    generateEventProperties: (state: State, window: Window): AnalyticsEventOptions => {
        let orderSource = 'unknown';
        const orderProvider = state.providerState.assetBuyer.orderProvider;
        if (orderProvider instanceof StandardRelayerAPIOrderProvider) {
            orderSource = orderProvider.apiUrl;
        } else if (orderProvider instanceof BasicOrderProvider) {
            orderSource = 'provided';
        }

        return {
            embeddedHost: window.location.host,
            embeddedUrl: window.location.href,
            networkId: state.network,
            providerName: state.providerState.name,
            gitSha: process.env.GIT_SHA,
            npmVersion: process.env.NPM_PACKAGE_VERSION,
            orderSource,
        };
    },
    trackWalletReady: trackingEventFnWithoutPayload(EventNames.WALLET_READY),
    trackInstantOpened: trackingEventFnWithoutPayload(EventNames.INSTANT_OPENED),
};