Browse Source

only identify is sent & only if you pass onboarding

master
Gaëtan Renaudeau 7 years ago
parent
commit
dfc01897f2
  1. 18
      src/analytics/segment.js
  2. 24
      src/middlewares/analytics.js
  3. 6
      src/reducers/devices.js

18
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

24
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)
}
}

6
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 }) => ({

Loading…
Cancel
Save