Skip to content

Commit

Permalink
Merge pull request #56 from vtex-apps/B2BTEAM-1558
Browse files Browse the repository at this point in the history
Use Quote Metric
  • Loading branch information
Mauro Takeda authored Aug 3, 2023
2 parents f1b4c40 + 4f5771e commit 6e8e773
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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 = (
metricsParam: UseQuoteMetricsParams
): 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 = buildUseQuoteMetric(metricsParam)

await sendMetric(metric)
} catch (error) {
console.warn('Unable to log metrics', error)
}
}

0 comments on commit 6e8e773

Please sign in to comment.