generated from vtex-apps/admin-example
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from vtex-apps/B2BTEAM-1261
Impersonate Metrics
- Loading branch information
Showing
11 changed files
with
294 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { Session } from '../../modules/session' | ||
import type { Metric } from './metrics' | ||
import { sendMetric } from './metrics' | ||
|
||
type ChangeTeamFieldsMetric = { | ||
date: string | ||
user_role: string | ||
user_email?: string | ||
org_id: string | ||
cost_center_id: string | ||
} | ||
|
||
type ChangeTeamMetric = Metric & { fields: ChangeTeamFieldsMetric } | ||
|
||
export type StopImpersonateMetricParams = { | ||
sessionResponse: Session | ||
currentCostCenter: string | ||
costCenterInput: string | ||
currentOrganization: string | ||
organizationInput: string | ||
email: string | ||
} | ||
|
||
export type ChangeTeamParams = { | ||
sessionResponse: Session | ||
currentRoleName: string | ||
currentOrganization: string | ||
currentCostCenter: string | ||
} | ||
|
||
const buildMetric = (metricParams: ChangeTeamParams): ChangeTeamMetric => { | ||
return { | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
account: | ||
metricParams.sessionResponse?.namespaces?.account?.accountName?.value, | ||
kind: 'change-team-ui-event', | ||
description: 'User change team/organization - UI', | ||
fields: { | ||
date: new Date().toISOString(), | ||
user_role: metricParams.currentRoleName, | ||
user_email: | ||
metricParams.sessionResponse?.namespaces?.profile?.email?.value, | ||
org_id: metricParams.currentOrganization, | ||
cost_center_id: metricParams.currentCostCenter, | ||
}, | ||
} | ||
} | ||
|
||
export const sendChangeTeamMetric = async (metricParams: ChangeTeamParams) => { | ||
try { | ||
const metric: ChangeTeamMetric = buildMetric(metricParams) | ||
|
||
await sendMetric(metric) | ||
} catch (error) { | ||
console.warn('Unable to log metrics', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import type { B2BUserSimple } from '../../components/OrganizationUsersTable' | ||
import type { Session } from '../../modules/session' | ||
import { getSession } from '../../modules/session' | ||
import type { Metric } from './metrics' | ||
import { sendMetric } from './metrics' | ||
|
||
type ImpersonatePerson = { | ||
email: string | ||
buyer_org_id: string | ||
cost_center_id: string | ||
cost_center_name: string | ||
} | ||
|
||
type ImpersonateUser = ImpersonatePerson | ||
type ImpersonateTarget = ImpersonatePerson & { buyer_org_name: string } | ||
|
||
type ImpersonateFieldsMetric = { | ||
user: ImpersonateUser | ||
target: ImpersonateTarget | ||
date: string | ||
} | ||
|
||
type ImpersonateMetric = Metric & { fields: ImpersonateFieldsMetric } | ||
|
||
export type ImpersonateMetricParams = { | ||
costCenterData: { | ||
getCostCenterByIdStorefront: { | ||
id: string | ||
name: string | ||
organization: string | ||
} | ||
} | ||
target: B2BUserSimple | ||
} | ||
|
||
export type StopImpersonateMetricParams = { | ||
sessionResponse: Session | ||
currentCostCenter: string | ||
costCenterInput: string | ||
currentOrganization: string | ||
organizationInput: string | ||
email: string | ||
} | ||
|
||
const buildImpersonateMetric = async ( | ||
metricParams: ImpersonateMetricParams | ||
) => { | ||
const { target, costCenterData } = metricParams | ||
|
||
const session = await getSession() | ||
const sessionResponse = session?.response | ||
const isSession = | ||
sessionResponse && | ||
(!sessionResponse.type || sessionResponse.type === 'Session') | ||
|
||
return { | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
kind: 'impersonate-ui-event', | ||
description: 'Impersonate User Action - UI', | ||
account: isSession | ||
? (sessionResponse as Session).namespaces?.account?.accountName?.value | ||
: undefined, | ||
fields: { | ||
user: { | ||
email: isSession | ||
? (sessionResponse as Session).namespaces?.profile?.email?.value | ||
: undefined, | ||
buyer_org_id: costCenterData?.getCostCenterByIdStorefront.organization, | ||
cost_center_id: costCenterData?.getCostCenterByIdStorefront.id, | ||
cost_center_name: costCenterData?.getCostCenterByIdStorefront.name, | ||
}, | ||
target: { | ||
email: target.email, | ||
buyer_org_id: target.orgId, | ||
buyer_org_name: target.organizationName, | ||
cost_center_id: target.costId, | ||
cost_center_name: target.costCenterName, | ||
}, | ||
date: new Date().toISOString(), | ||
}, | ||
} as ImpersonateMetric | ||
} | ||
|
||
const buildStopImpersonateMetric = ( | ||
metricParams: StopImpersonateMetricParams | ||
) => { | ||
return { | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
kind: 'stop-impersonate-ui-event', | ||
description: 'Stop Impersonate User Action - UI', | ||
account: | ||
metricParams.sessionResponse?.namespaces?.account?.accountName?.value, | ||
fields: { | ||
user: { | ||
email: | ||
metricParams.sessionResponse?.namespaces?.authentication | ||
?.storeUserEmail?.value, | ||
}, | ||
target: { | ||
email: metricParams.email, | ||
buyer_org_id: metricParams.currentOrganization, | ||
buyer_org_name: metricParams.organizationInput, | ||
cost_center_id: metricParams.currentCostCenter, | ||
cost_center_name: metricParams.costCenterInput, | ||
}, | ||
date: new Date().toISOString(), | ||
}, | ||
} as ImpersonateMetric | ||
} | ||
|
||
export const sendImpersonateMetric = async ( | ||
metricParams: ImpersonateMetricParams | ||
) => { | ||
try { | ||
const metric = await buildImpersonateMetric(metricParams) | ||
|
||
await sendMetric(metric) | ||
} catch (error) { | ||
console.warn('Unable to log metrics', error) | ||
} | ||
} | ||
|
||
export const sendStopImpersonateMetric = async ( | ||
metricParams: StopImpersonateMetricParams | ||
) => { | ||
try { | ||
const metric = buildStopImpersonateMetric(metricParams) | ||
|
||
await sendMetric(metric) | ||
} catch (error) { | ||
console.warn('Unable to log metrics', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import axios from 'axios' | ||
|
||
const ANALYTICS_URL = 'https://rc.vtex.com/api/analytics/schemaless-events' | ||
|
||
type ImpersonateMetric = { | ||
kind: 'impersonate-ui-event' | ||
description: 'Impersonate User Action - UI' | ||
} | ||
|
||
type StopImpersonateMetric = { | ||
kind: 'stop-impersonate-ui-event' | ||
description: 'Stop Impersonate User Action - UI' | ||
} | ||
|
||
export type ChangeTeamMetric = { | ||
kind: 'change-team-ui-event' | ||
description: 'User change team/organization - UI' | ||
} | ||
|
||
export type Metric = { | ||
name: 'b2b-suite-buyerorg-data' | ||
account: string | ||
} & (ImpersonateMetric | StopImpersonateMetric | ChangeTeamMetric) | ||
|
||
export const sendMetric = async (metric: Metric) => { | ||
try { | ||
await axios.post(ANALYTICS_URL, metric) | ||
} catch (error) { | ||
console.warn('Unable to log metrics', error) | ||
} | ||
} |
Oops, something went wrong.