Skip to content

Commit

Permalink
Optimizely attributes - use null if empty instead of undefined (#3134)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
  • Loading branch information
nicoback2 and nicoback authored Mar 31, 2023
1 parent 92ab79e commit 55f5819
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
27 changes: 20 additions & 7 deletions packages/common/src/services/remote-config/remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ type State = {
id: Nullable<number>
}

type MobileClientInfo = {
/** This is the type of Platform.OS, but we only expect ios or android here */
mobilePlatform: 'ios' | 'android' | 'web' | 'windows' | 'macos'
mobileAppVersion: string
codePushUpdateNumber: number | undefined
}

export type RemoteConfigOptions<Client> = {
createOptimizelyClient: () => Promise<Client>
getFeatureFlagSessionId: () => Promise<Nullable<number>>
setFeatureFlagSessionId: (id: number) => Promise<void>
setLogLevel: () => void
environment: Environment
appVersion: string
getMobileClientVersion?: () => Promise<string> | string
platform: 'web' | 'mobile' | 'desktop'
getMobileClientInfo?: () => Promise<MobileClientInfo> | MobileClientInfo
}

export const remoteConfig = <
Expand All @@ -64,7 +72,8 @@ export const remoteConfig = <
setLogLevel,
environment,
appVersion,
getMobileClientVersion
platform,
getMobileClientInfo
}: RemoteConfigOptions<Client>) => {
const state: State = {
didInitialize: false,
Expand All @@ -76,8 +85,9 @@ export const remoteConfig = <

// Optimizely client
let client: Client | undefined
/** Mobile app version that is being run */
let mobileClientVersion: string | undefined

/** Mobile app info if platform is mobile */
let mobileClientInfo: MobileClientInfo | undefined

const emitter = new EventEmitter()
emitter.setMaxListeners(1000)
Expand All @@ -92,8 +102,8 @@ export const remoteConfig = <
} else {
state.id = savedSessionId
}
mobileClientVersion = getMobileClientVersion
? await getMobileClientVersion()
mobileClientInfo = getMobileClientInfo
? await getMobileClientInfo()
: undefined
client = await createOptimizelyClient()

Expand Down Expand Up @@ -234,7 +244,10 @@ export const remoteConfig = <
return client.isFeatureEnabled(f, id.toString(), {
userId: id,
appVersion,
mobileClientVersion: mobileClientVersion ?? 'N/A'
platform,
mobilePlatform: mobileClientInfo?.mobilePlatform ?? null,
mobileAppVersion: mobileClientInfo?.mobileAppVersion ?? null,
codePushUpdateNumber: mobileClientInfo?.codePushUpdateNumber ?? null
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,41 @@ const { version: appVersion } = packageInfo
const OPTIMIZELY_KEY = Config.OPTIMIZELY_KEY
const DATA_FILE_URL = 'https://experiments.audius.co/datafiles/%s.json'

/** Returns version in the form of `a1.1.33+codepush:v5` if Android, `i1.1.33+codepush:v5` if iOS. The `+codepush:*` portion is omitted if there is no CodePush update installed. */
const getMobileClientVersion = async () => {
const baseVersion = `${Platform.OS === 'android' ? 'a' : 'i'}${
VersionNumber.appVersion
}`
let res = baseVersion // This is our default value if getting the CodePush update metadata fails for some reason, or if there is no CodePush update installed.
let codePushUpdateMetadata: LocalPackage | null
/** Returns mobile platform (ios or android), mobile app version, and code push update number (if any) */
const getMobileClientInfo = async () => {
const mobilePlatform = Platform.OS
const mobileAppVersion = VersionNumber.appVersion

let codePushUpdateMetadata: LocalPackage | null = null
try {
codePushUpdateMetadata = await codePush.getUpdateMetadata()
} catch (e) {
console.error(
'Error getting CodePush metadata for remote config instance.',
e
)
return res
}
if (codePushUpdateMetadata) {
res = `${baseVersion}+codepush:${codePushUpdateMetadata.label}`

let codePushUpdateNumber: number | undefined
if (
codePushUpdateMetadata &&
codePushUpdateMetadata.label &&
codePushUpdateMetadata.label.length > 1
) {
// Codepush version nunbers are formatted as e.g."v10" - remove the leading "v".
codePushUpdateNumber = Number(codePushUpdateMetadata.label.slice(1))
}
return {
mobilePlatform,
mobileAppVersion,
codePushUpdateNumber
}
return res
}

export const remoteConfigInstance = remoteConfig({
appVersion,
getMobileClientVersion,
platform: 'mobile',
getMobileClientInfo,
createOptimizelyClient: async () => {
return optimizely.createInstance({
sdkKey: OPTIMIZELY_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import optimizely, { Config } from '@optimizely/optimizely-sdk'
import { isEmpty } from 'lodash'

import { reportToSentry } from 'store/errors/reportToSentry'
import { isElectron } from 'utils/clientUtil'

import packageInfo from '../../../package.json'

Expand All @@ -18,6 +19,7 @@ export const FEATURE_FLAG_LOCAL_STORAGE_SESSION_KEY = 'featureFlagSessionId-2'

export const remoteConfigInstance = remoteConfig({
appVersion,
platform: isElectron() ? 'desktop' : 'web',
createOptimizelyClient: async () => {
// Wait for optimizely to load if necessary (as it can be an async or defer tag)
if (!window.optimizelyDatafile) {
Expand Down

0 comments on commit 55f5819

Please sign in to comment.