diff --git a/.env b/.env index fdebe809..63ad0390 100644 --- a/.env +++ b/.env @@ -17,5 +17,5 @@ USER_INFO_COOKIE_NAME=null AUTHN_MINIMAL_HEADER=true LOGIN_ISSUE_SUPPORT_LINK=null REGISTRATION_OPTIONAL_FIELDS=null -USER_SIGNUP_SURVEY_COOKIE_NAME=null +USER_SURVEY_COOKIE_NAME=null COOKIE_DOMAIN=null diff --git a/.env.development b/.env.development index 5c08f92e..54cd540b 100644 --- a/.env.development +++ b/.env.development @@ -24,5 +24,5 @@ LOGIN_ISSUE_SUPPORT_LINK='' TOS_AND_HONOR_CODE='http://localhost:18000/honor' PRIVACY_POLICY='http://localhost:18000/privacy' REGISTRATION_OPTIONAL_FIELDS='gender,goals,level_of_education,year_of_birth' -USER_SIGNUP_SURVEY_COOKIE_NAME='openedx-user-signup-timestamp' +USER_SURVEY_COOKIE_NAME='openedx-user-survey-type' COOKIE_DOMAIN='localhost' diff --git a/.env.test b/.env.test index 03015c5d..7013d71b 100644 --- a/.env.test +++ b/.env.test @@ -18,4 +18,4 @@ SEGMENT_KEY=null SITE_NAME='edX' USER_INFO_COOKIE_NAME='edx-user-info' LOGIN_ISSUE_SUPPORT_LINK='https://login-issue-support-url.com' -USER_SIGNUP_SURVEY_COOKIE_NAME='openedx-user-signup-timestamp' +USER_SURVEY_COOKIE_NAME='openedx-user-survey-type' diff --git a/src/data/utils/cookies.js b/src/data/utils/cookies.js new file mode 100644 index 00000000..e9e5265c --- /dev/null +++ b/src/data/utils/cookies.js @@ -0,0 +1,14 @@ +import Cookies from 'universal-cookie'; +import { getConfig } from '@edx/frontend-platform'; + +export default function setSurveyCookie(surveyType) { + const cookieName = getConfig().USER_SURVEY_COOKIE_NAME; + if (cookieName) { + const cookies = new Cookies(); + const signupTimestamp = (new Date()).getTime(); + // set expiry to exactly 24 hours from now + const cookieExpiry = new Date(signupTimestamp + 1 * 864e5); + const options = { domain: getConfig().COOKIE_DOMAIN, expires: cookieExpiry, path: '/' }; + cookies.set(cookieName, surveyType, options); + } +} diff --git a/src/data/utils/index.js b/src/data/utils/index.js index 55710159..09722533 100644 --- a/src/data/utils/index.js +++ b/src/data/utils/index.js @@ -8,3 +8,4 @@ export { windowScrollTo, } from './dataUtils'; export { default as AsyncActionType } from './reduxUtils'; +export { default as setSurveyCookie } from './cookies'; diff --git a/src/index.jsx b/src/index.jsx index 1d43bbdb..2e8a7df2 100755 --- a/src/index.jsx +++ b/src/index.jsx @@ -65,7 +65,7 @@ initialize({ TOS_AND_HONOR_CODE: process.env.TOS_AND_HONOR_CODE || null, PRIVACY_POLICY: process.env.PRIVACY_POLICY || null, REGISTRATION_OPTIONAL_FIELDS: process.env.REGISTRATION_OPTIONAL_FIELDS || '', - USER_SIGNUP_SURVEY_COOKIE_NAME: process.env.USER_SIGNUP_SURVEY_COOKIE_NAME || null, + USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME || null, COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, }); }, diff --git a/src/login/LoginPage.jsx b/src/login/LoginPage.jsx index 688c2494..ffe9d1f1 100644 --- a/src/login/LoginPage.jsx +++ b/src/login/LoginPage.jsx @@ -33,7 +33,13 @@ import { } from '../data/constants'; import { forgotPasswordResultSelector } from '../forgot-password'; import { - getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, getActivationStatus, windowScrollTo, + getTpaHint, + getTpaProvider, + windowScrollTo, + setSurveyCookie, + getActivationStatus, + getAllPossibleQueryParam, + updatePathWithQueryParams, } from '../data/utils'; class LoginPage extends React.Component { @@ -167,6 +173,10 @@ class LoginPage extends React.Component { ); } + if (this.props.loginResult.success) { + setSurveyCookie('login'); + } + return ( <> diff --git a/src/login/tests/LoginPage.test.jsx b/src/login/tests/LoginPage.test.jsx index 57cf9050..bffff36e 100644 --- a/src/login/tests/LoginPage.test.jsx +++ b/src/login/tests/LoginPage.test.jsx @@ -5,7 +5,7 @@ import { mount } from 'enzyme'; import configureStore from 'redux-mock-store'; import CookiePolicyBanner from '@edx/frontend-component-cookie-policy-banner'; -import { getConfig } from '@edx/frontend-platform'; +import { getConfig, mergeConfig } from '@edx/frontend-platform'; import * as analytics from '@edx/frontend-platform/analytics'; import { IntlProvider, injectIntl } from '@edx/frontend-platform/i18n'; @@ -25,6 +25,10 @@ const IntlLoginPage = injectIntl(LoginPage); const mockStore = configureStore(); describe('LoginPage', () => { + mergeConfig({ + USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME, + }); + const initialState = { forgotPassword: { status: null }, login: { @@ -501,4 +505,19 @@ describe('LoginPage', () => { mount(reduxWrapper()); expect(window.location.href).toEqual(getConfig().LMS_BASE_URL + secondaryProviders.loginUrl); }); + + it('should set login survey cookie', () => { + store = mockStore({ + ...initialState, + login: { + ...initialState.login, + loginResult: { + success: true, + }, + }, + }); + + renderer.create(reduxWrapper()); + expect(document.cookie).toMatch(`${getConfig().USER_SURVEY_COOKIE_NAME}=login`); + }); }); diff --git a/src/register/RegistrationPage.jsx b/src/register/RegistrationPage.jsx index 510f1e50..2834c789 100644 --- a/src/register/RegistrationPage.jsx +++ b/src/register/RegistrationPage.jsx @@ -5,7 +5,6 @@ import { connect } from 'react-redux'; import Skeleton from 'react-loading-skeleton'; import { Helmet } from 'react-helmet'; import PropTypes from 'prop-types'; -import Cookies from 'universal-cookie'; import { getConfig } from '@edx/frontend-platform'; import { sendPageEvent, sendTrackEvent } from '@edx/frontend-platform/analytics'; @@ -33,7 +32,7 @@ import { DEFAULT_STATE, LOGIN_PAGE, PENDING_STATE, REGISTER_PAGE, VALID_EMAIL_REGEX, } from '../data/constants'; import { - getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, + getTpaProvider, getTpaHint, updatePathWithQueryParams, getAllPossibleQueryParam, setSurveyCookie, } from '../data/utils'; class RegistrationPage extends React.Component { @@ -430,15 +429,7 @@ class RegistrationPage extends React.Component { } if (this.props.registrationResult.success) { - const cookieName = getConfig().USER_SIGNUP_SURVEY_COOKIE_NAME; - if (cookieName) { - const cookies = new Cookies(); - const signupTimestamp = (new Date()).getTime(); - // set expiry to exactly 24 hours from now - const cookieExpiry = new Date(signupTimestamp + 1 * 864e5); - const options = { domain: getConfig().COOKIE_DOMAIN, expires: cookieExpiry, path: '/' }; - cookies.set(cookieName, signupTimestamp, options); - } + setSurveyCookie('register'); } return ( diff --git a/src/register/tests/RegistrationPage.test.jsx b/src/register/tests/RegistrationPage.test.jsx index 16f638d5..cf0c5307 100644 --- a/src/register/tests/RegistrationPage.test.jsx +++ b/src/register/tests/RegistrationPage.test.jsx @@ -29,7 +29,7 @@ describe('RegistrationPageTests', () => { PRIVACY_POLICY: 'http://privacy-policy.com', REGISTRATION_OPTIONAL_FIELDS: 'gender,goals,level_of_education,year_of_birth', TOS_AND_HONOR_CODE: 'http://tos-and-honot-code.com', - USER_SIGNUP_SURVEY_COOKIE_NAME: process.env.USER_SIGNUP_SURVEY_COOKIE_NAME, + USER_SURVEY_COOKIE_NAME: process.env.USER_SURVEY_COOKIE_NAME, }); const initialState = { @@ -541,7 +541,7 @@ describe('RegistrationPageTests', () => { expect(window.location.href).toBe(dasboardUrl); }); - it('should set registration timestamp cookie', () => { + it('should set registration survey cookie', () => { store = mockStore({ ...initialState, register: { @@ -553,7 +553,7 @@ describe('RegistrationPageTests', () => { }); renderer.create(reduxWrapper()); - expect(document.cookie).toMatch(getConfig().USER_SIGNUP_SURVEY_COOKIE_NAME); + expect(document.cookie).toMatch(`${getConfig().USER_SURVEY_COOKIE_NAME}=register`); }); it('should display institution register button', () => {