Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic web analytics tracking implementation #681

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add unit tests
Signed-off-by: Mykhailo Semenchenko <mykhailo.semenchenko@logz.io>
  • Loading branch information
th3M1ke committed Feb 12, 2021
commit 6e147f7aed49c58a4f24dc444483bd5514b9028f
2 changes: 1 addition & 1 deletion packages/jaeger-ui/src/types/tracking.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Uber Technologies, Inc.
// Copyright (c) 2021 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
27 changes: 18 additions & 9 deletions packages/jaeger-ui/src/utils/tracking/ga.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ jest.mock('./conv-raven-to-ga', () => () => ({
}));

jest.mock('./index', () => {
process.env.REACT_APP_VSN_STATE = '{}';
global.process.env.REACT_APP_VSN_STATE = '{}';
return require.requireActual('./index');
});

jest.mock('../config/get-config', () => () => ({
tracking: {
gaID: 'UA-123456',
trackErrors: true,
},
}));

import ReactGA from 'react-ga';

import * as tracking from './index';
import GA from './ga';

let longStr = '---';
function getStr(len) {
Expand All @@ -45,6 +38,22 @@ function getStr(len) {

describe('google analytics tracking', () => {
let calls;
let tracking;

beforeAll(() => {
tracking = GA(
{
tracking: {
gaID: 'UA-123456',
trackErrors: true,
},
},
'unknown',
'unknown'
);

tracking.init();
});

beforeEach(() => {
calls = ReactGA.testModeAPI.calls;
Expand Down
147 changes: 147 additions & 0 deletions packages/jaeger-ui/src/utils/tracking/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) 2021 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const mockGA = {
init: jest.fn(),
context: jest.fn(),
isEnabled: jest.fn(),
};

const mockNoopWebAnalytics = {
init: jest.fn(),
context: jest.fn(),
isEnabled: jest.fn(),
};

jest.mock('./ga', () => ({
__esModule: true,
default: () => {
return mockGA;
},
}));
let internalVersionShort;
let internalVersionLong;

jest.mock('./noopWebAnalytics', () => ({
__esModule: true,
default: (config, versionShort, versionLong) => {
internalVersionShort = versionShort;
internalVersionLong = versionLong;
return mockNoopWebAnalytics;
},
}));

describe('generic analytics tracking', () => {
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();
});

it('no web analytic test', () => {
jest.doMock('../config/get-config', () => {
return {
__esModule: true,
default: () => ({}),
};
});

return import('.').then(() => {
expect(internalVersionShort).toBe('unknown');
expect(internalVersionLong).toBe('unknown');
expect(mockNoopWebAnalytics.init).toHaveBeenCalled();
expect(mockGA.init).not.toHaveBeenCalled();
});
});

it('Google Analytics test', () => {
jest.doMock('../config/get-config', () => {
return {
__esModule: true,
default: () => ({
tracking: {
gaID: 'UA123',
},
}),
};
});

return import('.').then(() => {
expect(mockNoopWebAnalytics.init).not.toHaveBeenCalled();
expect(mockGA.init).toHaveBeenCalled();
});
});

it('Custom Web Analytics test', () => {
const mockCustomWA = {
init: jest.fn(),
context: jest.fn(),
isEnabled: jest.fn(),
};

jest.doMock('../config/get-config', () => {
return {
__esModule: true,
default: () => ({
tracking: {
gaID: 'UA123',
customWebAnalytics: () => mockCustomWA,
},
}),
};
});

return import('.').then(() => {
expect(mockNoopWebAnalytics.init).not.toHaveBeenCalled();
expect(mockGA.init).not.toHaveBeenCalled();
expect(mockCustomWA.init).toHaveBeenCalled();
});
});

it('get versions as a string or bad JSON test', () => {
const version = '123456';
process.env.REACT_APP_VSN_STATE = version;
jest.doMock('../config/get-config', () => {
return {
__esModule: true,
default: () => ({}),
};
});

return import('.').then(() => {
expect(internalVersionShort).toBe(version);
expect(internalVersionLong).toBe(version);
expect(mockNoopWebAnalytics.init).toHaveBeenCalled();
expect(mockGA.init).not.toHaveBeenCalled();
});
});

it('get versions as an object test', () => {
const vShot = '48956d5';
const vLong = ' | github.com/jaegertracing/jaeger-ui | 48956d5 | master';
process.env.REACT_APP_VSN_STATE = `{"remote":"github.com/jaegertracing/jaeger-ui","objName":"${vShot}","changed":{"hasChanged":false,"files":0,"insertions":0,"deletions":0,"untracked":0,"pretty":""},"refName":"master","pretty":"${vLong}"}`;
jest.doMock('../config/get-config', () => {
return {
__esModule: true,
default: () => ({}),
};
});

return import('.').then(() => {
expect(internalVersionShort).toBe(vShot);
expect(internalVersionLong).toBe(vLong);
expect(mockNoopWebAnalytics.init).toHaveBeenCalled();
expect(mockGA.init).not.toHaveBeenCalled();
});
});
});
10 changes: 1 addition & 9 deletions packages/jaeger-ui/src/utils/tracking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { TNil } from '../../types';
import { IWebAnalyticsFunc } from '../../types/tracking';
import GA from './ga';
import NoopWebAnalytics from './noopWebAnalytics';
import getConfig from '../config/get-config';

const TrackingImplementation = () => {
Expand All @@ -41,15 +42,6 @@ const TrackingImplementation = () => {
versionLong = 'unknown';
}

const NoopWebAnalytics: IWebAnalyticsFunc = () => ({
init: () => {},
trackPageView: () => {},
trackError: () => {},
trackEvent: () => {},
context: null,
isEnabled: () => false,
});

let webAnalyticsFunc = NoopWebAnalytics;

if (config.tracking && config.tracking.customWebAnalytics) {
Expand Down
26 changes: 26 additions & 0 deletions packages/jaeger-ui/src/utils/tracking/noopWebAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { IWebAnalyticsFunc } from '../../types/tracking';

const NoopWebAnalytics: IWebAnalyticsFunc = () => ({
init: () => {},
trackPageView: () => {},
trackError: () => {},
trackEvent: () => {},
context: null,
isEnabled: () => false,
});

export default NoopWebAnalytics;