2020-04-26 20:33:39 +00:00
|
|
|
import * as electron from 'electron';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { globalBeforeEach } from '../../__jest__/before-each';
|
|
|
|
import * as models from '../../models/index';
|
|
|
|
import { _trackEvent, _trackPageView } from '../analytics';
|
2020-04-27 20:42:57 +00:00
|
|
|
import {
|
|
|
|
getAppPlatform,
|
|
|
|
getAppVersion,
|
|
|
|
getBrowserUserAgent,
|
|
|
|
getAppId,
|
|
|
|
getAppName,
|
|
|
|
getGoogleAnalyticsId,
|
|
|
|
getGoogleAnalyticsLocation,
|
|
|
|
} from '../constants';
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
describe('init()', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await globalBeforeEach();
|
|
|
|
electron.net.request = jest.fn(url => {
|
|
|
|
const req = new EventEmitter();
|
2020-05-14 22:54:07 +00:00
|
|
|
req.end = function() {};
|
2020-04-26 20:33:39 +00:00
|
|
|
return req;
|
|
|
|
});
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not work with tracking disabled', async () => {
|
|
|
|
const settings = await models.settings.getOrCreate({
|
|
|
|
enableAnalytics: false,
|
|
|
|
deviceId: 'device',
|
|
|
|
});
|
|
|
|
expect(settings.enableAnalytics).toBe(false);
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([]);
|
|
|
|
|
|
|
|
await _trackEvent(true, 'Foo', 'Bar');
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works with tracking enabled', async () => {
|
|
|
|
const settings = await models.settings.getOrCreate({
|
|
|
|
enableAnalytics: true,
|
|
|
|
deviceId: 'device',
|
|
|
|
});
|
|
|
|
expect(settings.enableAnalytics).toBe(true);
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([]);
|
|
|
|
|
|
|
|
await _trackEvent(true, 'Foo', 'Bar');
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'https://www.google-analytics.com/collect?' +
|
2020-05-14 22:54:07 +00:00
|
|
|
'v=1&' +
|
|
|
|
`tid=${getGoogleAnalyticsId()}&` +
|
|
|
|
'cid=device&' +
|
|
|
|
`ua=${getBrowserUserAgent()}&` +
|
|
|
|
`dl=${encodeURIComponent(getGoogleAnalyticsLocation())}%2F&` +
|
|
|
|
'sr=1920x1080&' +
|
|
|
|
'ul=en-US&' +
|
|
|
|
`dt=${getAppId()}%3A${getAppVersion()}&` +
|
|
|
|
`cd1=${getAppPlatform()}&` +
|
|
|
|
`cd2=${getAppVersion()}&` +
|
|
|
|
'aip=1&' +
|
|
|
|
`an=${encodeURI(getAppName())}&` +
|
|
|
|
`aid=${getAppId()}&` +
|
|
|
|
`av=${getAppVersion()}&` +
|
|
|
|
'vp=1900x1060&' +
|
|
|
|
'de=UTF-8&' +
|
|
|
|
't=event&' +
|
|
|
|
'ec=Foo&' +
|
|
|
|
'ea=Bar',
|
2020-04-26 20:33:39 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tracks non-interactive event', async () => {
|
|
|
|
await models.settings.getOrCreate({
|
|
|
|
deviceId: 'device',
|
|
|
|
enableAnalytics: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await _trackEvent(false, 'Foo', 'Bar');
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'https://www.google-analytics.com/collect?' +
|
2020-05-14 22:54:07 +00:00
|
|
|
'v=1&' +
|
|
|
|
`tid=${getGoogleAnalyticsId()}&` +
|
|
|
|
'cid=device&' +
|
|
|
|
`ua=${getBrowserUserAgent()}&` +
|
|
|
|
`dl=${encodeURIComponent(getGoogleAnalyticsLocation())}%2F&` +
|
|
|
|
'sr=1920x1080&' +
|
|
|
|
'ul=en-US&' +
|
|
|
|
`dt=${getAppId()}%3A${getAppVersion()}&` +
|
|
|
|
`cd1=${getAppPlatform()}&` +
|
|
|
|
`cd2=${getAppVersion()}&` +
|
|
|
|
'aip=1&' +
|
|
|
|
`an=${encodeURI(getAppName())}&` +
|
|
|
|
`aid=${getAppId()}&` +
|
|
|
|
`av=${getAppVersion()}&` +
|
|
|
|
'vp=1900x1060&' +
|
|
|
|
'de=UTF-8&' +
|
|
|
|
't=event&' +
|
|
|
|
'ec=Foo&' +
|
|
|
|
'ea=Bar&' +
|
|
|
|
'ni=1',
|
2020-04-26 20:33:39 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tracks page view', async () => {
|
|
|
|
await models.settings.getOrCreate({
|
|
|
|
deviceId: 'device',
|
|
|
|
enableAnalytics: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await _trackPageView('/my/path');
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'https://www.google-analytics.com/collect?' +
|
2020-05-14 22:54:07 +00:00
|
|
|
'v=1&' +
|
|
|
|
`tid=${getGoogleAnalyticsId()}&` +
|
|
|
|
'cid=device&' +
|
|
|
|
`ua=${getBrowserUserAgent()}&` +
|
|
|
|
`dl=${encodeURIComponent(getGoogleAnalyticsLocation())}%2Fmy%2Fpath&` +
|
|
|
|
'sr=1920x1080&' +
|
|
|
|
'ul=en-US&' +
|
|
|
|
`dt=${getAppId()}%3A${getAppVersion()}&` +
|
|
|
|
`cd1=${getAppPlatform()}&` +
|
|
|
|
`cd2=${getAppVersion()}&` +
|
|
|
|
'aip=1&' +
|
|
|
|
`an=${encodeURI(getAppName())}&` +
|
|
|
|
`aid=${getAppId()}&` +
|
|
|
|
`av=${getAppVersion()}&` +
|
|
|
|
'vp=1900x1060&' +
|
|
|
|
'de=UTF-8&' +
|
|
|
|
't=pageview',
|
2020-04-26 20:33:39 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('tracking page view remembers path', async () => {
|
|
|
|
await models.settings.getOrCreate({
|
|
|
|
deviceId: 'device',
|
|
|
|
enableAnalytics: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
await _trackPageView('/my/path');
|
|
|
|
jest.runAllTimers();
|
|
|
|
await _trackEvent(true, 'cat', 'act', 'lab', 'val');
|
|
|
|
expect(electron.net.request.mock.calls).toEqual([
|
|
|
|
[
|
|
|
|
'https://www.google-analytics.com/collect?' +
|
2020-05-14 22:54:07 +00:00
|
|
|
'v=1&' +
|
|
|
|
`tid=${getGoogleAnalyticsId()}&` +
|
|
|
|
'cid=device&' +
|
|
|
|
`ua=${getBrowserUserAgent()}&` +
|
|
|
|
`dl=${encodeURIComponent(getGoogleAnalyticsLocation())}%2Fmy%2Fpath&` +
|
|
|
|
'sr=1920x1080&' +
|
|
|
|
'ul=en-US&' +
|
|
|
|
`dt=${getAppId()}%3A${getAppVersion()}&` +
|
|
|
|
`cd1=${getAppPlatform()}&` +
|
|
|
|
`cd2=${getAppVersion()}&` +
|
|
|
|
'aip=1&' +
|
|
|
|
`an=${encodeURI(getAppName())}&` +
|
|
|
|
`aid=${getAppId()}&` +
|
|
|
|
`av=${getAppVersion()}&` +
|
|
|
|
'vp=1900x1060&' +
|
|
|
|
'de=UTF-8&' +
|
|
|
|
't=pageview',
|
2020-04-26 20:33:39 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'https://www.google-analytics.com/collect?' +
|
2020-05-14 22:54:07 +00:00
|
|
|
'v=1&' +
|
|
|
|
`tid=${getGoogleAnalyticsId()}&` +
|
|
|
|
'cid=device&' +
|
|
|
|
`ua=${getBrowserUserAgent()}&` +
|
|
|
|
`dl=${encodeURIComponent(getGoogleAnalyticsLocation())}%2Fmy%2Fpath&` +
|
|
|
|
'sr=1920x1080&' +
|
|
|
|
'ul=en-US&' +
|
|
|
|
`dt=${getAppId()}%3A${getAppVersion()}&` +
|
|
|
|
`cd1=${getAppPlatform()}&` +
|
|
|
|
`cd2=${getAppVersion()}&` +
|
|
|
|
'aip=1&' +
|
|
|
|
`an=${encodeURI(getAppName())}&` +
|
|
|
|
`aid=${getAppId()}&` +
|
|
|
|
`av=${getAppVersion()}&` +
|
|
|
|
'vp=1900x1060&' +
|
|
|
|
'de=UTF-8&' +
|
|
|
|
't=event&' +
|
|
|
|
'ec=cat&' +
|
|
|
|
'ea=act&' +
|
|
|
|
'el=lab&' +
|
|
|
|
'ev=val',
|
2020-04-26 20:33:39 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|