Skip to content

Commit

Permalink
feat(events): add responder and stepId/Index to flow events (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
alikirec authored Sep 19, 2024
1 parent 90a9d3e commit cb10581
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ lib/

yarn-debug.log*
yarn-error.log*
.yarn/
.yarnrc.yml

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
8 changes: 8 additions & 0 deletions packages/constants/src/embed-events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { AnalyticsEventType, WebEmbedMessage, IFlowAnswers } from '../';

export interface IBaseEventData {
answers?: IFlowAnswers;
responder: {
responderUuid: string;
sessionUuid: string;
isReturningResponder?: boolean;
}
}

export interface IRedirectEventData extends IBaseEventData {
Expand All @@ -19,6 +24,9 @@ export interface IIFrameAnalyticsEventData
createdAt: Date;
eventType: AnalyticsEventType;
variantRevisionUuid: string;
// for StepLoaded and StepCompleted events
stepId?: string;
stepIndex?: number;
}

export enum TokenRequestPayload {
Expand Down
22 changes: 12 additions & 10 deletions packages/embed-messaging-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
TokenRequestPayload,
type IIFramePushMessage,
WebEmbedMessage,
IBaseEventData,
IFlowAnswers,
type IBaseEventData,
} from '@formsort/constants';
import {
isIFrameAnalyticsEventData,
Expand All @@ -24,10 +23,7 @@ interface IAuthenticationConfig {
idToken: string;
}

interface IFlowEventPayload {
variantRevisionUuid: string;
answers?: IFlowAnswers;
}
type IFlowEventPayload = Omit<IIFrameAnalyticsEventData, 'type' | 'createdAt' | 'eventType'>;

export interface IFormsortEmbedConfig {
autoHeight?: boolean;
Expand Down Expand Up @@ -140,13 +136,13 @@ class EmbedMessagingManager {
};

private onRedirectMessage = (redirectData: IIFrameRedirectEventData) => {
const { payload: url, answers } = redirectData;
const { payload: url, answers, responder } = redirectData;

if (!isEmpty(this.eventListenersArrayMap.redirect)) {
let cancelRedirect = false;
// Cancel redirect if any of the redirect listeners return `{ cancel: true }`
for (const redirectListener of this.eventListenersArrayMap.redirect) {
const { cancel } = redirectListener({ url, answers }) || {};
const { cancel } = redirectListener({ url, answers, responder }) || {};
if (!cancelRedirect && cancel) {
cancelRedirect = true;
}
Expand Down Expand Up @@ -180,7 +176,7 @@ class EmbedMessagingManager {
};

private onEventMessage = (eventData: IIFrameAnalyticsEventData) => {
const { eventType, answers, variantRevisionUuid } = eventData;
const { eventType, answers, variantRevisionUuid, responder, stepId, stepIndex } = eventData;

if (eventType === AnalyticsEventType.FlowClosed) {
this.onFlowClosed();
Expand All @@ -193,7 +189,13 @@ class EmbedMessagingManager {
}

for (const eventListener of eventListenersArr) {
eventListener({ answers, variantRevisionUuid });
eventListener({
answers,
responder,
variantRevisionUuid,
stepId,
stepIndex,
});
}
};

Expand Down
1 change: 0 additions & 1 deletion packages/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"importHelpers": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"suppressImplicitAnyIndexErrors": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,14 @@
"@formsort/constants" "^1.7.0"
events "^3.2.0"

"@formsort/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@formsort/web-embed-api/-/web-embed-api-2.6.2.tgz#7e5ee7e071af928c3a033d2a86b26a51c26422ae"
integrity sha512-3jUiZggAfFl/6ElSy/s1UXYgh4FI+ZeLVODDzNi5bDK1HpiabmTnHIKg1XsE2wT06n3T+OuoFIvPrL+x2GPTlg==
dependencies:
"@formsort/constants" "^1.10.0"
"@formsort/embed-messaging-manager" "^0.1.1"

"@humanwhocodes/config-array@^0.9.2":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"
Expand Down Expand Up @@ -7785,7 +7793,7 @@ write-file-atomic@^4.0.2:
imurmurhash "^0.1.4"
signal-exit "^3.0.7"

ws@^8.11.0, ws@^8.16.0, ws@^8.17.1:
ws@^8.11.0, ws@^8.16.0:
version "8.17.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b"
integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==
Expand Down

0 comments on commit cb10581

Please sign in to comment.