diff --git a/src/main/ScratchDesktopTelemetry.js b/src/main/ScratchDesktopTelemetry.js index e70eba7..0c4302f 100644 --- a/src/main/ScratchDesktopTelemetry.js +++ b/src/main/ScratchDesktopTelemetry.js @@ -1,3 +1,5 @@ +import {ipcMain} from 'electron'; + import TelemetryClient from './telemetry/TelemetryClient'; const info = { @@ -17,6 +19,13 @@ class ScratchDesktopTelemetry { this._telemetryClient = new TelemetryClient(); } + get didOptIn () { + return this._telemetryClient.didOptIn; + } + set didOptIn (value) { + this._telemetryClient.didOptIn = value; + } + appWasOpened () { this._telemetryClient.addEvent('app::open', info); } @@ -42,4 +51,14 @@ class ScratchDesktopTelemetry { } } -export default ScratchDesktopTelemetry; +// make a singleton so it's easy to share across both Electron processes +const scratchDesktopTelemetrySingleton = new ScratchDesktopTelemetry(); + +ipcMain.on('getTelemetryDidOptIn', event => { + event.returnValue = scratchDesktopTelemetrySingleton.didOptIn; +}); +ipcMain.on('setTelemetryDidOptIn', (event, arg) => { + scratchDesktopTelemetrySingleton.didOptIn = arg; +}); + +export default scratchDesktopTelemetrySingleton; diff --git a/src/main/index.js b/src/main/index.js index 37b45bc..6c95a0a 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,9 +1,8 @@ import {BrowserWindow, app, dialog} from 'electron'; import * as path from 'path'; import {format as formatUrl} from 'url'; -import ScratchDesktopTelemetry from './ScratchDesktopTelemetry'; +import telemetry from './ScratchDesktopTelemetry'; -const telemetry = new ScratchDesktopTelemetry(); telemetry.appWasOpened(); diff --git a/src/main/telemetry/TelemetryClient.js b/src/main/telemetry/TelemetryClient.js index 8b6f263..23480ac 100644 --- a/src/main/telemetry/TelemetryClient.js +++ b/src/main/telemetry/TelemetryClient.js @@ -132,7 +132,8 @@ class TelemetryClient { * @type {boolean} */ get didOptIn () { - return this._store.get('optIn', false); + // don't supply a default here: we want to track "opt out" separately from "undecided" + return this._store.get('optIn'); } set didOptIn (value) { this._store.set('optIn', !!value); diff --git a/src/renderer/index.js b/src/renderer/index.js index 217c840..559c217 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -1,3 +1,4 @@ +import {ipcRenderer} from 'electron'; import React from 'react'; import ReactDOM from 'react-dom'; import GUI, {AppStateHOC} from 'scratch-gui'; @@ -42,7 +43,14 @@ const onStorageInit = storageInstance => { const guiProps = { onStorageInit, isScratchDesktop: true, - projectId: defaultProjectId + projectId: defaultProjectId, + showTelemetryModal: (typeof ipcRenderer.sendSync('getTelemetryDidOptIn')) !== 'boolean', + onTelemetryModalOptIn: () => { + ipcRenderer.send('setTelemetryDidOptIn', true); + }, + onTelemetryModalOptOut: () => { + ipcRenderer.send('setTelemetryDidOptIn', false); + } }; const wrappedGui = React.createElement(WrappedGui, guiProps); ReactDOM.render(wrappedGui, appTarget);