aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/analytics.ts
blob: dd595529df7058af058fc039ae32815e34bc452f (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
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 {
            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 = {
    addUserProperties: (properties: ObjectMap<string | number>): void => {
        evaluteHeapCall(heap => heap.addUserProperties(properties));
    },
    addEventProperties: (properties: ObjectMap<string | number>): void => {
        evaluteHeapCall(heap => heap.addEventProperties(properties));
    },
    track: (eventName: string, eventProperties?: ObjectMap<string | number>): void => {
        evaluteHeapCall(heap => heap.track(eventName, eventProperties));
    },
};