From 75329e5de1778587ad60d51909422312b208b64e Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Tue, 31 Aug 2021 11:46:15 +0530 Subject: [PATCH] fix: Add missing referer & initiated_at.timestamp to events (#2920) --- app/javascript/widget/api/endPoints.js | 2 ++ app/javascript/widget/api/events.js | 12 +++++++++++- app/javascript/widget/api/specs/endPoints.spec.js | 11 +++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/javascript/widget/api/endPoints.js b/app/javascript/widget/api/endPoints.js index 7a0a3c94f..fddcfa3d6 100755 --- a/app/javascript/widget/api/endPoints.js +++ b/app/javascript/widget/api/endPoints.js @@ -1,4 +1,5 @@ import { buildSearchParamsWithLocale } from '../helpers/urlParamsHelper'; +import { generateEventParams } from './events'; const createConversation = params => { const referrerURL = window.referrerURL || ''; @@ -76,6 +77,7 @@ const triggerCampaign = ({ websiteToken, campaignId }) => ({ name: 'campaign.triggered', event_info: { campaign_id: campaignId, + ...generateEventParams(), }, }, params: { diff --git a/app/javascript/widget/api/events.js b/app/javascript/widget/api/events.js index 0c8fcfb23..b1757ce5d 100644 --- a/app/javascript/widget/api/events.js +++ b/app/javascript/widget/api/events.js @@ -1,9 +1,19 @@ import { API } from 'widget/helpers/axios'; import { buildSearchParamsWithLocale } from '../helpers/urlParamsHelper'; +export const generateEventParams = () => ({ + initiated_at: { + timestamp: new Date().toString(), + }, + referer: window.referrerURL || '', +}); + export default { create(name) { const search = buildSearchParamsWithLocale(window.location.search); - return API.post(`/api/v1/widget/events${search}`, { name }); + return API.post(`/api/v1/widget/events${search}`, { + name, + event_info: generateEventParams(), + }); }, }; diff --git a/app/javascript/widget/api/specs/endPoints.spec.js b/app/javascript/widget/api/specs/endPoints.spec.js index 7e4ad4dd1..21ed98a5a 100644 --- a/app/javascript/widget/api/specs/endPoints.spec.js +++ b/app/javascript/widget/api/specs/endPoints.spec.js @@ -47,6 +47,10 @@ describe('#getConversation', () => { describe('#triggerCampaign', () => { it('should returns correct payload', () => { + const spy = jest.spyOn(global, 'Date').mockImplementation(() => ({ + toString: () => 'mock date', + })); + const windowSpy = jest.spyOn(window, 'window', 'get'); const websiteToken = 'ADSDJ2323MSDSDFMMMASDM'; const campaignId = 12; expect( @@ -60,11 +64,18 @@ describe('#triggerCampaign', () => { name: 'campaign.triggered', event_info: { campaign_id: campaignId, + referer: '', + initiated_at: { + timestamp: 'mock date', + }, }, }, params: { website_token: websiteToken, }, }); + windowSpy.mockRestore(); + + spy.mockRestore(); }); });