Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(protocol-designer): analytics opt in modal fixes #17106

Open
wants to merge 9 commits into
base: chore_release-pd-8.2.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion protocol-designer/src/ProtocolRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function ProtocolRoutes(): JSX.Element {
const allRoutes: RouteProps[] = [...pdRoutes, landingPage]
const showGateModal =
process.env.NODE_ENV === 'production' || process.env.OT_PD_SHOW_GATE

const navigate = useNavigate()
const handleReset = (): void => {
navigate('/', { replace: true })
Expand Down
17 changes: 13 additions & 4 deletions protocol-designer/src/analytics/actions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { OLDEST_MIGRATEABLE_VERSION } from '../load-file/migration'
import { setMixpanelTracking } from './mixpanel'
import type { AnalyticsEvent } from './mixpanel'

export interface SetOptIn {
type: 'SET_OPT_IN'
payload: boolean
payload: { hasOptedIn: boolean; appVersion: string }
}

const _setOptIn = (payload: SetOptIn['payload']): SetOptIn => {
Expand All @@ -16,12 +17,20 @@ const _setOptIn = (payload: SetOptIn['payload']): SetOptIn => {

return {
type: 'SET_OPT_IN',
payload,
payload: { hasOptedIn: payload.hasOptedIn, appVersion: payload.appVersion },
}
}

export const optIn = (): SetOptIn => _setOptIn(true)
export const optOut = (): SetOptIn => _setOptIn(false)
export const optIn = (): SetOptIn =>
_setOptIn({
hasOptedIn: true,
appVersion: process.env.OT_PD_VERSION || OLDEST_MIGRATEABLE_VERSION,
})
export const optOut = (): SetOptIn =>
_setOptIn({
hasOptedIn: false,
appVersion: process.env.OT_PD_VERSION || OLDEST_MIGRATEABLE_VERSION,
})
export interface AnalyticsEventAction {
type: 'ANALYTICS_EVENT'
payload: AnalyticsEvent
Expand Down
2 changes: 1 addition & 1 deletion protocol-designer/src/analytics/middleware.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not directly related to this pr but seems that dispatch in trackEventMiddleware isn't used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll leave that to a follow up unless you think its affecting the modal?

Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ export const trackEventMiddleware: Middleware<BaseState, any> = ({
// NOTE: this is the Redux state AFTER the action has been fully dispatched
const state = getState()

const optedIn = getHasOptedIn(state as BaseState) ?? false
const optedIn = getHasOptedIn(state as BaseState)?.hasOptedIn ?? false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure but the request is mixpanel is on as the default setting so this might be true?

const event = reduxActionToAnalyticsEvent(state as BaseState, action)

if (event != null) {
Expand Down
2 changes: 1 addition & 1 deletion protocol-designer/src/analytics/mixpanel.ts
jerader marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const MIXPANEL_OPTS = {
}

export function initializeMixpanel(state: BaseState): void {
const optedIn = getHasOptedIn(state) ?? false
const optedIn = getHasOptedIn(state)?.hasOptedIn ?? false
if (MIXPANEL_ID != null) {
console.debug('Initializing Mixpanel', { optedIn })

Expand Down
14 changes: 11 additions & 3 deletions protocol-designer/src/analytics/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import type { Reducer } from 'redux'
import type { Action } from '../types'
import type { SetOptIn } from './actions'
import type { RehydratePersistedAction } from '../persist'
type OptInState = boolean | null
const optInInitialState = null
export interface OptInState {
hasOptedIn: boolean | null
appVersion?: string
}
const optInInitialState = {
hasOptedIn: null,
}

// @ts-expect-error(sb, 2021-6-17): cannot use string literals as action type
// TODO IMMEDIATELY: refactor this to the old fashioned way if we cannot have type safety: https://github.com/redux-utilities/redux-actions/issues/282#issuecomment-595163081
const hasOptedIn: Reducer<OptInState, any> = handleActions(
Expand All @@ -17,7 +23,9 @@ const hasOptedIn: Reducer<OptInState, any> = handleActions(
action: RehydratePersistedAction
) => {
const persistedState = action.payload?.['analytics.hasOptedIn']
return persistedState !== undefined ? persistedState : optInInitialState
return persistedState !== undefined && state.appVersion != null
? persistedState
: optInInitialState
},
},
optInInitialState
Expand Down
3 changes: 2 additions & 1 deletion protocol-designer/src/analytics/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BaseState } from '../types'
export const getHasOptedIn = (state: BaseState): boolean | null =>
import type { OptInState } from './reducers'
export const getHasOptedIn = (state: BaseState): OptInState =>
state.analytics.hasOptedIn
2 changes: 1 addition & 1 deletion protocol-designer/src/assets/localization/en/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"only_tiprack": "Incompatible file type",
"opentrons_flex": "Opentrons Flex",
"opentrons": "Opentrons",
"opentrons_collects_data": "In order to improve our products, Opentrons would like to collect data related to your use of Protocol Designer. With your consent, Opentrons will collect and store analytics and session data, including through the use of cookies and similar technologies, solely for the purpose enhancing our products.",
"opentrons_collects_data": "In order to improve our products, Opentrons would like to collect data related to your use of Protocol Designer. Opentrons will collect and store analytics and session data, including through the use of cookies and similar technologies, solely for the purpose enhancing our products.",
"ot2": "Opentrons OT-2",
"overwrite_labware": "Overwrite labware",
"overwrite": "Click Overwrite to replace the existing labware with the new labware.",
Expand Down
15 changes: 9 additions & 6 deletions protocol-designer/src/components/SettingsPage/SettingsApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@

export function SettingsApp(): JSX.Element {
const dispatch = useDispatch()
const hasOptedIn = useSelector(analyticsSelectors.getHasOptedIn)
const analytics = useSelector(analyticsSelectors.getHasOptedIn)
jerader marked this conversation as resolved.
Show resolved Hide resolved
const canClearHintDismissals = useSelector(
tutorialSelectors.getCanClearHintDismissals
)
const _toggleOptedIn = hasOptedIn
? analyticsActions.optOut
: analyticsActions.optIn

const { t } = useTranslation(['card', 'application', 'button'])
return (
Expand Down Expand Up @@ -72,8 +69,14 @@
<p className={styles.toggle_label}>{t('toggle.share_session')}</p>
<ToggleButton
className={styles.toggle_button}
toggledOn={Boolean(hasOptedIn)}
onClick={() => dispatch(_toggleOptedIn())}
toggledOn={Boolean(analytics.hasOptedIn)}
onClick={() =>
dispatch(
analytics.hasOptedIn

Check failure on line 75 in protocol-designer/src/components/SettingsPage/SettingsApp.tsx

View workflow job for this annotation

GitHub Actions / js checks

Argument of type '() => SetOptIn' is not assignable to parameter of type 'AnyAction'.

Check failure on line 75 in protocol-designer/src/components/SettingsPage/SettingsApp.tsx

View workflow job for this annotation

GitHub Actions / js checks

Argument of type '() => SetOptIn' is not assignable to parameter of type 'AnyAction'.
? analyticsActions.optOut
: analyticsActions.optIn
jerader marked this conversation as resolved.
Show resolved Hide resolved
)
}
/>
</div>

Expand Down
17 changes: 3 additions & 14 deletions protocol-designer/src/organisms/GateModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Modal,
PrimaryButton,
SPACING,
SecondaryButton,
StyledText,
} from '@opentrons/components'
import {
Expand All @@ -22,10 +21,10 @@ const EULA_URL = 'https://opentrons.com/eula'

export function GateModal(): JSX.Element | null {
const { t } = useTranslation('shared')
const hasOptedIn = useSelector(analyticsSelectors.getHasOptedIn)
const analytics = useSelector(analyticsSelectors.getHasOptedIn)
jerader marked this conversation as resolved.
Show resolved Hide resolved
const dispatch = useDispatch()

if (hasOptedIn == null) {
if (analytics.appVersion == null || analytics.hasOptedIn == null) {
return (
<Modal
position="bottomRight"
Expand All @@ -36,16 +35,9 @@ export function GateModal(): JSX.Element | null {
gridGap={SPACING.spacing8}
padding={SPACING.spacing24}
>
<SecondaryButton
onClick={() => dispatch(analyticsActions.optOut())}
>
<StyledText desktopStyle="bodyDefaultRegular">
{t('reject')}
</StyledText>
</SecondaryButton>
<PrimaryButton onClick={() => dispatch(analyticsActions.optIn())}>
<StyledText desktopStyle="bodyDefaultRegular">
{t('agree')}
{t('confirm')}
</StyledText>
</PrimaryButton>
</Flex>
Expand Down Expand Up @@ -85,9 +77,6 @@ export function GateModal(): JSX.Element | null {
}}
/>
</StyledText>
<StyledText desktopStyle="bodyDefaultRegular">
{t('analytics_tracking')}
</StyledText>
</Flex>
</Modal>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const render = () => {

describe('Landing', () => {
beforeEach(() => {
vi.mocked(getHasOptedIn).mockReturnValue(false)
vi.mocked(getHasOptedIn).mockReturnValue({
hasOptedIn: false,
appVersion: '8.2.1',
})
vi.mocked(getFileMetadata).mockReturnValue({})
vi.mocked(loadProtocolFile).mockReturnValue(vi.fn())
vi.mocked(useAnnouncements).mockReturnValue({} as any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const render = () => {

describe('Settings', () => {
beforeEach(() => {
vi.mocked(getHasOptedIn).mockReturnValue(false)
vi.mocked(getHasOptedIn).mockReturnValue({
hasOptedIn: false,
appVersion: '8.2.1',
})
vi.mocked(getFeatureFlagData).mockReturnValue({})
vi.mocked(getCanClearHintDismissals).mockReturnValue(true)
})
Expand Down
23 changes: 15 additions & 8 deletions protocol-designer/src/pages/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import { actions as featureFlagActions } from '../../feature-flags'
import { getFeatureFlagData } from '../../feature-flags/selectors'
import type { FlagTypes } from '../../feature-flags'
import { OLDEST_MIGRATEABLE_VERSION } from '../../load-file/migration'

Check failure on line 34 in protocol-designer/src/pages/Settings/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

'OLDEST_MIGRATEABLE_VERSION' is defined but never used

Check failure on line 34 in protocol-designer/src/pages/Settings/index.tsx

View workflow job for this annotation

GitHub Actions / js checks

'OLDEST_MIGRATEABLE_VERSION' is defined but never used
jerader marked this conversation as resolved.
Show resolved Hide resolved

const HOT_KEY_FLAG = 'OT_PD_ENABLE_HOT_KEYS_DISPLAY'
const PRIVACY_POLICY_URL = 'https://opentrons.com/privacy-policy'
Expand All @@ -42,18 +43,16 @@
const [showAnnouncementModal, setShowAnnouncementModal] = useState<boolean>(
false
)
const hasOptedIn = useSelector(analyticsSelectors.getHasOptedIn)
const analytics = useSelector(analyticsSelectors.getHasOptedIn)
const flags = useSelector(getFeatureFlagData)
const canClearHintDismissals = useSelector(
tutorialSelectors.getCanClearHintDismissals
)
const _toggleOptedIn = hasOptedIn
? analyticsActions.optOut
: analyticsActions.optIn

const prereleaseModeEnabled = flags.PRERELEASE_MODE === true
const pdVersion = process.env.OT_PD_VERSION

const prereleaseModeEnabled = flags.PRERELEASE_MODE === true

const allFlags = Object.keys(flags) as FlagTypes[]

const getDescription = (flag: FlagTypes): string => {
Expand Down Expand Up @@ -276,15 +275,23 @@
data-testid="analyticsToggle"
size="2rem"
css={
Boolean(hasOptedIn)
Boolean(analytics.hasOptedIn)
? TOGGLE_ENABLED_STYLES
: TOGGLE_DISABLED_STYLES
}
onClick={() => dispatch(_toggleOptedIn())}
onClick={() =>
dispatch(
analytics.hasOptedIn
? analyticsActions.optOut()
: analyticsActions.optIn()
)
}
>
<Icon
name={
hasOptedIn ? 'ot-toggle-input-on' : 'ot-toggle-input-off'
analytics.hasOptedIn
? 'ot-toggle-input-on'
: 'ot-toggle-input-off'
}
height="1rem"
/>
Expand Down
Loading