# All files / src/_hooks Analytics.tsx

50% Statements 9/18
0% Branches 0/7
0% Functions 0/4
47.05% Lines 8/17

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
    681x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

     import React from 'react'
    import { Analytics as BaseAnalytics } from '../_utils/genericAnalyticsClient'

    type AnalyticsProviderProps = React.PropsWithChildren<{
      analytics?: BaseAnalytics
    }>
    type HydratedContextValue = Pick<BaseAnalytics, 'trackEvent'>
    type DefaultContextValue = {}
    type ContextValue = HydratedContextValue | DefaultContextValue

    const defaultTrackEvent: BaseAnalytics['trackEvent'] = (e, params) => {
      console.log('analytics::trackEvent', e, params)
    }

    /**
     * @deprecatedSince 11
     * @deprecated
     */
    export const AnalyticsContext = React.createContext<ContextValue>({})

    /**
     * @deprecatedSince 11
     * @deprecated
     */
    export function AnalyticsProvider({
      analytics,
      children,
    }: AnalyticsProviderProps) {
      return (
        <AnalyticsContext.Provider
          value={{ trackEvent: analytics?.trackEvent || defaultTrackEvent }}
        >
          {children}
        </AnalyticsContext.Provider>
      )
    }

    const isAnalyticsHydrated = (
      analytics: ContextValue
    ): analytics is HydratedContextValue => {
      return Object.keys(analytics).length > 0
    }

    /**
     * @deprecatedSince 11
     * @deprecated
     */
    export const useAnalyticsContext = () => {
      const context = React.useContext(AnalyticsContext)
      Iif (!isAnalyticsHydrated(context)) {
        throw new Error('Must wrap app in Analytics.Provider')
      }
      return context
    }

    /**
     * @deprecatedSince 11
     * @deprecated Use fetch instead. Previous implementation:
     * - https://github.com/procore/core/blob/%40procore/core-react%4010.30.0/packages/core-react/src/decorators/Analytics.tsx
     * - https://github.com/procore/core/blob/%40procore/core-react%4010.30.0/packages/core-react/src/utils/genericAnalyticsClient.ts
     */
    export const Analytics = {
      Client: BaseAnalytics,
      Consumer: AnalyticsContext.Consumer,
      Provider: AnalyticsProvider,
      Context: AnalyticsContext,
    }