@hanzo/insights-react 1.8.1 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +40 -40
- package/dist/esm/index.js +48 -48
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/surveys/index.js +12 -12
- package/dist/esm/surveys/index.js.map +1 -1
- package/dist/types/index.d.ts +29 -29
- package/dist/umd/index.js +55 -55
- package/dist/umd/index.js.map +1 -1
- package/dist/umd/surveys/index.js +16 -16
- package/dist/umd/surveys/index.js.map +1 -1
- package/package.json +38 -32
- package/src/components/{PostHogCaptureOnViewed.tsx → InsightsCaptureOnViewed.tsx} +12 -37
- package/src/components/{PostHogErrorBoundary.tsx → InsightsErrorBoundary.tsx} +13 -13
- package/src/components/{PostHogFeature.tsx → InsightsFeature.tsx} +14 -14
- package/src/components/__tests__/{PostHogCaptureOnViewed.test.tsx → InsightsCaptureOnViewed.test.tsx} +33 -33
- package/src/components/__tests__/{PostHogErrorBoundary.test.tsx → InsightsErrorBoundary.test.tsx} +17 -17
- package/src/components/__tests__/{PostHogFeature.test.tsx → InsightsFeature.test.tsx} +73 -73
- package/src/components/index.ts +6 -6
- package/src/context/InsightsContext.ts +9 -0
- package/src/context/InsightsProvider.tsx +108 -0
- package/src/context/__tests__/{PostHogContext.test.tsx → InsightsContext.test.tsx} +9 -9
- package/src/context/__tests__/{PostHogProvider.test.tsx → InsightsProvider.test.tsx} +33 -33
- package/src/context/index.ts +2 -2
- package/src/helpers/error-helpers.ts +2 -2
- package/src/hooks/__tests__/featureFlags.test.tsx +15 -15
- package/src/hooks/__tests__/useInsights.test.tsx +19 -0
- package/src/hooks/__tests__/useThumbSurvey.test.tsx +7 -7
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useActiveFeatureFlags.ts +2 -2
- package/src/hooks/useFeatureFlagEnabled.ts +2 -2
- package/src/hooks/useFeatureFlagPayload.ts +2 -2
- package/src/hooks/useFeatureFlagResult.ts +2 -2
- package/src/hooks/useFeatureFlagVariantKey.ts +2 -2
- package/src/hooks/useInsights.ts +7 -0
- package/src/hooks/useThumbSurvey.ts +13 -13
- package/src/utils/type-utils.ts +2 -2
- package/surveys/package.json +1 -1
- package/src/context/PostHogContext.ts +0 -9
- package/src/context/PostHogProvider.tsx +0 -136
- package/src/hooks/__tests__/usePostHog.test.tsx +0 -19
- package/src/hooks/usePostHog.ts +0 -7
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import posthogJs, { PostHogConfig } from '@hanzo/insights'
|
|
3
|
-
|
|
4
|
-
import React, { useEffect, useMemo, useRef } from 'react'
|
|
5
|
-
import { PostHog, PostHogContext } from './PostHogContext'
|
|
6
|
-
import { isDeepEqual } from '../utils/object-utils'
|
|
7
|
-
|
|
8
|
-
interface PreviousInitialization {
|
|
9
|
-
apiKey: string
|
|
10
|
-
options: Partial<PostHogConfig>
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
type WithOptionalChildren<T> = T & { children?: React.ReactNode | undefined }
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Props for the PostHogProvider component.
|
|
17
|
-
* This is a discriminated union type that ensures mutually exclusive props:
|
|
18
|
-
*
|
|
19
|
-
* - If `client` is provided, `apiKey` and `options` must not be provided
|
|
20
|
-
* - If `apiKey` is provided, `client` must not be provided, and `options` is optional
|
|
21
|
-
*/
|
|
22
|
-
type PostHogProviderProps =
|
|
23
|
-
| { client: PostHog; apiKey?: never; options?: never }
|
|
24
|
-
| { apiKey: string; options?: Partial<PostHogConfig>; client?: never }
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* PostHogProvider is a React context provider for PostHog analytics.
|
|
28
|
-
* It can be initialized in two mutually exclusive ways:
|
|
29
|
-
*
|
|
30
|
-
* 1. By providing an existing PostHog `client` instance
|
|
31
|
-
* 2. By providing an `apiKey` (and optionally `options`) to create a new client
|
|
32
|
-
*
|
|
33
|
-
* These initialization methods are mutually exclusive - you must use one or the other,
|
|
34
|
-
* but not both simultaneously.
|
|
35
|
-
*
|
|
36
|
-
* We strongly suggest you memoize the `options` object to ensure that you don't
|
|
37
|
-
* accidentally trigger unnecessary re-renders. We'll properly detect if the options
|
|
38
|
-
* have changed and only call `posthogJs.set_config` if they have, but it's better to
|
|
39
|
-
* avoid unnecessary re-renders in the first place.
|
|
40
|
-
*/
|
|
41
|
-
export function PostHogProvider({ children, client, apiKey, options }: WithOptionalChildren<PostHogProviderProps>) {
|
|
42
|
-
// Used to detect if the client was already initialized
|
|
43
|
-
// This is used to prevent double initialization when running under React.StrictMode
|
|
44
|
-
// We're not storing a simple boolean here because we want to be able to detect if the
|
|
45
|
-
// apiKey or options have changed.
|
|
46
|
-
const previousInitializationRef = useRef<PreviousInitialization | null>(null)
|
|
47
|
-
|
|
48
|
-
const posthog = useMemo(() => {
|
|
49
|
-
if (client) {
|
|
50
|
-
if (apiKey) {
|
|
51
|
-
console.warn(
|
|
52
|
-
'[PostHog.js] You have provided both `client` and `apiKey` to `PostHogProvider`. `apiKey` will be ignored in favour of `client`.'
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
if (options) {
|
|
56
|
-
console.warn(
|
|
57
|
-
'[PostHog.js] You have provided both `client` and `options` to `PostHogProvider`. `options` will be ignored in favour of `client`.'
|
|
58
|
-
)
|
|
59
|
-
}
|
|
60
|
-
return client
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (apiKey) {
|
|
64
|
-
// return the global client, we'll initialize it in the useEffect
|
|
65
|
-
return posthogJs
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
console.warn(
|
|
69
|
-
'[PostHog.js] No `apiKey` or `client` were provided to `PostHogProvider`. Using default global `window.posthog` instance. You must initialize it manually. This is not recommended behavior.'
|
|
70
|
-
)
|
|
71
|
-
return posthogJs
|
|
72
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
73
|
-
}, [client, apiKey, JSON.stringify(options)]) // Stringify options to be a stable reference
|
|
74
|
-
|
|
75
|
-
// TRICKY: The init needs to happen in a useEffect rather than useMemo, as useEffect does not happen during SSR. Otherwise
|
|
76
|
-
// we'd end up trying to call posthogJs.init() on the server, which can cause issues around hydration and double-init.
|
|
77
|
-
useEffect(() => {
|
|
78
|
-
if (client) {
|
|
79
|
-
// if the user has passed their own client, assume they will also handle calling init().
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
const previousInitialization = previousInitializationRef.current
|
|
83
|
-
|
|
84
|
-
if (!previousInitialization) {
|
|
85
|
-
// If it's the first time running this, but it has been loaded elsewhere, warn the user about it.
|
|
86
|
-
if (posthogJs.__loaded) {
|
|
87
|
-
console.warn('[PostHog.js] `posthog` was already loaded elsewhere. This may cause issues.')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Init global client
|
|
91
|
-
posthogJs.init(apiKey, options)
|
|
92
|
-
|
|
93
|
-
// Keep track of whether the client was already initialized
|
|
94
|
-
// This is used to prevent double initialization when running under React.StrictMode, and to know when options change
|
|
95
|
-
previousInitializationRef.current = {
|
|
96
|
-
apiKey: apiKey,
|
|
97
|
-
options: options ?? {},
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
// If the client was already initialized, we might still end up running the effect again for a few reasons:
|
|
101
|
-
// * someone is developing locally under `React.StrictMode`
|
|
102
|
-
// * the config has changed
|
|
103
|
-
// * the apiKey has changed (not supported!)
|
|
104
|
-
//
|
|
105
|
-
// Changing the apiKey isn't well supported and we'll simply log a message suggesting them
|
|
106
|
-
// to take control of the `client` initialization themselves. This is tricky to handle
|
|
107
|
-
// ourselves because we wouldn't know if we should call `.reset()` or not, for example.
|
|
108
|
-
if (apiKey !== previousInitialization.apiKey) {
|
|
109
|
-
console.warn(
|
|
110
|
-
"[PostHog.js] You have provided a different `apiKey` to `PostHogProvider` than the one that was already initialized. This is not supported by our provider and we'll keep using the previous key. If you need to toggle between API Keys you need to control the `client` yourself and pass it in as a prop rather than an `apiKey` prop."
|
|
111
|
-
)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Changing options is better supported because we can just call `posthogJs.set_config(options)`
|
|
115
|
-
// and they'll be good to go with their new config. The SDK will know how to handle the changes.
|
|
116
|
-
if (options && !isDeepEqual(options, previousInitialization.options)) {
|
|
117
|
-
posthogJs.set_config(options)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Keep track of the possibly-new set of apiKey and options
|
|
121
|
-
previousInitializationRef.current = {
|
|
122
|
-
apiKey: apiKey,
|
|
123
|
-
options: options ?? {},
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
127
|
-
}, [client, apiKey, JSON.stringify(options)]) // Stringify options to be a stable reference
|
|
128
|
-
|
|
129
|
-
return (
|
|
130
|
-
<PostHogContext.Provider
|
|
131
|
-
value={{ client: posthog, bootstrap: options?.bootstrap ?? client?.config?.bootstrap }}
|
|
132
|
-
>
|
|
133
|
-
{children}
|
|
134
|
-
</PostHogContext.Provider>
|
|
135
|
-
)
|
|
136
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
import { renderHook } from '@testing-library/react'
|
|
3
|
-
import { PostHogProvider, PostHog } from '../../context'
|
|
4
|
-
import { usePostHog } from '..'
|
|
5
|
-
|
|
6
|
-
jest.useFakeTimers()
|
|
7
|
-
|
|
8
|
-
const posthog = { posthog_client: true } as unknown as PostHog
|
|
9
|
-
|
|
10
|
-
describe('usePostHog hook', () => {
|
|
11
|
-
it('should return the client', () => {
|
|
12
|
-
const { result } = renderHook(() => usePostHog(), {
|
|
13
|
-
wrapper: ({ children }: { children: React.ReactNode }) => (
|
|
14
|
-
<PostHogProvider client={posthog}>{children}</PostHogProvider>
|
|
15
|
-
),
|
|
16
|
-
})
|
|
17
|
-
expect(result.current).toEqual(posthog)
|
|
18
|
-
})
|
|
19
|
-
})
|