Merge pull request #1106 from balena-io/roman/analytics-fix

event-tracker: Use std mixpanel types
This commit is contained in:
Roman Mazur 2019-11-06 11:25:54 +02:00 committed by GitHub
commit f8cfdebba8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 13 deletions

View File

@ -2,8 +2,7 @@ import * as Bluebird from 'bluebird';
import mask = require('json-mask');
import * as _ from 'lodash';
import * as memoizee from 'memoizee';
import Mixpanel = require('mixpanel');
import * as mixpanel from 'mixpanel';
import { ConfigType } from './config';
import log from './lib/supervisor-console';
@ -35,7 +34,7 @@ const mixpanelMask = [
export class EventTracker {
private defaultProperties: EventTrackProperties | null;
private client: any;
private client: mixpanel.Mixpanel | null;
public constructor() {
this.client = null;
@ -54,10 +53,10 @@ export class EventTracker {
uuid,
supervisorVersion,
};
if (unmanaged || mixpanelHost == null) {
if (unmanaged || mixpanelHost == null || mixpanelToken == null) {
return;
}
this.client = Mixpanel.init(mixpanelToken, {
this.client = mixpanel.init(mixpanelToken, {
host: mixpanelHost.host,
path: mixpanelHost.path,
});
@ -94,7 +93,9 @@ export class EventTracker {
// Call this function at maximum once every minute
return _.throttle(
properties => {
this.client.track(event, properties);
if (this.client != null) {
this.client.track(event, properties);
}
},
eventDebounceTime,
{ leading: true },

View File

@ -1,5 +1,6 @@
import { Mixpanel } from 'mixpanel';
import * as mixpanel from 'mixpanel';
import { stub } from 'sinon';
import { SinonStub, stub } from 'sinon';
import { expect } from './lib/chai-config';
@ -9,12 +10,16 @@ import supervisorVersion = require('../src/lib/supervisor-version');
describe('EventTracker', () => {
let eventTrackerOffline: EventTracker;
let eventTracker: EventTracker;
let initStub: SinonStub;
before(() => {
stub(mixpanel, 'init').callsFake(token => ({
token,
track: stub().returns(undefined),
}));
initStub = stub(mixpanel, 'init').callsFake(
token =>
(({
token,
track: stub().returns(undefined),
} as unknown) as Mixpanel),
);
eventTrackerOffline = new EventTracker();
eventTracker = new EventTracker();
@ -23,7 +28,7 @@ describe('EventTracker', () => {
after(() => {
(EventTracker.prototype as any).logEvent.restore();
return mixpanel.init.restore();
return initStub.restore();
});
it('initializes in unmanaged mode', () => {

View File

@ -1 +0,0 @@
declare module 'mixpanel';