diff --git a/src/analytics/segment.js b/src/analytics/segment.js index bf0d7938..4faa73d0 100644 --- a/src/analytics/segment.js +++ b/src/analytics/segment.js @@ -3,8 +3,11 @@ import uuid from 'uuid/v4' import logger from 'logger' import invariant from 'invariant' -import { langAndRegionSelector } from 'reducers/settings' import { getSystemLocale } from 'helpers/systemLocale' +import { langAndRegionSelector, shareAnalyticsSelector } from 'reducers/settings' +import { getCurrentDevice } from 'reducers/devices' +import type { State } from 'reducers' + import { load } from './inject-in-window' invariant(typeof window !== 'undefined', 'analytics/segment must be called on renderer thread') @@ -21,9 +24,15 @@ const getContext = _store => ({ }) const extraProperties = store => { - const state = store.getState() + const state: State = store.getState() const { language, region } = langAndRegionSelector(state) const systemLocale = getSystemLocale() + const device = getCurrentDevice(state) + const deviceInfo = device && { + productId: device.productId, + product: device.product, + vendorId: device.vendorId, + } return { appVersion: __APP_VERSION__, language, @@ -32,6 +41,7 @@ const extraProperties = store => { systemLanguage: systemLocale.language, systemRegion: systemLocale.region, sessionId, + ...deviceInfo, } } @@ -66,7 +76,7 @@ export const stop = () => { export const track = (event: string, properties: ?Object) => { logger.analyticsTrack(event, properties) - if (!storeInstance) { + if (!storeInstance || !shareAnalyticsSelector(storeInstance.getState())) { return } const { analytics } = window @@ -88,7 +98,7 @@ export const track = (event: string, properties: ?Object) => { export const page = (category: string, name: ?string, properties: ?Object) => { logger.analyticsPage(category, name, properties) - if (!storeInstance) { + if (!storeInstance || !shareAnalyticsSelector(storeInstance.getState())) { return } const { analytics } = window diff --git a/src/middlewares/analytics.js b/src/middlewares/analytics.js index 26d116b7..8257c575 100644 --- a/src/middlewares/analytics.js +++ b/src/middlewares/analytics.js @@ -1,18 +1,18 @@ -import { shareAnalyticsSelector } from 'reducers/settings' -import { start, stop } from 'analytics/segment' +// @flow + +import { hasCompletedOnboardingSelector } from 'reducers/settings' +import { start } from 'analytics/segment' +import type { State } from 'reducers' let isAnalyticsStarted = false -export default store => next => action => { +export default (store: *) => (next: *) => (action: *) => { next(action) - const state = store.getState() - const shareAnalytics = shareAnalyticsSelector(state) - if (shareAnalytics !== isAnalyticsStarted) { - isAnalyticsStarted = shareAnalytics - if (shareAnalytics) { - start(store) - } else { - stop() - } + const state: State = store.getState() + const hasCompletedOnboarding = hasCompletedOnboardingSelector(state) + + if (hasCompletedOnboarding && !isAnalyticsStarted) { + isAnalyticsStarted = true + start(store) } } diff --git a/src/reducers/devices.js b/src/reducers/devices.js index 430eda36..31194064 100644 --- a/src/reducers/devices.js +++ b/src/reducers/devices.js @@ -5,7 +5,7 @@ import { handleActions } from 'redux-actions' import type { Device } from 'types/common' export type DevicesState = { - currentDevice: Device | null, + currentDevice: ?Device, devices: Device[], } @@ -31,9 +31,7 @@ const handlers: Object = { REMOVE_DEVICE: (state: DevicesState, { payload: device }: { payload: Device }) => ({ ...state, currentDevice: - state.currentDevice !== null && state.currentDevice.path === device.path - ? null - : state.currentDevice, + state.currentDevice && state.currentDevice.path === device.path ? null : state.currentDevice, devices: state.devices.filter(d => d.path !== device.path), }), SET_CURRENT_DEVICE: (state: DevicesState, { payload: currentDevice }: { payload: Device }) => ({