-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracking): wogaa tracking (#7123)
* feat: add wogaa controller * feat: add tracking to view, feedback, submit * feat: add wogaa config * feat: add pageUrl to wogaa submit * chore: clean up * chore: add logs
- Loading branch information
Showing
8 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import convict, { Schema } from 'convict' | ||
|
||
export interface IWogaa { | ||
wogaaSecretKey: string | ||
wogaaStartEndpoint: string | ||
wogaaSubmitEndpoint: string | ||
wogaaFeedbackEndpoint: string | ||
} | ||
|
||
const wogaaSchema: Schema<IWogaa> = { | ||
wogaaSecretKey: { | ||
doc: 'Wogaa shared secret key', | ||
format: String, | ||
default: '', | ||
env: 'WOGAA_SECRET_KEY', | ||
}, | ||
wogaaStartEndpoint: { | ||
doc: 'Wogaa endpoint when a form is loaded', | ||
format: String, | ||
default: '', | ||
env: 'WOGAA_START_ENDPOINT', | ||
}, | ||
wogaaSubmitEndpoint: { | ||
doc: 'Wogaa endpoint when a form is loaded', | ||
format: String, | ||
default: '', | ||
env: 'WOGAA_SUBMIT_ENDPOINT', | ||
}, | ||
wogaaFeedbackEndpoint: { | ||
doc: 'Wogaa endpoint when a form is loaded', | ||
format: String, | ||
default: '', | ||
env: 'WOGAA_FEEDBACK_ENDPOINT', | ||
}, | ||
} | ||
|
||
export const wogaaConfig = convict(wogaaSchema) | ||
.validate({ allowed: 'strict' }) | ||
.getProperties() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import Axios from 'axios' | ||
import * as crypto from 'crypto' | ||
import uuidGen from 'uuid-by-string' | ||
|
||
import { wogaaConfig } from '../../config/features/wogaa' | ||
import { createLoggerWithLabel } from '../../config/logger' | ||
import { ControllerHandler } from '../core/core.types' | ||
|
||
const logger = createLoggerWithLabel(module) | ||
const generateSignature = (payload: Record<string, unknown>) => { | ||
const signature = crypto | ||
.createHmac('sha256', wogaaConfig.wogaaSecretKey) | ||
.update(JSON.stringify(payload)) | ||
.digest('hex') | ||
return signature | ||
} | ||
|
||
const isConfigValid = () => { | ||
if (!wogaaConfig.wogaaSecretKey) { | ||
return false | ||
} | ||
if (!wogaaConfig.wogaaStartEndpoint) { | ||
return false | ||
} | ||
if (!wogaaConfig.wogaaSubmitEndpoint) { | ||
return false | ||
} | ||
if (!wogaaConfig.wogaaFeedbackEndpoint) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
export const handleSubmit: ControllerHandler<{ formId: string }> = async ( | ||
req, | ||
_, | ||
next, | ||
) => { | ||
const { formId } = req.params | ||
|
||
if (!req.sessionID || !formId || !isConfigValid()) { | ||
return next() | ||
} | ||
|
||
const logMeta = { | ||
action: 'wogaaHandleSubmit', | ||
formId, | ||
} | ||
|
||
const payload = { | ||
formSgId: formId, | ||
transactionId: uuidGen(req.sessionID), | ||
} | ||
// fire and forget | ||
void Axios.post(wogaaConfig.wogaaSubmitEndpoint, payload, { | ||
headers: { | ||
'WOGAA-Signature': generateSignature(payload), | ||
}, | ||
}) | ||
.then(() => { | ||
logger.info({ | ||
message: 'Successfully sent WOGAA submit endpoint', | ||
meta: logMeta, | ||
}) | ||
}) | ||
.catch((e) => { | ||
logger.warn({ | ||
message: 'Error sending to WOGAA submit endpoint', | ||
meta: { ...logMeta, wogaaRespError: e }, | ||
}) | ||
}) | ||
|
||
return next() | ||
} | ||
|
||
export const handleFormView: ControllerHandler<{ formId: string }> = async ( | ||
req, | ||
_, | ||
next, | ||
) => { | ||
const { formId } = req.params | ||
|
||
if (!req.sessionID || !formId || !isConfigValid()) { | ||
return next() | ||
} | ||
|
||
const logMeta = { | ||
action: 'wogaaHandleFormView', | ||
formId, | ||
} | ||
const payload = { | ||
formSgId: formId, | ||
pageUrl: formId, | ||
transactionId: uuidGen(req.sessionID), | ||
} | ||
void Axios.post(wogaaConfig.wogaaStartEndpoint, payload, { | ||
headers: { | ||
'WOGAA-Signature': generateSignature(payload), | ||
}, | ||
}) | ||
.then(() => { | ||
logger.info({ | ||
message: 'Successfully sent WOGAA load form endpoint', | ||
meta: logMeta, | ||
}) | ||
}) | ||
.catch((e) => { | ||
logger.warn({ | ||
message: 'Error sending to WOGAA load form endpoint', | ||
meta: { ...logMeta, wogaaRespError: e }, | ||
}) | ||
}) | ||
|
||
return next() | ||
} | ||
|
||
export const handleFormFeedback: ControllerHandler< | ||
{ formId: string }, | ||
unknown, | ||
{ rating: number; comment: string } | ||
> = async (req, _, next) => { | ||
const { formId } = req.params | ||
const { rating, comment } = req.body | ||
|
||
if (!req.sessionID || !formId || !isConfigValid()) { | ||
return next() | ||
} | ||
|
||
const logMeta = { | ||
action: 'wogaaHandleFormFeedback', | ||
formId, | ||
} | ||
const payload = { | ||
formSgId: formId, | ||
transactionId: uuidGen(req.sessionID), | ||
rating, | ||
comment, | ||
} | ||
|
||
void Axios.post(wogaaConfig.wogaaFeedbackEndpoint, payload, { | ||
headers: { | ||
'WOGAA-Signature': generateSignature(payload), | ||
}, | ||
}) | ||
.then(() => { | ||
logger.info({ | ||
message: 'Successfully sent WOGAA form feedback endpoint', | ||
meta: logMeta, | ||
}) | ||
}) | ||
.catch((e) => { | ||
logger.warn({ | ||
message: 'Error sending to WOGAA form feedback endpoint', | ||
meta: { ...logMeta, wogaaRespError: e }, | ||
}) | ||
}) | ||
|
||
return next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters