Skip to content

Commit 375a554

Browse files
TCA-755 #comment This commit adds the Sprig SDK and creates a wrapper function for surveys. #time 1h
1 parent 0bcf276 commit 375a554

File tree

11 files changed

+82
-37
lines changed

11 files changed

+82
-37
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@datadog/browser-logs": "^4.21.2",
2525
"@heroicons/react": "^1.0.6",
26+
"@sprig-technologies/sprig-browser": "^2.20.1",
2627
"@stripe/react-stripe-js": "1.13.0",
2728
"@stripe/stripe-js": "1.41.0",
2829
"apexcharts": "^3.36.0",

public/index.html

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,6 @@
4646
To begin the development, run `npm start` or `yarn start`.
4747
To create a production bundle, use `npm run build` or `yarn build`.
4848
-->
49-
50-
<script>
51-
(function (l, e, a, p) {
52-
if (window.Sprig) return;
53-
window.Sprig = function () { S._queue.push(arguments) }
54-
var S = window.Sprig; S.appId = a; S._queue = []; window.UserLeap = S;
55-
a = l.createElement('script');
56-
a.async = 1; a.src = e + '?id=' + S.appId;
57-
p = l.getElementsByTagName('script')[0];
58-
p.parentNode.insertBefore(a, p);
59-
})(document, 'https://cdn.sprig.com/shim.js', 'bUcousVQ0-yF');
60-
</script>
61-
6249
</body>
6350

6451
</html>

src-ts/config/environments/environment.default.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export const EnvironmentConfigDefault: EnvironmentConfigModel = {
2020
SERVICE: 'platform-ui',
2121
},
2222
REAUTH_OFFSET: 55,
23+
SPRIG: {
24+
ENVIRONMENT_ID: 'bUcousVQ0-yF',
25+
},
2326
// TODO: Move stripe creds to .env file
2427
STRIPE: {
2528
ADMIN_TOKEN:

src-ts/config/environments/environment.prod.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const EnvironmentConfigProd: EnvironmentConfigModel = {
1818
},
1919
DISABLED_TOOLS: [],
2020
ENV: 'prod',
21+
SPRIG: {
22+
ENVIRONMENT_ID: 'a-IZBZ6-r7bU',
23+
},
2124
// TODO: Move stripe creds to .env file
2225
STRIPE: {
2326
ADMIN_TOKEN:

src-ts/lib/functions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export * from './error-functions'
1010
export * from './file-functions'
1111
export * from './logging-functions'
1212
export * from './text-format-functions'
13+
export * from './survey-functions'
1314
export * from './user-functions'
1415
export * from './xhr-functions'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { triggerForUser as surveyTriggerForUser } from './survey.functions'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { sprig, SprigAPI } from '@sprig-technologies/sprig-browser'
2+
3+
import { EnvironmentConfig } from '../../../config'
4+
5+
const Sprig: SprigAPI = sprig.configure({
6+
environmentId: EnvironmentConfig.SPRIG.ENVIRONMENT_ID,
7+
})
8+
9+
export function triggerForUser(surveyName: string, userId?: number): void {
10+
11+
if (!userId) {
12+
Sprig.track(surveyName)
13+
return
14+
}
15+
16+
Sprig.identifyAndTrack({
17+
anonymousId: '',
18+
eventName: surveyName,
19+
metadata: undefined,
20+
userId: `${userId}`,
21+
})
22+
}

src-ts/lib/global-config.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export interface GlobalConfig {
1616
SERVICE: string
1717
}
1818
REAUTH_OFFSET: number
19+
SPRIG: {
20+
ENVIRONMENT_ID: string
21+
}
1922
STRIPE: {
2023
ADMIN_TOKEN: string
2124
API_KEY: string

src-ts/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
logError,
1919
logInfo,
2020
logInitialize,
21+
surveyTriggerForUser,
2122
textFormatDateLocaleShortString,
2223
textFormatGetSafeString,
2324
textFormatMoneyLocaleString,

src-ts/tools/learn/free-code-camp/FreeCodeCamp.tsx

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
LoadingSpinner,
1818
profileContext,
1919
ProfileContextData,
20+
surveyTriggerForUser,
2021
textFormatGetSafeString,
2122
} from '../../../lib'
2223
import {
@@ -203,43 +204,55 @@ const FreeCodeCamp: FC<{}> = () => {
203204
}, 500)
204205
}
205206
// eslint-disable-next-line react-hooks/exhaustive-deps
206-
}, [])
207+
}, [
208+
certificateProgress,
209+
lesson?.course.certificationId,
210+
lesson?.course.id,
211+
profile?.userId,
212+
])
207213

