Skip to content

Commit

Permalink
change in embedded api params
Browse files Browse the repository at this point in the history
  • Loading branch information
hani-iterable committed Dec 22, 2023
1 parent eb9d23e commit d583ba4
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 35 deletions.
23 changes: 13 additions & 10 deletions react-example/src/components/EmbeddedForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ export const EmbeddedForm: FC<Props> = ({
const [messageId, setMessageId] = useState<string>('');

const [isTrackingEvent, setTrackingEvent] = useState<boolean>(false);
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

const handleFetchEmbeddedMessages = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();

try {
await new EmbeddedManager().syncMessages(
userId,
userId,
emailRegex.test(userId) ? '' : userId,
emailRegex.test(userId) ? userId : '',
'Web',
'1',
'my-website',
() => console.log('Synced message'),
[9]
() => console.log('Synced message')
);
} catch (error: any) {
setTrackResponse(JSON.stringify(error.response.data));
Expand All @@ -69,7 +69,8 @@ export const EmbeddedForm: FC<Props> = ({
setTrackingEvent(true);

const receivedMessage = {
userId: userId,
email: emailRegex.test(userId) ? userId : undefined,
userId: emailRegex.test(userId) ? undefined : userId,
messageId: messageId,
deviceInfo: { appPackageName: 'my-lil-site' },
createdAt: 1627060811283
Expand Down Expand Up @@ -102,12 +103,13 @@ export const EmbeddedForm: FC<Props> = ({
const appPackageName = 'my-lil-site';

trackEmbeddedMessageClick(
userId,
payload,
buttonIdentifier,
clickedUrl,
appPackageName,
1627060811283
1627060811283,
emailRegex.test(userId) ? undefined : userId,
emailRegex.test(userId) ? userId : undefined
)
.then((response) => {
setTrackResponse(JSON.stringify(response.data));
Expand All @@ -126,8 +128,8 @@ export const EmbeddedForm: FC<Props> = ({
setTrackingEvent(true);

const sessionData = {
email: userId,
userId: userId,
email: emailRegex.test(userId) ? userId : undefined,
userId: emailRegex.test(userId) ? undefined : userId,
messageId: messageId,
buttonIdentifier: '123',
deviceInfo: {
Expand All @@ -154,7 +156,8 @@ export const EmbeddedForm: FC<Props> = ({
setTrackingEvent(true);

const sessionData = {
userId: userId,
userId: emailRegex.test(userId) ? undefined : userId,
email: emailRegex.test(userId) ? userId : undefined,
session: {
id: 'abcd123',
start: 1701753762,
Expand Down
5 changes: 3 additions & 2 deletions react-example/src/views/EmbeddedMsgs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const EmbeddedMsgs: FC<Props> = () => {
const [selectedButtonIndex, setSelectedButtonIndex] = useState(0);
const [userId, setUserId] = useState<string>();
const [messages, setMessages] = useState([]);
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

useEffect(() => {
initialize(process.env.API_KEY);
Expand All @@ -24,8 +25,8 @@ export const EmbeddedMsgs: FC<Props> = () => {
try {
const embeddedManager = new EmbeddedManager();
await embeddedManager.syncMessages(
userId,
userId,
emailRegex.test(userId) ? '' : userId,
emailRegex.test(userId) ? userId : '',
'Web',
'1',
'my-website',
Expand Down
33 changes: 27 additions & 6 deletions src/embedded/embeddedManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,38 @@ export class EmbeddedManager {
placementIds: number[]
) {
try {
let url = `${embedded_msg_endpoint}?userId=${userId}`;
url += `&email=${email}`;
url += `&platform=${platform}`;
let url = `${embedded_msg_endpoint}?`;

if (userId.trim() !== '') {
url += `userId=${userId}&`;
}

if (email.trim() !== '') {
url += `email=${email}&`;
}

url += `platform=${platform}`;
url += `&sdkVersion=${sdkVersion}`;
url += `&packageName=${packageName}`;
url += placementIds.map((id) => `&placementIds=${id}`).join('');

if (placementIds.length > 0) {
url += placementIds.map((id) => `&placementIds=${id}`).join('');
}
url = url.replace(/&$/, '');

const iterableResult: any = await baseIterableRequest<IterableResponse>({
method: 'GET',
url: url
});
if (iterableResult?.data?.placements[0]?.embeddedMessages?.length) {
const processor = new EmbeddedMessagingProcessor(
[...this.messages],
iterableResult?.data?.placements[0]?.embeddedMessages
this.getEmbeddedMessages(iterableResult?.data?.placements)
);
this.setMessages(processor);
await this.trackNewlyRetrieved(processor);
this.messages = [
...iterableResult?.data?.placements[0]?.embeddedMessages
...this.getEmbeddedMessages(iterableResult?.data?.placements)
];
}
} catch (error: any) {
Expand All @@ -77,6 +90,14 @@ export class EmbeddedManager {
}
}

private getEmbeddedMessages(placements: any): IEmbeddedMessage[] {
let messages: IEmbeddedMessage[] = [];
placements.forEach((placement: any) => {
messages = [...messages, ...placement.embeddedMessages];
});
return messages;
}

private setMessages(_processor: EmbeddedMessagingProcessor) {
this.messages = _processor.processedMessagesList();
}
Expand Down
3 changes: 2 additions & 1 deletion src/events/embedded/events.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export const embaddedMessagingDismissSchema = object().shape({
});

export const embaddedMessagingSessionSchema = object().shape({
userId: string().required(),
email: string(),
userId: string(),
session: object()
.shape({
id: string().required(),
Expand Down
7 changes: 4 additions & 3 deletions src/events/embedded/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export interface DeviceInfo {
}

export interface EnbeddedMessagingDismiss {
email: string;
userId: string;
email?: string;
userId?: string;
messageId: string;
buttonIdentifier: string;
deviceInfo: DeviceInfo;
Expand All @@ -85,7 +85,8 @@ export interface Impression {
}

export interface EnbeddedMessagingSession {
userId: string;
userId?: string;
email?: string;
session: Session;
impressions: Array<Impression>;
deviceInfo: DeviceInfo;
Expand Down
6 changes: 4 additions & 2 deletions src/events/events.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export const trackEmbeddedMessageSchema = object().shape({
});

export const trackEmbeddedMessageClickSchema = object().shape({
userId: string().required(),
email: string(),
userId: string(),
messageId: string().required(),
buttonIdentifier: string(),
targetUrl: string(),
Expand Down Expand Up @@ -127,7 +128,8 @@ export const embaddedMessagingDismissSchema = object().shape({
});

export const embaddedMessagingSessionSchema = object().shape({
userId: string().required(),
email: string(),
userId: string(),
session: object()
.shape({
id: string().required(),
Expand Down
12 changes: 6 additions & 6 deletions src/events/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ describe('Events Requests', () => {
const clickedUrl = 'https://example.com';
const appPackageName = 'my-lil-site';
const response = await trackEmbeddedMessageClick(
'abc123',
payload,
buttonIdentifier,
clickedUrl,
appPackageName,
0
0,
'abc123'
);

expect(JSON.parse(response.config.data).messageId).toBe('abc123');
Expand All @@ -309,15 +309,15 @@ describe('Events Requests', () => {
it('should reject embedded message click on bad params', async () => {
try {
await trackEmbeddedMessageClick(
'abc123',
{
messageId: 'abc123',
campaignId: 1
} as any,
'',
'',
'',
0
0,
'abc123'
);
} catch (e: any) {
expect(e).toEqual(
Expand Down Expand Up @@ -424,15 +424,15 @@ describe('Events Requests', () => {
deviceInfo: { appPackageName: 'my-lil-site' }
});
const trackEmClickResponse = await trackEmbeddedMessageClick(
'abc123',
{
messageId: 'abc123',
campaignId: 1
},
'button-123',
'https://example.com',
'my-lil-site',
0
0,
'abc123'
);
const trackSessionResponse = await trackEmbeddedSession({
session: {
Expand Down
6 changes: 4 additions & 2 deletions src/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ export const trackEmbeddedMessageReceived = (payload: IEmbeddedMessage) => {
};

export const trackEmbeddedMessageClick = (
userId: string,
payload: IEmbeddedMessageMetadata,
buttonIdentifier: string,
clickedUrl: string,
appPackageName: string,
createdAt: number
createdAt: number,
userId?: string,
email?: string
) => {
return baseIterableRequest<IterableResponse>({
method: 'POST',
url: '/embedded-messaging/events/click',
data: {
userId: userId,
email: email,
messageId: payload.messageId,
buttonIdentifier: buttonIdentifier,
targetUrl: clickedUrl,
Expand Down
7 changes: 4 additions & 3 deletions src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export interface DeviceInfo {
}

export interface EnbeddedMessagingDismiss {
email: string;
userId: string;
userId?: string;
email?: string;
messageId: string;
buttonIdentifier: string;
deviceInfo: DeviceInfo;
Expand All @@ -85,7 +85,8 @@ export interface Impression {
}

export interface EnbeddedMessagingSession {
userId: string;
userId?: string;
email?: string;
session: Session;
impressions: Array<Impression>;
deviceInfo: DeviceInfo;
Expand Down

0 comments on commit d583ba4

Please sign in to comment.