@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,9 +1,9 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { render, act } from '@testing-library/react'
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import { InsightsProvider, Insights } from '..'
|
|
4
|
+
import insightsJs from '@hanzo/insights'
|
|
5
5
|
|
|
6
|
-
// Mock
|
|
6
|
+
// Mock insights-js
|
|
7
7
|
jest.mock('@hanzo/insights', () => ({
|
|
8
8
|
__esModule: true,
|
|
9
9
|
default: {
|
|
@@ -13,21 +13,21 @@ jest.mock('@hanzo/insights', () => ({
|
|
|
13
13
|
},
|
|
14
14
|
}))
|
|
15
15
|
|
|
16
|
-
describe('
|
|
16
|
+
describe('InsightsProvider component', () => {
|
|
17
17
|
it('should render children components', () => {
|
|
18
|
-
const
|
|
18
|
+
const insights = {} as unknown as Insights
|
|
19
19
|
const { getByText } = render(
|
|
20
|
-
<
|
|
20
|
+
<InsightsProvider client={insights}>
|
|
21
21
|
<div>Test</div>
|
|
22
|
-
</
|
|
22
|
+
</InsightsProvider>
|
|
23
23
|
)
|
|
24
24
|
expect(getByText('Test')).toBeTruthy()
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
describe('when using apiKey initialization', () => {
|
|
28
28
|
const apiKey = 'test-api-key'
|
|
29
|
-
const initialOptions = { api_host: 'https://app.
|
|
30
|
-
const updatedOptions = { api_host: 'https://eu.
|
|
29
|
+
const initialOptions = { api_host: 'https://app.insights.hanzo.ai' }
|
|
30
|
+
const updatedOptions = { api_host: 'https://eu.insights.com' }
|
|
31
31
|
|
|
32
32
|
beforeEach(() => {
|
|
33
33
|
jest.clearAllMocks()
|
|
@@ -35,49 +35,49 @@ describe('PostHogProvider component', () => {
|
|
|
35
35
|
|
|
36
36
|
it('should call set_config when options change', () => {
|
|
37
37
|
const { rerender } = render(
|
|
38
|
-
<
|
|
38
|
+
<InsightsProvider apiKey={apiKey} options={initialOptions}>
|
|
39
39
|
<div>Test</div>
|
|
40
|
-
</
|
|
40
|
+
</InsightsProvider>
|
|
41
41
|
)
|
|
42
42
|
|
|
43
43
|
// First render should initialize
|
|
44
|
-
expect(
|
|
44
|
+
expect(insightsJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
|
|
45
45
|
|
|
46
46
|
// Rerender with new options
|
|
47
47
|
act(() => {
|
|
48
48
|
rerender(
|
|
49
|
-
<
|
|
49
|
+
<InsightsProvider apiKey={apiKey} options={updatedOptions}>
|
|
50
50
|
<div>Test</div>
|
|
51
|
-
</
|
|
51
|
+
</InsightsProvider>
|
|
52
52
|
)
|
|
53
53
|
})
|
|
54
54
|
|
|
55
55
|
// Should call set_config with new options
|
|
56
|
-
expect(
|
|
56
|
+
expect(insightsJs.set_config).toHaveBeenCalledWith(updatedOptions)
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
it('should NOT call set_config when we pass new options that are the same as the previous options', () => {
|
|
60
60
|
const { rerender } = render(
|
|
61
|
-
<
|
|
61
|
+
<InsightsProvider apiKey={apiKey} options={initialOptions}>
|
|
62
62
|
<div>Test</div>
|
|
63
|
-
</
|
|
63
|
+
</InsightsProvider>
|
|
64
64
|
)
|
|
65
65
|
|
|
66
66
|
// First render should initialize
|
|
67
|
-
expect(
|
|
67
|
+
expect(insightsJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
|
|
68
68
|
|
|
69
69
|
// Rerender with new options
|
|
70
70
|
const sameOptionsButDifferentReference = { ...initialOptions }
|
|
71
71
|
act(() => {
|
|
72
72
|
rerender(
|
|
73
|
-
<
|
|
73
|
+
<InsightsProvider apiKey={apiKey} options={sameOptionsButDifferentReference}>
|
|
74
74
|
<div>Test</div>
|
|
75
|
-
</
|
|
75
|
+
</InsightsProvider>
|
|
76
76
|
)
|
|
77
77
|
})
|
|
78
78
|
|
|
79
79
|
// Should NOT call set_config
|
|
80
|
-
expect(
|
|
80
|
+
expect(insightsJs.set_config).not.toHaveBeenCalled()
|
|
81
81
|
})
|
|
82
82
|
|
|
83
83
|
it('should warn when attempting to change apiKey', () => {
|
|
@@ -85,47 +85,47 @@ describe('PostHogProvider component', () => {
|
|
|
85
85
|
const newApiKey = 'different-api-key'
|
|
86
86
|
|
|
87
87
|
const { rerender } = render(
|
|
88
|
-
<
|
|
88
|
+
<InsightsProvider apiKey={apiKey} options={initialOptions}>
|
|
89
89
|
<div>Test</div>
|
|
90
|
-
</
|
|
90
|
+
</InsightsProvider>
|
|
91
91
|
)
|
|
92
92
|
|
|
93
93
|
// First render should initialize
|
|
94
|
-
expect(
|
|
94
|
+
expect(insightsJs.init).toHaveBeenCalledWith(apiKey, initialOptions)
|
|
95
95
|
|
|
96
96
|
// Rerender with new apiKey
|
|
97
97
|
act(() => {
|
|
98
98
|
rerender(
|
|
99
|
-
<
|
|
99
|
+
<InsightsProvider apiKey={newApiKey} options={initialOptions}>
|
|
100
100
|
<div>Test</div>
|
|
101
|
-
</
|
|
101
|
+
</InsightsProvider>
|
|
102
102
|
)
|
|
103
103
|
})
|
|
104
104
|
|
|
105
105
|
// Should warn about apiKey change
|
|
106
106
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
107
|
-
expect.stringContaining('You have provided a different `apiKey` to `
|
|
107
|
+
expect.stringContaining('You have provided a different `apiKey` to `InsightsProvider`')
|
|
108
108
|
)
|
|
109
109
|
|
|
110
110
|
consoleSpy.mockRestore()
|
|
111
111
|
})
|
|
112
112
|
|
|
113
|
-
it('warns if
|
|
114
|
-
;(
|
|
113
|
+
it('warns if insightsJs has been loaded elsewhere', () => {
|
|
114
|
+
;(insightsJs as any).__loaded = true
|
|
115
115
|
|
|
116
116
|
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
|
|
117
117
|
render(
|
|
118
|
-
<
|
|
118
|
+
<InsightsProvider apiKey={apiKey} options={initialOptions}>
|
|
119
119
|
<div>Test</div>
|
|
120
|
-
</
|
|
120
|
+
</InsightsProvider>
|
|
121
121
|
)
|
|
122
122
|
|
|
123
123
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
124
|
-
expect.stringContaining('`
|
|
124
|
+
expect.stringContaining('`insights` was already loaded elsewhere. This may cause issues.')
|
|
125
125
|
)
|
|
126
126
|
|
|
127
127
|
consoleSpy.mockRestore()
|
|
128
|
-
;(
|
|
128
|
+
;(insightsJs as any).__loaded = false
|
|
129
129
|
})
|
|
130
130
|
})
|
|
131
131
|
})
|
package/src/context/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './InsightsContext'
|
|
2
|
+
export * from './InsightsProvider'
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ErrorInfo } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { Insights } from '../context'
|
|
3
3
|
import { CaptureResult } from '@hanzo/insights'
|
|
4
4
|
|
|
5
5
|
export const setupReactErrorHandler = (
|
|
6
|
-
client:
|
|
6
|
+
client: Insights,
|
|
7
7
|
callback?: (event: CaptureResult | undefined, error: any, errorInfo: ErrorInfo) => void
|
|
8
8
|
) => {
|
|
9
9
|
return (error: any, errorInfo: ErrorInfo): void => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { renderHook, act } from '@testing-library/react'
|
|
3
|
-
import {
|
|
3
|
+
import { InsightsProvider, Insights } from '../../context'
|
|
4
4
|
import { isUndefined } from '../../utils/type-utils'
|
|
5
5
|
import {
|
|
6
6
|
useFeatureFlagPayload,
|
|
@@ -30,11 +30,11 @@ const FEATURE_FLAG_PAYLOADS: Record<string, any> = {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
describe('feature flag hooks', () => {
|
|
33
|
-
let
|
|
33
|
+
let insights: Insights
|
|
34
34
|
let renderProvider: React.FC<{ children: React.ReactNode }>
|
|
35
35
|
|
|
36
36
|
beforeEach(() => {
|
|
37
|
-
|
|
37
|
+
insights = {
|
|
38
38
|
isFeatureEnabled: (flag: string) => !!FEATURE_FLAG_STATUS[flag],
|
|
39
39
|
getFeatureFlag: (flag: string) => FEATURE_FLAG_STATUS[flag],
|
|
40
40
|
getFeatureFlagPayload: (flag: string) => FEATURE_FLAG_PAYLOADS[flag],
|
|
@@ -63,11 +63,11 @@ describe('feature flag hooks', () => {
|
|
|
63
63
|
featureFlags: {
|
|
64
64
|
getFlags: () => ACTIVE_FEATURE_FLAGS,
|
|
65
65
|
hasLoadedFlags: true,
|
|
66
|
-
} as unknown as
|
|
67
|
-
} as unknown as
|
|
66
|
+
} as unknown as Insights['featureFlags'],
|
|
67
|
+
} as unknown as Insights
|
|
68
68
|
|
|
69
69
|
// eslint-disable-next-line react/display-name
|
|
70
|
-
renderProvider = ({ children }) => <
|
|
70
|
+
renderProvider = ({ children }) => <InsightsProvider client={insights}>{children}</InsightsProvider>
|
|
71
71
|
})
|
|
72
72
|
|
|
73
73
|
it.each([
|
|
@@ -132,12 +132,12 @@ describe('feature flag hooks', () => {
|
|
|
132
132
|
},
|
|
133
133
|
featureFlags: {
|
|
134
134
|
hasLoadedFlags: false,
|
|
135
|
-
} as unknown as
|
|
136
|
-
} as unknown as
|
|
135
|
+
} as unknown as Insights['featureFlags'],
|
|
136
|
+
} as unknown as Insights
|
|
137
137
|
|
|
138
138
|
// eslint-disable-next-line react/display-name
|
|
139
139
|
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
140
|
-
<
|
|
140
|
+
<InsightsProvider client={client}>{children}</InsightsProvider>
|
|
141
141
|
)
|
|
142
142
|
|
|
143
143
|
return wrapper
|
|
@@ -211,12 +211,12 @@ describe('feature flag hooks', () => {
|
|
|
211
211
|
},
|
|
212
212
|
featureFlags: {
|
|
213
213
|
hasLoadedFlags: true,
|
|
214
|
-
} as unknown as
|
|
215
|
-
} as unknown as
|
|
214
|
+
} as unknown as Insights['featureFlags'],
|
|
215
|
+
} as unknown as Insights
|
|
216
216
|
|
|
217
217
|
// eslint-disable-next-line react/display-name
|
|
218
218
|
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
219
|
-
<
|
|
219
|
+
<InsightsProvider client={client}>{children}</InsightsProvider>
|
|
220
220
|
)
|
|
221
221
|
|
|
222
222
|
const { result } = renderHook(() => useFeatureFlagResult('flag'), { wrapper })
|
|
@@ -254,12 +254,12 @@ describe('feature flag hooks', () => {
|
|
|
254
254
|
onFeatureFlags: () => unsubscribe,
|
|
255
255
|
featureFlags: {
|
|
256
256
|
hasLoadedFlags: true,
|
|
257
|
-
} as unknown as
|
|
258
|
-
} as unknown as
|
|
257
|
+
} as unknown as Insights['featureFlags'],
|
|
258
|
+
} as unknown as Insights
|
|
259
259
|
|
|
260
260
|
// eslint-disable-next-line react/display-name
|
|
261
261
|
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
|
|
262
|
-
<
|
|
262
|
+
<InsightsProvider client={client}>{children}</InsightsProvider>
|
|
263
263
|
)
|
|
264
264
|
|
|
265
265
|
const { unmount } = renderHook(() => useFeatureFlagResult('flag'), { wrapper })
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { renderHook } from '@testing-library/react'
|
|
3
|
+
import { InsightsProvider, Insights } from '../../context'
|
|
4
|
+
import { useInsights } from '..'
|
|
5
|
+
|
|
6
|
+
jest.useFakeTimers()
|
|
7
|
+
|
|
8
|
+
const insights = { insights_client: true } as unknown as Insights
|
|
9
|
+
|
|
10
|
+
describe('useInsights hook', () => {
|
|
11
|
+
it('should return the client', () => {
|
|
12
|
+
const { result } = renderHook(() => useInsights(), {
|
|
13
|
+
wrapper: ({ children }: { children: React.ReactNode }) => (
|
|
14
|
+
<InsightsProvider client={insights}>{children}</InsightsProvider>
|
|
15
|
+
),
|
|
16
|
+
})
|
|
17
|
+
expect(result.current).toEqual(insights)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { renderHook, act } from '@testing-library/react'
|
|
3
|
-
import {
|
|
3
|
+
import { InsightsProvider, Insights } from '../../context'
|
|
4
4
|
import { useThumbSurvey } from '../useThumbSurvey'
|
|
5
5
|
import { SurveyEventName, SurveyEventProperties } from '@hanzo/insights'
|
|
6
6
|
import { isUndefined } from '../../utils/type-utils'
|
|
@@ -8,7 +8,7 @@ import { isUndefined } from '../../utils/type-utils'
|
|
|
8
8
|
jest.useFakeTimers()
|
|
9
9
|
|
|
10
10
|
describe('useThumbSurvey hook', () => {
|
|
11
|
-
let
|
|
11
|
+
let insights: Insights
|
|
12
12
|
let captureMock: jest.Mock
|
|
13
13
|
let displaySurveyMock: jest.Mock
|
|
14
14
|
let wrapper: React.FC<{ children: React.ReactNode }>
|
|
@@ -17,13 +17,13 @@ describe('useThumbSurvey hook', () => {
|
|
|
17
17
|
captureMock = jest.fn()
|
|
18
18
|
displaySurveyMock = jest.fn()
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
insights = {
|
|
21
21
|
capture: captureMock,
|
|
22
|
-
get_session_replay_url: () => 'https://app.
|
|
22
|
+
get_session_replay_url: () => 'https://app.insights.hanzo.ai/replay/123',
|
|
23
23
|
surveys: { displaySurvey: displaySurveyMock },
|
|
24
|
-
} as unknown as
|
|
24
|
+
} as unknown as Insights
|
|
25
25
|
|
|
26
|
-
wrapper = ({ children }) => <
|
|
26
|
+
wrapper = ({ children }) => <InsightsProvider client={insights}>{children}</InsightsProvider>
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
describe('survey shown tracking', () => {
|
|
@@ -57,7 +57,7 @@ describe('useThumbSurvey hook', () => {
|
|
|
57
57
|
expect(captureMock).toHaveBeenCalledTimes(1)
|
|
58
58
|
expect(captureMock).toHaveBeenCalledWith(SurveyEventName.SHOWN, {
|
|
59
59
|
[SurveyEventProperties.SURVEY_ID]: 'test-survey',
|
|
60
|
-
sessionRecordingUrl: 'https://app.
|
|
60
|
+
sessionRecordingUrl: 'https://app.insights.hanzo.ai/replay/123',
|
|
61
61
|
})
|
|
62
62
|
})
|
|
63
63
|
})
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useContext, useEffect, useState } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { InsightsContext } from '../context'
|
|
3
3
|
|
|
4
4
|
export function useActiveFeatureFlags(): string[] {
|
|
5
|
-
const { client, bootstrap } = useContext(
|
|
5
|
+
const { client, bootstrap } = useContext(InsightsContext)
|
|
6
6
|
|
|
7
7
|
const [featureFlags, setFeatureFlags] = useState<string[]>(() => client.featureFlags.getFlags())
|
|
8
8
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useContext, useEffect, useState } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { InsightsContext } from '../context'
|
|
3
3
|
import { isUndefined } from '../utils/type-utils'
|
|
4
4
|
|
|
5
5
|
export function useFeatureFlagEnabled(flag: string): boolean | undefined {
|
|
6
|
-
const { client, bootstrap } = useContext(
|
|
6
|
+
const { client, bootstrap } = useContext(InsightsContext)
|
|
7
7
|
|
|
8
8
|
const [featureEnabled, setFeatureEnabled] = useState<boolean | undefined>(() => client.isFeatureEnabled(flag))
|
|
9
9
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { JsonType } from '@hanzo/insights'
|
|
2
2
|
import { useContext, useEffect, useState } from 'react'
|
|
3
|
-
import {
|
|
3
|
+
import { InsightsContext } from '../context'
|
|
4
4
|
|
|
5
5
|
export function useFeatureFlagPayload(flag: string): JsonType {
|
|
6
|
-
const { client, bootstrap } = useContext(
|
|
6
|
+
const { client, bootstrap } = useContext(InsightsContext)
|
|
7
7
|
|
|
8
8
|
const [featureFlagPayload, setFeatureFlagPayload] = useState<JsonType>(() => client.getFeatureFlagPayload(flag))
|
|
9
9
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { FeatureFlagResult } from '@hanzo/insights'
|
|
2
2
|
import { useContext, useEffect, useState } from 'react'
|
|
3
|
-
import {
|
|
3
|
+
import { InsightsContext } from '../context'
|
|
4
4
|
import { isUndefined } from '../utils/type-utils'
|
|
5
5
|
|
|
6
6
|
export function useFeatureFlagResult(flag: string): FeatureFlagResult | undefined {
|
|
7
|
-
const { client, bootstrap } = useContext(
|
|
7
|
+
const { client, bootstrap } = useContext(InsightsContext)
|
|
8
8
|
|
|
9
9
|
const [result, setResult] = useState<FeatureFlagResult | undefined>(() => client.getFeatureFlagResult(flag))
|
|
10
10
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useContext, useEffect, useState } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { InsightsContext } from '../context'
|
|
3
3
|
|
|
4
4
|
export function useFeatureFlagVariantKey(flag: string): string | boolean | undefined {
|
|
5
|
-
const { client, bootstrap } = useContext(
|
|
5
|
+
const { client, bootstrap } = useContext(InsightsContext)
|
|
6
6
|
|
|
7
7
|
const [featureFlagVariantKey, setFeatureFlagVariantKey] = useState<string | boolean | undefined>(() =>
|
|
8
8
|
client.getFeatureFlag(flag)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useState, useCallback, useRef, useMemo, type RefCallback, useEffect } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { useInsights } from './useInsights'
|
|
3
3
|
import { DisplaySurveyType, SurveyEventName, SurveyEventProperties, SurveyPosition } from '@hanzo/insights'
|
|
4
4
|
|
|
5
5
|
export interface UseThumbSurveyOptions {
|
|
6
|
-
/** ID of the target
|
|
6
|
+
/** ID of the target Insights survey */
|
|
7
7
|
surveyId: string
|
|
8
8
|
/** Configure the position of the pop-up for followup questions, if applicable. Defaults to SurveyPosition.NextToTrigger */
|
|
9
9
|
displayPosition?: SurveyPosition
|
|
@@ -32,11 +32,11 @@ const TRIGGER_ATTR = 'data-ph-thumb-survey-trigger'
|
|
|
32
32
|
* Convenience hook for managing a "thumb" (1-2 rating scale) survey.
|
|
33
33
|
*
|
|
34
34
|
* Pre-requisites:
|
|
35
|
-
* 1) Ensure surveys are not disabled in your
|
|
36
|
-
* 2) Ensure surveys are enabled in your
|
|
35
|
+
* 1) Ensure surveys are not disabled in your Insights config (`disable_surveys: false`)
|
|
36
|
+
* 2) Ensure surveys are enabled in your Insights project (Settings > Surveys > Enable surveys)
|
|
37
37
|
*
|
|
38
38
|
* How-to:
|
|
39
|
-
* 1) Create an API survey in
|
|
39
|
+
* 1) Create an API survey in Insights (New survey > Presentation > API)
|
|
40
40
|
* 2) Set the first question to a thumb rating scale (Question type: Rating -> Display type: Emoji -> Scale: 1-2 (thumbs up/down))
|
|
41
41
|
* 3) Set the thumb question to "Automatically submit on selection"
|
|
42
42
|
* 4) Optionally add follow-up questions
|
|
@@ -77,7 +77,7 @@ export function useThumbSurvey({
|
|
|
77
77
|
onResponse,
|
|
78
78
|
disableAutoShownTracking,
|
|
79
79
|
}: UseThumbSurveyOptions): UseThumbSurveyResult {
|
|
80
|
-
const
|
|
80
|
+
const insights = useInsights()
|
|
81
81
|
const [responded, setResponded] = useState<'up' | 'down' | null>(null)
|
|
82
82
|
const [instanceId] = useState(() => Math.random().toString(36).slice(2, 9))
|
|
83
83
|
const triggerValue = useMemo(() => `${surveyId}-${instanceId}`, [surveyId, instanceId])
|
|
@@ -100,14 +100,14 @@ export function useThumbSurvey({
|
|
|
100
100
|
const respondedRef = useRef(false)
|
|
101
101
|
|
|
102
102
|
const trackShown = useCallback(() => {
|
|
103
|
-
if (shownRef.current || !
|
|
103
|
+
if (shownRef.current || !insights) return
|
|
104
104
|
shownRef.current = true
|
|
105
|
-
|
|
105
|
+
insights.capture(SurveyEventName.SHOWN, {
|
|
106
106
|
[SurveyEventProperties.SURVEY_ID]: surveyId,
|
|
107
|
-
sessionRecordingUrl:
|
|
107
|
+
sessionRecordingUrl: insights.get_session_replay_url?.(),
|
|
108
108
|
...properties,
|
|
109
109
|
})
|
|
110
|
-
}, [
|
|
110
|
+
}, [insights, surveyId, properties])
|
|
111
111
|
|
|
112
112
|
useEffect(() => {
|
|
113
113
|
if (!disableAutoShownTracking) {
|
|
@@ -117,13 +117,13 @@ export function useThumbSurvey({
|
|
|
117
117
|
|
|
118
118
|
const respond = useCallback(
|
|
119
119
|
(value: 'up' | 'down') => {
|
|
120
|
-
if (!
|
|
120
|
+
if (!insights?.surveys || respondedRef.current) return
|
|
121
121
|
respondedRef.current = true
|
|
122
122
|
|
|
123
123
|
setResponded(value)
|
|
124
124
|
onResponse?.(value)
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
insights.surveys.displaySurvey(surveyId, {
|
|
127
127
|
displayType: DisplaySurveyType.Popover,
|
|
128
128
|
ignoreConditions: true,
|
|
129
129
|
ignoreDelay: true,
|
|
@@ -134,7 +134,7 @@ export function useThumbSurvey({
|
|
|
134
134
|
skipShownEvent: true,
|
|
135
135
|
})
|
|
136
136
|
},
|
|
137
|
-
[
|
|
137
|
+
[insights, surveyId, displayPosition, properties, onResponse, triggerValue]
|
|
138
138
|
)
|
|
139
139
|
|
|
140
140
|
return {
|
package/src/utils/type-utils.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// fails on only one very rare and deliberate custom object:
|
|
3
3
|
// let bomb = { toString : undefined, valueOf: function(o) { return "function BOMBA!"; }};
|
|
4
4
|
export const isFunction = function (f: any): f is (...args: any[]) => any {
|
|
5
|
-
// eslint-disable-next-line
|
|
5
|
+
// eslint-disable-next-line @hanzo/insights/no-direct-function-check
|
|
6
6
|
return typeof f === 'function'
|
|
7
7
|
}
|
|
8
8
|
|
|
@@ -11,6 +11,6 @@ export const isUndefined = function (x: unknown): x is undefined {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export const isNull = function (x: unknown): x is null {
|
|
14
|
-
// eslint-disable-next-line
|
|
14
|
+
// eslint-disable-next-line @hanzo/insights/no-direct-null-check
|
|
15
15
|
return x === null
|
|
16
16
|
}
|
package/surveys/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import posthogJs, { BootstrapConfig } from '@hanzo/insights'
|
|
2
|
-
import { createContext } from 'react'
|
|
3
|
-
|
|
4
|
-
export type PostHog = typeof posthogJs
|
|
5
|
-
|
|
6
|
-
export const PostHogContext = createContext<{ client: PostHog; bootstrap?: BootstrapConfig }>({
|
|
7
|
-
client: posthogJs,
|
|
8
|
-
bootstrap: undefined,
|
|
9
|
-
})
|