Skip to content

Commit

Permalink
feat: Add audit trial length optimizely experiment
Browse files Browse the repository at this point in the history
- Add the experiment and variation keys
- And some logic for upgrade eligibility
  • Loading branch information
ilee2u committed Jan 31, 2025
1 parent 2a667bf commit b8281e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/data/optimizely.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ const getOptimizely = () => {
return instance;
};

const OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY = 'xpert_audit_trial_experiment';
const OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_VARIATION_KEYS = {
XPERT_AUDIT_14_DAY_TRIAL: 'xpert_audit_14_day_trial',
XPERT_AUDIT_28_DAY_TRIAL: 'xpert_audit_28_day_trial',
};

const OPTIMIZELY_PROMPT_EXPERIMENT_KEY = '_cosmo__xpert_gpt_4_0_prompt';
const OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS = {
UPDATED_PROMPT: 'updated_prompt',
};

export {
getOptimizely,
OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY,
OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_VARIATION_KEYS,
OPTIMIZELY_PROMPT_EXPERIMENT_KEY,
OPTIMIZELY_PROMPT_EXPERIMENT_VARIATION_KEYS,
};
14 changes: 13 additions & 1 deletion src/experiments/experimentHooks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useDecision } from '@optimizely/react-sdk';

import { OPTIMIZELY_PROMPT_EXPERIMENT_KEY } from '../data/optimizely';
import { OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY, OPTIMIZELY_PROMPT_EXPERIMENT_KEY } from '../data/optimizely';

// eslint-disable-next-line import/prefer-default-export
export function usePromptExperimentDecision() {
Expand All @@ -14,3 +14,15 @@ export function usePromptExperimentDecision() {

return [decision];
}

// eslint-disable-next-line import/prefer-default-export
export function useAuditTrialExperimentDecision() {
const { userId } = getAuthenticatedUser();

const [decision] = useDecision(
OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY,
{ overrideUserId: userId.toString() }, // This override is just to make sure the userId is up to date.
);

return [decision];
}
33 changes: 33 additions & 0 deletions src/hooks/use-course-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { useModel } from '@src/generic/model-store'; // eslint-disable-line impo
import { useSelector } from 'react-redux';
import { CourseInfoContext } from '../context';

import { sendTrackEvent } from '@edx/frontend-platform/analytics';

Check failure on line 6 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

`@edx/frontend-platform/analytics` import should occur before import of `../context`

Check failure on line 6 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

'sendTrackEvent' is defined but never used

import {
OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY,
OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_VARIATION_KEYS,
} from '../../data/optimizely';

Check failure on line 11 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../../data/optimizely'

Check failure on line 11 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../../data/optimizely"
import { useAuditTrialExperimentDecision } from '../../experiments';

Check failure on line 12 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

Unable to resolve path to module '../../experiments'

Check failure on line 12 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

Missing file extension for "../../experiments"

const millisecondsInOneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds

/**
Expand All @@ -28,6 +36,7 @@ const millisecondsInOneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milli
*
* @returns {CourseUpgradeInfo}
*/
// Update logic here to make upgrade eligible if in experiment variation
export default function useCourseUpgrade() {
const { courseId, isUpgradeEligible } = useContext(CourseInfoContext);
const {
Expand All @@ -41,6 +50,30 @@ export default function useCourseUpgrade() {
auditTrial,
} = useSelector(state => state.learningAssistant);

const [decision] = useAuditTrialExperimentDecision();
const { enabled, variationKey } = decision || {};
const experimentPayload = enabled ? {

Check failure on line 55 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

'experimentPayload' is assigned a value but never used
experiment_name: OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_KEY,
variation_key: variationKey,
} : {};

// Make upgrade eligible if in experiment variation
if (OPTIMIZELY_AUDIT_TRIAL_LENGTH_EXPERIMENT_VARIATION_KEYS.includes(variationKey)) {
isUpgradeEligible = true;

Check failure on line 62 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

'isUpgradeEligible' is constant

// Do I need to add a sendTrackEvent call like this?
// sendTrackEvent(
// 'edx.ui.lms.learning_assistant.audit_trial_started',
// {
// course_id: courseId,
// user_id: userId,
// source: event.target.id === 'toggle-button' ? 'toggle' : 'cta',
// ...experimentPayload,
// },
// );
}


Check failure on line 76 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

More than 1 blank line not allowed
const upgradeUrl = offer?.upgradeUrl || verifiedMode?.upgradeUrl;

if (!isUpgradeEligible || !upgradeUrl) { return { upgradeable: false }; }
Expand Down

0 comments on commit b8281e1

Please sign in to comment.