Skip to content

Commit

Permalink
Add Use Quote Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Takeda committed Jul 22, 2023
1 parent f1b4c40 commit 6a64cf5
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 31 deletions.
14 changes: 12 additions & 2 deletions react/components/QuoteDetails/QuoteDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -207,7 +209,7 @@ const QuoteDetails: FunctionComponent = () => {
account,
}

sendMetric(metricsParam)
sendCreateQuoteMetric(metricsParam)

toastMessage(quoteMessages.createSuccess)
handleClearCart(orderForm.orderFormId).then(() => {
Expand Down Expand Up @@ -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)
})
Expand Down
43 changes: 14 additions & 29 deletions react/utils/metrics.ts → react/utils/metrics/createQuote.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand All @@ -31,26 +25,15 @@ 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
account: string
sendToSalesRep: boolean
}

type QuoteMetric = Metric & { fields: QuoteFieldsMetric }
type CreateQuoteMetric = Metric & { fields: CreateQuoteFieldsMetric }

const fetchMetricsData = async (
accountName: string,
Expand Down Expand Up @@ -93,9 +76,9 @@ const fetchMetricsData = async (
}
}

const buildQuoteMetric = async (
metricsParam: MetricsParam
): Promise<QuoteMetric> => {
const buildCreateQuoteMetric = async (
metricsParam: CreateQuoteMetricsParam
): Promise<CreateQuoteMetric> => {
const { namespaces } = metricsParam.sessionResponse
const userEmail = namespaces?.profile?.email?.value

Expand All @@ -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',
Expand Down Expand Up @@ -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)
}
Expand Down
37 changes: 37 additions & 0 deletions react/utils/metrics/metrics.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
65 changes: 65 additions & 0 deletions react/utils/metrics/useQuote.ts
Original file line number Diff line number Diff line change
@@ -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<UseQuoteMetric> => {
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)
}
}

0 comments on commit 6a64cf5

Please sign in to comment.