Skip to content

Commit

Permalink
fix: Optimizely init fix (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma authored Aug 6, 2024
1 parent 627f589 commit c4bed2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/data/optimizely.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ ensureConfig([
* the OPTIMIZELY_FULL_STACK_SDK_KEY is available and will not be reinitialized afterward. Wrapping the initialization
* in a function allows components to request the instance as-needed.
*/
let instance = null;
const getOptimizely = () => {
const OPTIMIZELY_SDK_KEY = getConfig().OPTIMIZELY_FULL_STACK_SDK_KEY;

if (OPTIMIZELY_SDK_KEY) {
return createInstance({
if (!instance && OPTIMIZELY_SDK_KEY) {
instance = createInstance({
sdkKey: OPTIMIZELY_SDK_KEY,
});
}

return null;
return instance;
};

const OPTIMIZELY_PROMPT_EXPERIMENT_KEY = '_cosmo__xpert_gpt_4_0_prompt';
Expand Down
7 changes: 5 additions & 2 deletions src/experiments/ExperimentsProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ const ExperimentsProvider = ({ children }) => {
const { userId } = getAuthenticatedUser();
const optimizely = getOptimizely();

return (
return optimizely ? (
<OptimizelyProvider
optimizely={optimizely}
user={{
id: userId.toString(),
attributes: { lms_language_preference: getLocale() },
}}
>{children}
>
{children}
</OptimizelyProvider>
) : (
children
);
};

Expand Down
7 changes: 6 additions & 1 deletion src/utils/optimizelyExperiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { getOptimizely } from '../data/optimizely';

const trackChatBotMessageOptimizely = (userId, userAttributes = {}) => {
const optimizelyInstance = getOptimizely();
optimizelyInstance.track('learning_assistant_chat_message', userId, userAttributes);

if (!optimizelyInstance) { return; }

optimizelyInstance.onReady().then(() => {
optimizelyInstance.track('learning_assistant_chat_message', userId, userAttributes);
});
};

export default trackChatBotMessageOptimizely;

0 comments on commit c4bed2e

Please sign in to comment.