Skip to content

Commit

Permalink
Merge pull request #572 from cboard-org/fix-ga
Browse files Browse the repository at this point in the history
HOTFIX - Fix for google analytics api update #571
  • Loading branch information
martinbedouret authored Nov 25, 2019
2 parents 2d660e0 + 679061d commit 778327a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import GoogleAnalyticsGtag from '@redux-beacon/google-analytics-gtag';
import offlineWeb from '@redux-beacon/offline-web';
// import logger from '@redux-beacon/logger';

import { isCordova } from './cordova-util';
import { isCordova, onCordovaReady } from './cordova-util';
import boardEventsMap from './components/Board/Board.analytics';
import speechEventsMap from './providers/SpeechProvider/SpeechProvider.analytics';

Expand All @@ -22,8 +22,4 @@ const gaMiddleware = createMiddleware(eventsMap, ga, {
offlineStorage
});

if (isCordova()) {
window.ga.startTrackerWithId('UA-152065055-1', 20);
}

export default gaMiddleware;
16 changes: 8 additions & 8 deletions src/components/Board/Board.analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trackEvent } from '@redux-beacon/google-analytics-gtag';
import { isCordova } from '../../cordova-util';
import { isCordova, cvaTrackEvent } from '../../cordova-util';

import {
IMPORT_BOARDS,
Expand All @@ -26,7 +26,7 @@ const importBoards = trackEvent((action, prevState, nextState) => {
action: 'Import Boards'
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action);
cvaTrackEvent(gaEvent.category, gaEvent.action);
}
return gaEvent;
});
Expand All @@ -42,7 +42,7 @@ const changeBoard = trackEvent((action, prevState, nextState) => {
label: boardName
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -54,7 +54,7 @@ const createBoard = trackEvent((action, prevState, nextState) => {
label: action.boardName
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -66,7 +66,7 @@ const createTile = trackEvent((action, prevState, nextState) => {
label: action.tile.label
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -83,7 +83,7 @@ const deleteTiles = trackEvent((action, prevState, nextState) => {
label: deletedTiles
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -99,7 +99,7 @@ const editTiles = trackEvent((action, prevState, nextState) => {
label: editedTiles
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -111,7 +111,7 @@ const clickSymbol = trackEvent((action, prevState, nextState) => {
label: action.symbolLabel
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand Down
23 changes: 23 additions & 0 deletions src/cordova-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ export const isCordova = () => !!window.cordova;
export const onCordovaReady = onReady =>
document.addEventListener('deviceready', onReady, false);

export const initCordovaPlugins = () => {
document.addEventListener('deviceready', onDeviceReady, false);
};

function onDeviceReady() {
console.log('now cordova is ready ');
if (isCordova()) {
try {
window.ga.startTrackerWithId('UA-152065055-1', 20);
} catch (err) {
console.log(err.message);
}
}
}

export const cvaTrackEvent = (category, action, label) => {
try {
window.ga.trackEvent(category, action, label);
} catch (err) {
console.log(err.message);
}
};

export const writeCvaFile = async (name, blob) => {
if (isCordova()) {
return new Promise(function(resolve, reject) {
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Provider } from 'react-redux';
import { BrowserRouter, HashRouter, Route } from 'react-router-dom';
import { PersistGate } from 'redux-persist/es/integration/react';
import App from './components/App';
import { isCordova, onCordovaReady } from './cordova-util';
import { isCordova, onCordovaReady, initCordovaPlugins } from './cordova-util';
import './index.css';
import './polyfills';
import LanguageProvider from './providers/LanguageProvider';
Expand Down Expand Up @@ -37,4 +37,7 @@ const renderApp = () => {
);
};

if (isCordova()) {
initCordovaPlugins();
}
isCordova() ? onCordovaReady(renderApp) : renderApp();
10 changes: 5 additions & 5 deletions src/providers/SpeechProvider/SpeechProvider.analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CHANGE_RATE,
START_SPEECH
} from './SpeechProvider.constants';
import { isCordova } from '../../cordova-util';
import { isCordova, cvaTrackEvent } from '../../cordova-util';

const changeVoice = trackEvent((action, prevState, nextState) => {
const gaEvent = {
Expand All @@ -15,7 +15,7 @@ const changeVoice = trackEvent((action, prevState, nextState) => {
label: action.voiceURI
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -27,7 +27,7 @@ const changePitch = trackEvent((action, prevState, nextState) => {
label: action.pitch
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -39,7 +39,7 @@ const changeRate = trackEvent((action, prevState, nextState) => {
label: action.rate
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand All @@ -51,7 +51,7 @@ const startSpeech = trackEvent((action, prevState, nextState) => {
label: action.text
};
if (isCordova()) {
window.ga.trackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
cvaTrackEvent(gaEvent.category, gaEvent.action, gaEvent.label);
}
return gaEvent;
});
Expand Down
7 changes: 6 additions & 1 deletion src/providers/SpeechProvider/tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ const tts = {

// Get voices depending on platform (browser/cordova)
_getPlatformVoices() {
const voices = synth.getVoices();
let voices = {};
try {
voices = synth.getVoices();
} catch (err) {
console.log(err.message);
}
// On Cordova, voice results are under `._list`
return voices._list || voices;
},
Expand Down

0 comments on commit 778327a

Please sign in to comment.