From 6a64cf5b9a422c92296603fa7c28de3a93ee5ed2 Mon Sep 17 00:00:00 2001 From: Mauro Takeda Date: Fri, 21 Jul 2023 21:57:24 -0300 Subject: [PATCH 1/3] Add Use Quote Metrics --- .../components/QuoteDetails/QuoteDetails.tsx | 14 +++- .../{metrics.ts => metrics/createQuote.ts} | 43 ++++-------- react/utils/metrics/metrics.ts | 37 +++++++++++ react/utils/metrics/useQuote.ts | 65 +++++++++++++++++++ 4 files changed, 128 insertions(+), 31 deletions(-) rename react/utils/{metrics.ts => metrics/createQuote.ts} (79%) create mode 100644 react/utils/metrics/metrics.ts create mode 100644 react/utils/metrics/useQuote.ts diff --git a/react/components/QuoteDetails/QuoteDetails.tsx b/react/components/QuoteDetails/QuoteDetails.tsx index 19e1bbe..6432083 100644 --- a/react/components/QuoteDetails/QuoteDetails.tsx +++ b/react/components/QuoteDetails/QuoteDetails.tsx @@ -38,7 +38,9 @@ import AlertMessage from './AlertMessage' import QuoteTable from './QuoteTable' import QuoteUpdateHistory from './QuoteUpdateHistory' import { Status } from '../../utils/status' -import { sendMetric } from '../../utils/metrics' +import { sendCreateQuoteMetric } from '../../utils/metrics/createQuote' +import type { UseQuoteMetricsParams } from '../../utils/metrics/useQuote' +import { sendUseQuoteMetric } from '../../utils/metrics/useQuote' const localStore = storageFactory(() => localStorage) const MAX_DISCOUNT_PERCENTAGE = 99 @@ -207,7 +209,7 @@ const QuoteDetails: FunctionComponent = () => { account, } - sendMetric(metricsParam) + sendCreateQuoteMetric(metricsParam) toastMessage(quoteMessages.createSuccess) handleClearCart(orderForm.orderFormId).then(() => { @@ -305,6 +307,14 @@ const QuoteDetails: FunctionComponent = () => { setUsingQuoteState(false) }) .then(() => { + const metricsParam: UseQuoteMetricsParams = { + quoteState, + orderFormId: variables.orderFormId, + account, + sessionResponse, + } + + sendUseQuoteMetric(metricsParam) goToCheckout(checkoutUrl) setUsingQuoteState(false) }) diff --git a/react/utils/metrics.ts b/react/utils/metrics/createQuote.ts similarity index 79% rename from react/utils/metrics.ts rename to react/utils/metrics/createQuote.ts index 2a87d6d..1db55fe 100644 --- a/react/utils/metrics.ts +++ b/react/utils/metrics/createQuote.ts @@ -1,6 +1,7 @@ import axios from 'axios' -const ANALYTICS_URL = 'https://rc.vtex.com/api/analytics/schemaless-events' +import type { Metric, SessionResponse } from './metrics' +import { sendMetric } from './metrics' const GRAPHQL_URL = (accountName: string, workspace?: string) => { if (workspace) { @@ -10,14 +11,7 @@ const GRAPHQL_URL = (accountName: string, workspace?: string) => { return `https://${accountName}.myvtex.com/_v/private/graphql/v1` } -type Metric = { - name: 'b2b-suite-buyerorg-data' - kind: 'create-quote-ui-event' - description: 'Create Quotation Action - UI' - account: string -} - -type QuoteFieldsMetric = { +type CreateQuoteFieldsMetric = { cost_center_id: string cost_center_name: string buy_org_id: string @@ -31,18 +25,7 @@ type QuoteFieldsMetric = { send_to_sales_rep: boolean } -export type SessionProfile = { - id: { value: string } - email: { value: string } -} - -type SessionResponse = { - namespaces: { - profile: SessionProfile - } -} - -type MetricsParam = { +type CreateQuoteMetricsParam = { quoteId: string sessionResponse: SessionResponse workspace: string @@ -50,7 +33,7 @@ type MetricsParam = { sendToSalesRep: boolean } -type QuoteMetric = Metric & { fields: QuoteFieldsMetric } +type CreateQuoteMetric = Metric & { fields: CreateQuoteFieldsMetric } const fetchMetricsData = async ( accountName: string, @@ -93,9 +76,9 @@ const fetchMetricsData = async ( } } -const buildQuoteMetric = async ( - metricsParam: MetricsParam -): Promise => { +const buildCreateQuoteMetric = async ( + metricsParam: CreateQuoteMetricsParam +): Promise => { const { namespaces } = metricsParam.sessionResponse const userEmail = namespaces?.profile?.email?.value @@ -106,7 +89,7 @@ const buildQuoteMetric = async ( userEmail ) - const metric: QuoteMetric = { + const metric: CreateQuoteMetric = { name: 'b2b-suite-buyerorg-data', kind: 'create-quote-ui-event', description: 'Create Quotation Action - UI', @@ -139,11 +122,13 @@ type QuoteMetricsData = { creationDate: string } -export const sendMetric = async (metricsParam: MetricsParam) => { +export const sendCreateQuoteMetric = async ( + metricsParam: CreateQuoteMetricsParam +) => { try { - const metric = await buildQuoteMetric(metricsParam) + const metric = await buildCreateQuoteMetric(metricsParam) - await axios.post(ANALYTICS_URL, metric) + await sendMetric(metric) } catch (error) { console.warn('Unable to log metrics', error) } diff --git a/react/utils/metrics/metrics.ts b/react/utils/metrics/metrics.ts new file mode 100644 index 0000000..8bc530b --- /dev/null +++ b/react/utils/metrics/metrics.ts @@ -0,0 +1,37 @@ +import axios from 'axios' + +const ANALYTICS_URL = 'https://rc.vtex.com/api/analytics/schemaless-events' + +type CreateQuoteMetric = { + kind: 'create-quote-ui-event' + description: 'Create Quotation Action - UI' +} + +type UseQuoteMetric = { + kind: 'use-quote-ui-event' + description: 'Use Quotation Action - UI' +} + +export type Metric = { + name: 'b2b-suite-buyerorg-data' + account: string +} & (CreateQuoteMetric | UseQuoteMetric) + +export type SessionProfile = { + id: { value: string } + email: { value: string } +} + +export type SessionResponse = { + namespaces: { + profile: SessionProfile + } +} + +export const sendMetric = async (metric: Metric) => { + try { + await axios.post(ANALYTICS_URL, metric) + } catch (error) { + console.warn('Unable to log metrics', error) + } +} diff --git a/react/utils/metrics/useQuote.ts b/react/utils/metrics/useQuote.ts new file mode 100644 index 0000000..de94ca4 --- /dev/null +++ b/react/utils/metrics/useQuote.ts @@ -0,0 +1,65 @@ +import type { Metric, SessionResponse } from './metrics' +import { sendMetric } from './metrics' + +type UseQuoteFieldsMetric = { + quote_id: string + quote_reference_name: string + order_form_id: string + quote_creation_date: string + quote_use_date: string + creator_email: string + user_email: string + cost_center_name: string + buy_org_id: string + buy_org_name: string + quote_last_update: string +} + +type UseQuoteMetric = Metric & { fields: UseQuoteFieldsMetric } + +export type UseQuoteMetricsParams = { + quoteState: Quote + orderFormId: string + account: string + sessionResponse: SessionResponse +} + +const buildUseQuoteMetric = async ( + metricsParam: UseQuoteMetricsParams +): Promise => { + const { quoteState, orderFormId, account, sessionResponse } = metricsParam + + const metric: UseQuoteMetric = { + name: 'b2b-suite-buyerorg-data', + kind: 'use-quote-ui-event', + description: 'Use Quotation Action - UI', + account, + fields: { + buy_org_id: quoteState.organization, + buy_org_name: quoteState.organizationName, + cost_center_name: quoteState.costCenterName, + quote_id: quoteState.id, + quote_reference_name: quoteState.referenceName, + order_form_id: orderFormId, + quote_creation_date: quoteState.creationDate, + quote_use_date: new Date().toISOString(), + creator_email: quoteState.creatorEmail, + user_email: sessionResponse.namespaces?.profile?.email?.value, + quote_last_update: quoteState.lastUpdate, + }, + } + + return metric +} + +export const sendUseQuoteMetric = async ( + metricsParam: UseQuoteMetricsParams +) => { + try { + const metric = await buildUseQuoteMetric(metricsParam) + + sendMetric(metric) + } catch (error) { + console.warn('Unable to log metrics', error) + } +} From dd752605b467951745bd6426e1a28755c74a5137 Mon Sep 17 00:00:00 2001 From: Mauro Takeda Date: Mon, 24 Jul 2023 09:00:57 -0300 Subject: [PATCH 2/3] Add change to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5afd027..211c6ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 + ### Added + - Quotation created metrics sent to Analytics Redshift ++ - Use quote metrics sent to Analytics Redshift ## [1.5.5] - 2023-06-29 From 4f5771e864fbd1699dfb434f942e002298b89d8a Mon Sep 17 00:00:00 2001 From: Mauro Takeda Date: Wed, 26 Jul 2023 10:58:41 -0300 Subject: [PATCH 3/3] Handle async in methods --- react/utils/metrics/useQuote.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/react/utils/metrics/useQuote.ts b/react/utils/metrics/useQuote.ts index de94ca4..9b89e85 100644 --- a/react/utils/metrics/useQuote.ts +++ b/react/utils/metrics/useQuote.ts @@ -24,9 +24,9 @@ export type UseQuoteMetricsParams = { sessionResponse: SessionResponse } -const buildUseQuoteMetric = async ( +const buildUseQuoteMetric = ( metricsParam: UseQuoteMetricsParams -): Promise => { +): UseQuoteMetric => { const { quoteState, orderFormId, account, sessionResponse } = metricsParam const metric: UseQuoteMetric = { @@ -56,9 +56,9 @@ export const sendUseQuoteMetric = async ( metricsParam: UseQuoteMetricsParams ) => { try { - const metric = await buildUseQuoteMetric(metricsParam) + const metric = buildUseQuoteMetric(metricsParam) - sendMetric(metric) + await sendMetric(metric) } catch (error) { console.warn('Unable to log metrics', error) }