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

feat(packages/sui-pde): upgrade optimizely version and add use-decisi… #1824

Merged
merged 6 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
135 changes: 57 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sui-pde/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@optimizely/optimizely-sdk": "4.9.4",
"@optimizely/optimizely-sdk": "5.3.4",
"@s-ui/js": "2"
},
"peerDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions packages/sui-pde/src/adapters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class DefaultAdapter {
return null
}

decide() {
return null
}

updateConsents() {
return null
}
Expand Down
21 changes: 20 additions & 1 deletion packages/sui-pde/src/adapters/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ export default class OptimizelyAdapter {
sdkKey = undefined
}

const isServer = typeof window === 'undefined'
const optimizelyInstance = optimizely.createInstance({
sdkKey,
datafileOptions: options,
datafile,
eventDispatcher,
...DEFAULT_EVENTS_OPTIONS
...DEFAULT_EVENTS_OPTIONS,
defaultDecideOptions: isServer ? [optimizely.OptimizelyDecideOption.DISABLE_DECISION_EVENT] : []
})

return optimizelyInstance
Expand Down Expand Up @@ -117,6 +119,23 @@ export default class OptimizelyAdapter {
})
}

/**
* @param {Object} params
* @param {string} params.name
* @param {object} [params.attributes]
* @returns {string=} variation name
*/
decide({name, attributes}) {
if (!this._hasUserConsents) return null

const user = this._optimizely.createUserContext(this._userId, {
...this._applicationAttributes,
...attributes
})

return user.decide(name)
}

/**
* Gets the variation without tracking the impression
* @param {Object} params
Expand Down
4 changes: 4 additions & 0 deletions packages/sui-pde/src/adapters/optimizely/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class MultipleOptimizelyAdapter {
return this.#adapters[adapterId].activateExperiment(props)
}

decide({adapterId = defaultAdapterId, ...props}) {
return this.#adapters[adapterId].decide(props)
}

getVariation({adapterId = defaultAdapterId, ...props}) {
return this.#adapters[adapterId].getVariation(props)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/sui-pde/src/hooks/common/platformStrategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const getServerStrategy = () => ({
getVariation: ({pde, experimentName, attributes, adapterId}) => {
return pde.getVariation({pde, name: experimentName, attributes, adapterId})
},
decide: ({pde, name, attributes, adapterId}) => {
return pde.decide({pde, name, attributes, adapterId})
},
trackExperiment: () => {},
getForcedValue: ({key, queryString}) => {
if (!queryString) {
Expand All @@ -27,6 +30,9 @@ const getBrowserStrategy = ({customTrackExperimentViewed, cache}) => ({

return variationName
},
decide: ({pde, name, attributes, adapterId}) => {
return pde.decide({pde, name, attributes, adapterId})
},
trackExperiment: ({variationName, experimentName}) => {
if (customTrackExperimentViewed) {
return customTrackExperimentViewed({variationName, experimentName})
Expand Down
58 changes: 58 additions & 0 deletions packages/sui-pde/src/hooks/useDecision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {useContext, useMemo} from 'react'

import PdeContext from '../contexts/PdeContext.js'
import {getPlatformStrategy} from './common/platformStrategies.js'

/**
* Hook to use a feature test
* @param {string} name
* @param {object} param
* @param {object} param.attributes
* @param {function} param.trackExperimentViewed
* @param {string} param.queryString
* @param {string} param.adapterId Adapter id to be executed
* @return {object}
*/
export default function useDecision(name, {attributes, trackExperimentViewed, queryString, adapterId} = {}) {
const {pde} = useContext(PdeContext)

if (pde === null) {
throw new Error('[sui-pde: useDecision] sui-pde provider is required to work')
}

const variation = useMemo(() => {
const strategy = getPlatformStrategy({
customTrackExperimentViewed: trackExperimentViewed
})

const forced = strategy.getForcedValue({
key: name,
queryString
})

const data = strategy.decide({
pde,
name,
attributes,
adapterId
})

const {ruleKey, variationKey} = data || {}

if (forced) {
if (!ruleKey) {
return {...data, enabled: forced === 'on'}
}

return {...data, enabled: true, variationKey: forced}
}

if (ruleKey) {
strategy.trackExperiment({variationName: variationKey, experimentName: name})
}

return data
}, [trackExperimentViewed, name, queryString, pde, attributes, adapterId])

return {variation}
}
1 change: 1 addition & 0 deletions packages/sui-pde/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export {default as PDE} from './pde.js'
export {default as useFeature} from './hooks/useFeature.js'
export {default as PdeContext} from './contexts/PdeContext.js'
export {default as useExperiment} from './hooks/useExperiment.js'
export {default as useDecision} from './hooks/useDecision.js'
export {default as Experiment} from './components/experiment.js'
export {default as Feature} from './components/feature.js'
9 changes: 9 additions & 0 deletions packages/sui-pde/src/pde.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export default class PDE {
return this._adapter.activateExperiment({name, attributes, adapterId})
}

/**
* @param {object} param
* @param {string} param.name
* @param {object} param.attributes
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param {object} param.attributes
* @param {object} param.attributes
* @param {string} param.adapterId

*/
decide({name, attributes, adapterId}) {
return this._adapter.decide({name, attributes, adapterId})
}

getInitialData() {
return this._adapter.getInitialData()
}
Expand Down