208214
const handleFccLessonComplete: (challengeUuid: string) => void = useCallback((challengeUuid: string) => {
215+
209216
const currentLesson: { [key: string]: string } = {
210217
lesson: lessonParam,
211218
module: moduleParam,
212219
uuid: challengeUuid,
213220
}
214-
if (certificateProgress) {
215-
userCertificationProgressUpdateAsync(
216-
certificateProgress.id,
217-
UserCertificationUpdateProgressActions.completeLesson,
218-
currentLesson,
219-
)
220-
.then((progress: LearnUserCertificationProgress) => {
221221

222-
setCertificateProgress(progress)
222+
if (!certificateProgress) {
223+
return
224+
}
223225

224-
// if this is the last lesson of the first module, show the survey
225-
const firstModule: LearnModuleProgress = progress.modules[0]
226+
userCertificationProgressUpdateAsync(
227+
certificateProgress.id,
228+
UserCertificationUpdateProgressActions.completeLesson,
229+
currentLesson,
230+
)
231+
.then((progress: LearnUserCertificationProgress) => {
226232

227-
if (moduleParam === firstModule.module
228-
&& firstModule.moduleStatus === LearnModuleStatus.completed) {
233+
setCertificateProgress(progress)
229234

230-
// TODO: use the Sprig SDK to send the event w/the user
231-
window.Sprig('track', 'TCA First Module Completed')
232-
}
233-
})
234-
}
235+
// if this is the last lesson of the first module, show the survey
236+
const firstModule: LearnModuleProgress = progress.modules[0]
237+
if (moduleParam === firstModule.module
238+
&& firstModule.moduleStatus === LearnModuleStatus.completed) {
239+
240+
surveyTriggerForUser('TCA First Module Completed', profile?.userId)
241+
}
242+
})
235243
// eslint-disable-next-line react-hooks/exhaustive-deps
236-
}, [])
244+
}, [
245+
certificateProgress,
246+
lessonParam,
247+
moduleParam,
248+
])
237249

238250
/**
239251
* Handle the navigation away from the last step of the course in the FCC frame
240252
* @returns
241253
*/
242254
const handleFccLastLessonNavigation: () => void = useCallback(() => {
255+
243256
if (!certificateProgress) {
244257
return
245258
}
@@ -284,7 +297,12 @@ const FreeCodeCamp: FC<{}> = () => {
284297

285298
navigate(nextLessonPath)
286299
// eslint-disable-next-line react-hooks/exhaustive-deps
287-
}, [])
300+
}, [
301+
certificateProgress,
302+
certificationParam,
303+
courseData?.modules,
304+
providerParam,
305+
])
288306

289307
useEffect(() => {
290308

@@ -315,13 +333,13 @@ const FreeCodeCamp: FC<{}> = () => {
315333
)
316334
navigate(completedPath)
317335
})
336+
// eslint-disable-next-line react-hooks/exhaustive-deps
318337
}, [
319338
certificateProgress,
320339
certificationParam,
321-
navigate,
322-
providerParam,
323340
profile?.handle,
324-
setCertificateProgress,
341+
profile?.userId,
342+
providerParam,
325343
])
326344

327345
useEffect(() => {
@@ -341,13 +359,13 @@ const FreeCodeCamp: FC<{}> = () => {
341359
navigate(lessonPath)
342360
}
343361
}
362+
// eslint-disable-next-line react-hooks/exhaustive-deps
344363
}, [
345364
certificationParam,
346365
courseData,
347366
courseDataReady,
348367
lessonParam,
349368
moduleParam,
350-
navigate,
351369
providerParam,
352370
])
353371

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,11 @@
20702070
dependencies:
20712071
"@sinonjs/commons" "^1.7.0"
20722072

2073+
"@sprig-technologies/sprig-browser@^2.20.1":
2074+
version "2.20.1"
2075+
resolved "https://registry.yarnpkg.com/@sprig-technologies/sprig-browser/-/sprig-browser-2.20.1.tgz#4c655052dc02514370e92886724cc740337b7d16"
2076+
integrity sha512-Qkt1yEhSz9FIXpDMjMzWklscF0HmKCmLebYDvVpJxqbEeYzCEQjzeD4WW5RTpvCgyuwzXT0At4Xx9UijCWAS8Q==
2077+
20732078
"@stripe/react-stripe-js@1.13.0":
20742079
version "1.13.0"
20752080
resolved "https://registry.yarnpkg.com/@stripe/react-stripe-js/-/react-stripe-js-1.13.0.tgz#8e0bde2b116870d2aca52976f46a92d81983e235"

0 commit comments

Comments
 (0)