aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/heap.ts
blob: 2f2c221b1944824ac36d4fdc6e77319307464946 (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
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<string | number>): void;
    resetIdentity(): void;
    addUserProperties(properties: ObjectMap<string | number>): void;
    addEventProperties(properties: ObjectMap<string | number>): void;
    removeEventProperty(property: string): void;
    clearEventProperties(): void;
}
interface ModifiedWindow {
    heap?: HeapAnalytics;
    zeroExInstantLoadedHeap?: boolean;
}
const getWindow = (): ModifiedWindow => {
    return window as ModifiedWindow;
};

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) {
            ((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 */

    return curWindow.heap as HeapAnalytics;
};

export const heapUtil = {
    getHeap: (): HeapAnalytics | null => {
        const curWindow = getWindow();
        const hasOtherExistingHeapIntegration = curWindow.heap && !curWindow.zeroExInstantLoadedHeap;
        if (hasOtherExistingHeapIntegration) {
            return null;
        }

        const zeroExInstantHeapIntegration = curWindow.zeroExInstantLoadedHeap && curWindow.heap;
        if (zeroExInstantHeapIntegration) {
            return zeroExInstantHeapIntegration;
        }

        return setupZeroExInstantHeap();
    },
};