mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
Google analytics tests
This commit is contained in:
parent
92ac295227
commit
494ce4b364
52
app/analytics/__tests__/index.test.js
Normal file
52
app/analytics/__tests__/index.test.js
Normal file
@ -0,0 +1,52 @@
|
||||
import * as analytics from '../index';
|
||||
import {GA_HOST} from '../../common/constants';
|
||||
|
||||
global.document = {
|
||||
getElementsByTagName () {
|
||||
return {
|
||||
parentNode: {
|
||||
insertBefore() {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
describe('init()', () => {
|
||||
it('correctly initializes', async () => {
|
||||
window.localStorage = {};
|
||||
|
||||
analytics.trackEvent('premature', 'event');
|
||||
analytics.setAccountId('acct_premature');
|
||||
|
||||
window.ga = jest.genMockFunction();
|
||||
analytics.initAnalytics('acct_123');
|
||||
|
||||
// Verify that Google Analytics works
|
||||
expect(window.ga.mock.calls.length).toBe(5);
|
||||
expect(window.ga.mock.calls[0]).toEqual(['create', 'UA-86416787-1', {
|
||||
clientId: 'dd2ccc1a-2745-477a-881a-9e8ef9d42403',
|
||||
storage: 'none'
|
||||
}]);
|
||||
expect(window.ga.mock.calls[1].slice(0, 2)).toEqual(['set', 'checkProtocolTask']);
|
||||
expect(window.ga.mock.calls[1][2]()).toBeNull();
|
||||
expect(window.ga.mock.calls[2]).toEqual(['set', 'location', `https://${GA_HOST}/`]);
|
||||
expect(window.ga.mock.calls[3]).toEqual(['send', 'pageview']);
|
||||
expect(window.ga.mock.calls[4]).toEqual(['set', 'userId', 'acct_123']);
|
||||
|
||||
analytics.trackEvent('foo', 'bar', 'baz');
|
||||
expect(window.ga.mock.calls.length).toBe(6);
|
||||
expect(window.ga.mock.calls[5]).toEqual(['send', 'event', 'foo', 'bar', 'baz']);
|
||||
|
||||
analytics.setAccountId('acct_456');
|
||||
expect(window.ga.mock.calls.length).toBe(7);
|
||||
expect(window.ga.mock.calls[6]).toEqual(['set', 'userId', 'acct_456']);
|
||||
|
||||
// Try reinitializing
|
||||
analytics.initAnalytics();
|
||||
expect(window.ga.mock.calls.length).toBe(7);
|
||||
|
||||
// TODO: Verify that Segment works (although it's not that important)
|
||||
});
|
||||
});
|
@ -12,8 +12,8 @@ export function init (accountId = null) {
|
||||
_injectGoogleAnalyticsScript();
|
||||
}
|
||||
|
||||
if (!localStorage['gaClientId']) {
|
||||
localStorage.setItem('gaClientId', require('node-uuid').v4());
|
||||
if (!window.localStorage['gaClientId']) {
|
||||
window.localStorage['gaClientId'] = require('node-uuid').v4();
|
||||
}
|
||||
|
||||
const _sessionId = window.localStorage['gaClientId'];
|
||||
@ -44,23 +44,23 @@ export function setAccountId (accountId) {
|
||||
}
|
||||
|
||||
export function trackEvent (...googleAnalyticsArgs) {
|
||||
if (!window.ga) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.ga('send', 'event', ...googleAnalyticsArgs);
|
||||
window.ga && window.ga('send', 'event', ...googleAnalyticsArgs);
|
||||
}
|
||||
|
||||
function _injectGoogleAnalyticsScript () {
|
||||
try {
|
||||
(function (i, s, o, g, r, a, m) {
|
||||
i['GoogleAnalyticsObject'] = r;
|
||||
i[r] = i[r] || function () {
|
||||
(i[r].q = i[r].q || []).push(arguments)
|
||||
}, i[r].l = 1 * new Date();
|
||||
a = s.createElement(o),
|
||||
a = s.createElement(o);
|
||||
m = s.getElementsByTagName(o)[0];
|
||||
a.async = 1;
|
||||
a.src = g;
|
||||
m.parentNode.insertBefore(a, m)
|
||||
})(window, document, 'script', 'https://www.google-analytics.com/segment.js', 'ga');
|
||||
} catch (e) {
|
||||
console.warn('-- Failed to inject Google Analytics --')
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,16 @@
|
||||
import * as segment from './segment';
|
||||
import * as google from './google';
|
||||
|
||||
let initialized = false;
|
||||
export function initAnalytics(accountId) {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
segment.init();
|
||||
google.init(accountId);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
export function trackEvent (...args) {
|
||||
|
@ -12,7 +12,7 @@ describe('LocalStorage()', () => {
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllTimers();
|
||||
})
|
||||
});
|
||||
|
||||
it('create directory', () => {
|
||||
const basePath = `/tmp/insomnia-localstorage-${Math.random()}`;
|
||||
|
Loading…
Reference in New Issue
Block a user