All files / modules/10-common/hooks useTelemetry.ts

100% Statements 27/27
95.65% Branches 22/23
100% Functions 5/5
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79              738x 738x   738x 738x 738x 738x                               738x 2357x 2354x 2354x 2354x 2354x 2354x 2354x 2354x   2354x               2354x 872x                 2354x 448x           2354x 16x             2354x 6x 2x   2354x    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { useEffect } from 'react'
import { useParams } from 'react-router-dom'
import type { AccountPathProps } from '@common/interfaces/RouteInterfaces'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { useModuleInfo } from '@common/hooks/useModuleInfo'
import { useLicenseStore } from 'framework/LicenseStore/LicenseStoreContext'
import { useTelemetryInstance } from './useTelemetryInstance'
 
type TrackEvent = (eventName: string, properties: Record<string, unknown>) => void
type TrackPage = (name: string, properties: Record<string, string>, category?: string) => void
type IdentifyUser = (email: string | undefined) => void
interface PageParams {
  pageName?: string
  category?: string
  properties?: Record<string, string>
}
interface TelemetryReturnType {
  trackEvent: TrackEvent
  identifyUser: IdentifyUser
  trackPage: TrackPage
}
 
export function useTelemetry(pageParams: PageParams = {}): TelemetryReturnType {
  const { currentUserInfo } = useAppStore()
  const { accountId: groupId } = useParams<AccountPathProps>()
  const telemetry = useTelemetryInstance()
  const userId = currentUserInfo.email || ''
  const { module } = useModuleInfo()
  const { licenseInformation } = useLicenseStore()
  const moduleLicense = module && licenseInformation[module.toUpperCase()]
  const SOURCE_UI = 'NG UI'
 
  const licenseProperties = module
    ? {
        module,
        licenseEdition: moduleLicense?.edition,
        licenseType: moduleLicense?.licenseType
      }
    : null
 
  useEffect(() => {
    pageParams.pageName &&
      telemetry.page({
        name: pageParams.pageName,
        category: pageParams.category || '',
        properties: { source: SOURCE_UI, userId, groupId, ...licenseProperties, ...pageParams.properties } || {}
      })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [pageParams.pageName, pageParams.category, pageParams.properties])
 
  const trackEvent: TrackEvent = (eventName: string, properties: Record<string, unknown>) => {
    telemetry.track({
      event: eventName,
      properties: { source: SOURCE_UI, userId, groupId, ...licenseProperties, ...properties }
    })
  }
 
  const trackPage: TrackPage = (name: string, properties: Record<string, string>, category?: string) => {
    telemetry.page({
      name: name,
      category: category,
      properties: { source: SOURCE_UI, ...licenseProperties, ...properties }
    })
  }
 
  const identifyUser: IdentifyUser = (email: string | undefined) => {
    if (!email) return
    telemetry.identify(email)
  }
  return { trackEvent, identifyUser, trackPage }
}