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: Optimizely init fix #60

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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;
Loading