Skip to content

Commit

Permalink
FIX (ga): fix ga loading for web and electron
Browse files Browse the repository at this point in the history
  • Loading branch information
sanusart committed Sep 19, 2018
1 parent 5a87676 commit 5ee2be9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 37 deletions.
5 changes: 5 additions & 0 deletions __mocks__/settings.js
@@ -0,0 +1,5 @@
jest.mock('electron-settings', () => ({
get: () => 'red',
set: () => 'blue',
getAll: () => {}
}));
5 changes: 0 additions & 5 deletions __tests__/utils/settings.spec.js
@@ -1,10 +1,5 @@
import { getSetting, setSetting, setBooleanSetting } from 'utils/settings';

jest.mock('electron-settings', ()=> ({
get: () => 'red',
set: () => 'blue'
}));

describe.skip('UTILS - settings', () => {
test('should get setting', () => {
expect(getSetting('color')).toBe('red');
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Expand Up @@ -13,6 +13,7 @@ module.exports = {
],
setupFiles: [
'<rootDir>/test/jest-setup.js',
'<rootDir>/__mocks__/settings.js',
'<rootDir>/__mocks__/electron.js',
'<rootDir>/__mocks__/localStorage.js'
],
Expand Down
1 change: 1 addition & 0 deletions src/components/layout/content/Settings.js
Expand Up @@ -47,6 +47,7 @@ export class Settings extends React.Component {
componentDidMount() {
gaPage('Settings');
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.table(getAllSettings());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/content/Snippet.js
Expand Up @@ -8,14 +8,14 @@ import {

import * as snippetActions from 'actions/snippets';
import { borderColor } from 'constants/colors';
import { SNIPPET_CACHE_SECONDS_DELAY } from 'constants/config';

import Editor from 'components/common/controls/Editor';
import SnippetHeader from 'components/layout/content/snippet/SnippetHeader';

import 'github-markdown-css/github-markdown.css';
import Comments from 'components/layout/content/snippet/Comments';
import { toUnixTimeStamp } from 'utils/date';
import { getSetting } from 'utils/settings';


const SnippetWrapper = styled.div`
Expand All @@ -42,7 +42,7 @@ export class Snippet extends React.Component {
const now = toUnixTimeStamp(new Date());
const viewed = get('viewed', this.props.snippet);

const shouldGetSnippet = isNaN(get('lastModified', this.props.snippet)) || (now - viewed) > SNIPPET_CACHE_SECONDS_DELAY;
const shouldGetSnippet = isNaN(get('lastModified', this.props.snippet)) || (now - viewed) > getSetting('snippet-fetch-cache-in-seconds') || 100;

if (get('files', this.props.snippet) && shouldGetSnippet) {
this.props.getSnippet(this.props.match.params.id || this.props.snippet.id);
Expand Down
9 changes: 3 additions & 6 deletions src/constants/config.js
@@ -1,18 +1,15 @@
import { getSetting } from 'utils/settings';

export const logoText = '{ Gisto }';
export const TAG_REGEX = /#(\d*[A-Za-z_0-9]+\d*)/g;
export const DEFAULT_SNIPPET_DESCRIPTION = 'untitled';
export const MINIMUM_CHARACTERS_TO_TRIGGER_SEARCH = 2;
export const SIDEBAR_WIDTH = 350;
export const SNIPPET_CACHE_SECONDS_DELAY = getSetting('snippet-fetch-cache-in-seconds') || 100;
export const GAUA = 'UA-40972813-6';

export const gitHubTokenKeyInStorage = 'github-api-key';
export const gitHubEnterpriseDomainInStorage = 'github-enterprise-url';

export const gitHubEnterpriseDomainInStorage = 'github-enterprise-url';
export const defaultURL = 'https://github.com';
export const DEFAULT_API_ENDPOINT_URL = 'https://api.github.com';
export const defaultGistURL = 'https://gist.github.com';
export const gateKeeperURL = 'https://gisto-gatekeeper.azurewebsites.net/authenticate';

export const GAUA = 'UA-40972813-6';
export const gateKeeperURL = 'https://gisto-gatekeeper.azurewebsites.net/authenticate';
29 changes: 8 additions & 21 deletions src/utils/ga.js
@@ -1,22 +1,12 @@
import { GAUA } from 'constants/config';
import { isElectron } from 'utils/electron';
import Analytics from 'electron-google-analytics';

let analytics = null;

if (isElectron) {
analytics = new Analytics(GAUA);
}

export const gaPage = (page) => {
if (isElectron) {
return analytics.pageview('https://web.gistoapp.com', page, 'Gisto')
.then((response) => {
return response;
})
.catch((err) => {
return err;
});
const Analytics = require('electron-google-analytics').default;
const analytics = new Analytics(GAUA);

return analytics.pageview('https://web.gistoapp.com', page, 'Gisto');
}

gtag('config', GAUA, {
Expand All @@ -31,13 +21,10 @@ export const gaEvent = ({
category, action, label, value, cid
}) => {
if (isElectron) {
return analytics.event(category, action, { evLabel: label, evValue: value, clientID: cid })
.then((response) => {
return response;
})
.catch((err) => {
return err;
});
const Analytics = require('electron-google-analytics').default;
const analytics = new Analytics(GAUA);

return analytics.event(category, action, { evLabel: label, evValue: value, clientID: cid });
}
gtag('event', action, {
event_category: category,
Expand Down
23 changes: 21 additions & 2 deletions src/utils/settings.js
@@ -1,5 +1,6 @@
import { isElectron } from 'utils/electron';
import { handleTypes } from 'utils/types';
import { gaEvent } from 'utils/ga';

let settings;

Expand All @@ -20,8 +21,26 @@ if (isElectron) {

export const getSetting = (key, fallback = undefined) => settings.get(key, fallback);

export const setSetting = (key, value) => settings.set(key, value);
export const setSetting = (key, value) => {
gaEvent({
category: 'settings',
action: 'set',
label: key,
value
});
settings.set(key, value);
};

export const setBooleanSetting = (key) => setSetting(key, !getSetting(key));
export const setBooleanSetting = (key) => {
const newValue = !getSetting(key);

gaEvent({
category: 'settings',
action: 'set bool',
label: key,
value: newValue
});
setSetting(key, newValue);
};

export const getAllSettings = () => settings.getAll();
4 changes: 3 additions & 1 deletion webpack.web.config.js
Expand Up @@ -116,7 +116,9 @@ module.exports = {
plugins: [
new CleanWebpackPlugin(pathsToClean),
new MonacoWebpackPlugin(),
new webpack.IgnorePlugin(new RegExp(/^(fs|ipc|shell|@sentry\/electron|electron-google-analytics)$/)),
new webpack.IgnorePlugin(
new RegExp(/^(fs|ipc|shell|electron-google-analytics|@sentry\/electron)$/)
),
new HtmlWebPackPlugin({
template: './src/index.html',
filename: './index.html',
Expand Down

0 comments on commit 5ee2be9

Please sign in to comment.