Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Allow changing telemetry preference without restarting app (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanglam2807 committed Feb 16, 2021
1 parent 000020d commit 82de1d4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 0 additions & 1 deletion main-src/windows/shared-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ contextMenu({

window.remote = remote;
window.ipcRenderer = ipcRenderer;
window.optOutTelemetry = !ipcRenderer.sendSync('get-preference', 'telemetry');
window.machineId = machineId.machineIdSync();
4 changes: 3 additions & 1 deletion src/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { v5 as uuidv5 } from 'uuid';

amplitude.getInstance().init(process.env.REACT_APP_AMPLITUDE_API_KEY);
amplitude.getInstance().setVersionName(`engine@${window.remote.app.getVersion()}`);
amplitude.getInstance().setOptOut(window.optOutTelemetry);
// opt out by default
// we sync this with user pref in TelemetryManager
amplitude.getInstance().setOptOut(true);

// custom device id to unify uniques between webcatalog-app & webcatalog-engine
if (window.machineId) {
Expand Down
1 change: 0 additions & 1 deletion src/components/dialog-preferences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,6 @@ const Preferences = ({
checked={telemetry}
onChange={(e) => {
requestSetPreference('telemetry', e.target.checked);
enqueueRequestRestartSnackbar();
}}
/>
</ListItemSecondaryAction>
Expand Down
10 changes: 8 additions & 2 deletions src/components/main/telemetry-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import getStaticGlobal from '../../helpers/get-static-global';

import amplitude from '../../amplitude';

const TelemetryManager = ({ iapPurchased }) => {
const TelemetryManager = ({ iapPurchased, telemetry }) => {
const appJson = getStaticGlobal('appJson');
const registered = appJson.registered || iapPurchased;

// run after setUserProperties
// https://blog.logrocket.com/post-hooks-guide-react-call-order
useEffect(() => {
amplitude.getInstance().setOptOut(!telemetry);
amplitude.getInstance().setUserProperties({
registered,
distributionChannel: (() => {
Expand All @@ -37,20 +39,24 @@ const TelemetryManager = ({ iapPurchased }) => {
return () => {
window.ipcRenderer.removeListener('log-focus', logFocus);
};
}, [registered]);
}, [registered, telemetry]);

return null;
};

TelemetryManager.defaultProps = {
iapPurchased: false,
telemetry: false,
};

TelemetryManager.propTypes = {
iapPurchased: PropTypes.bool,
telemetry: PropTypes.bool,
};

const mapStateToProps = (state) => ({
iapPurchased: state.preferences.iapPurchased,
telemetry: state.preferences.telemetry,
});

export default connectComponent(
Expand Down

0 comments on commit 82de1d4

Please sign in to comment.