@posthog/react 1.6.0 → 1.7.0
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/dist/esm/surveys/index.js +50 -0
- package/dist/esm/surveys/index.js.map +1 -0
- package/dist/types/surveys/index.d.ts +17 -0
- package/dist/umd/surveys/index.js +59 -0
- package/dist/umd/surveys/index.js.map +1 -0
- package/package.json +4 -3
- package/src/hooks/useThumbSurvey.ts +113 -0
- package/src/surveys/index.ts +1 -0
- package/surveys/package.json +7 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createContext, useContext, useState, useMemo, useRef, useCallback } from 'react';
|
|
2
|
+
import posthogJs, { SurveyPosition, DisplaySurveyType } from 'posthog-js';
|
|
3
|
+
|
|
4
|
+
var PostHogContext = createContext({
|
|
5
|
+
client: posthogJs,
|
|
6
|
+
bootstrap: undefined,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
var usePostHog = function () {
|
|
10
|
+
var client = useContext(PostHogContext).client;
|
|
11
|
+
return client;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
var TRIGGER_ATTR = 'data-ph-thumb-survey-trigger';
|
|
15
|
+
function useThumbSurvey(_a) {
|
|
16
|
+
var surveyId = _a.surveyId, _b = _a.displayPosition, displayPosition = _b === void 0 ? SurveyPosition.NextToTrigger : _b, properties = _a.properties, onResponse = _a.onResponse;
|
|
17
|
+
var posthog = usePostHog();
|
|
18
|
+
var _c = useState(null), responded = _c[0], setResponded = _c[1];
|
|
19
|
+
var instanceId = useState(function () { return Math.random().toString(36).slice(2, 9); })[0];
|
|
20
|
+
var triggerValue = useMemo(function () { return "".concat(surveyId, "-").concat(instanceId); }, [surveyId, instanceId]);
|
|
21
|
+
var elementRef = useRef(null);
|
|
22
|
+
var triggerRef = useCallback(function (el) {
|
|
23
|
+
if (elementRef.current) {
|
|
24
|
+
elementRef.current.removeAttribute(TRIGGER_ATTR);
|
|
25
|
+
}
|
|
26
|
+
elementRef.current = el;
|
|
27
|
+
if (el) {
|
|
28
|
+
el.setAttribute(TRIGGER_ATTR, triggerValue);
|
|
29
|
+
}
|
|
30
|
+
}, [triggerValue]);
|
|
31
|
+
var respond = useCallback(function (value) {
|
|
32
|
+
if (!(posthog === null || posthog === void 0 ? void 0 : posthog.surveys) || responded)
|
|
33
|
+
return;
|
|
34
|
+
setResponded(value);
|
|
35
|
+
onResponse === null || onResponse === void 0 ? void 0 : onResponse(value);
|
|
36
|
+
posthog.surveys.displaySurvey(surveyId, {
|
|
37
|
+
displayType: DisplaySurveyType.Popover,
|
|
38
|
+
ignoreConditions: true,
|
|
39
|
+
ignoreDelay: true,
|
|
40
|
+
properties: properties,
|
|
41
|
+
initialResponses: { 0: value === 'up' ? 1 : 2 },
|
|
42
|
+
position: displayPosition,
|
|
43
|
+
selector: "[".concat(TRIGGER_ATTR, "=\"").concat(triggerValue, "\"]"),
|
|
44
|
+
});
|
|
45
|
+
}, [posthog, surveyId, displayPosition, properties, responded, onResponse, triggerValue]);
|
|
46
|
+
return { respond: respond, response: responded, triggerRef: triggerRef };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { useThumbSurvey };
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/context/PostHogContext.ts","../../../src/hooks/usePostHog.ts","../../../src/hooks/useThumbSurvey.ts"],"sourcesContent":[null,null,null],"names":[],"mappings":";;;AAKO,IAAM,cAAc,GAAG,aAAa,CAAmD;AAC1F,IAAA,MAAM,EAAE,SAAS;AACjB,IAAA,SAAS,EAAE,SAAS;AACvB,CAAA,CAAC;;ACLK,IAAM,UAAU,GAAG,YAAA;AACd,IAAA,IAAA,MAAM,GAAK,UAAU,CAAC,cAAc,CAAC,OAA/B,CAA+B;AAC7C,IAAA,OAAO,MAAM,CAAA;AACjB,CAAC;;ACkBD,IAAM,YAAY,GAAG,8BAA8B,CAAA;AA0C7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;
|
|
6
|
+
displayPosition?: SurveyPosition;
|
|
7
|
+
properties?: Record<string, any>;
|
|
8
|
+
onResponse?: (response: 'up' | 'down') => void;
|
|
9
|
+
}
|
|
10
|
+
interface UseThumbSurveyResult {
|
|
11
|
+
respond: (value: 'up' | 'down') => void;
|
|
12
|
+
response: 'up' | 'down' | null;
|
|
13
|
+
triggerRef: RefCallback<HTMLElement>;
|
|
14
|
+
}
|
|
15
|
+
declare function useThumbSurvey({ surveyId, displayPosition, properties, onResponse, }: UseThumbSurveyOptions): UseThumbSurveyResult;
|
|
16
|
+
|
|
17
|
+
export { type UseThumbSurveyOptions, type UseThumbSurveyResult, useThumbSurvey };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('posthog-js')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'react', 'posthog-js'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.PosthogReactSurveys = {}, global.React, global.posthog));
|
|
5
|
+
})(this, (function (exports, React, posthogJs) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var posthogJs__default = /*#__PURE__*/_interopDefaultLegacy(posthogJs);
|
|
10
|
+
|
|
11
|
+
var PostHogContext = React.createContext({
|
|
12
|
+
client: posthogJs__default["default"],
|
|
13
|
+
bootstrap: undefined,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
var usePostHog = function () {
|
|
17
|
+
var client = React.useContext(PostHogContext).client;
|
|
18
|
+
return client;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
var TRIGGER_ATTR = 'data-ph-thumb-survey-trigger';
|
|
22
|
+
function useThumbSurvey(_a) {
|
|
23
|
+
var surveyId = _a.surveyId, _b = _a.displayPosition, displayPosition = _b === void 0 ? posthogJs.SurveyPosition.NextToTrigger : _b, properties = _a.properties, onResponse = _a.onResponse;
|
|
24
|
+
var posthog = usePostHog();
|
|
25
|
+
var _c = React.useState(null), responded = _c[0], setResponded = _c[1];
|
|
26
|
+
var instanceId = React.useState(function () { return Math.random().toString(36).slice(2, 9); })[0];
|
|
27
|
+
var triggerValue = React.useMemo(function () { return "".concat(surveyId, "-").concat(instanceId); }, [surveyId, instanceId]);
|
|
28
|
+
var elementRef = React.useRef(null);
|
|
29
|
+
var triggerRef = React.useCallback(function (el) {
|
|
30
|
+
if (elementRef.current) {
|
|
31
|
+
elementRef.current.removeAttribute(TRIGGER_ATTR);
|
|
32
|
+
}
|
|
33
|
+
elementRef.current = el;
|
|
34
|
+
if (el) {
|
|
35
|
+
el.setAttribute(TRIGGER_ATTR, triggerValue);
|
|
36
|
+
}
|
|
37
|
+
}, [triggerValue]);
|
|
38
|
+
var respond = React.useCallback(function (value) {
|
|
39
|
+
if (!(posthog === null || posthog === void 0 ? void 0 : posthog.surveys) || responded)
|
|
40
|
+
return;
|
|
41
|
+
setResponded(value);
|
|
42
|
+
onResponse === null || onResponse === void 0 ? void 0 : onResponse(value);
|
|
43
|
+
posthog.surveys.displaySurvey(surveyId, {
|
|
44
|
+
displayType: posthogJs.DisplaySurveyType.Popover,
|
|
45
|
+
ignoreConditions: true,
|
|
46
|
+
ignoreDelay: true,
|
|
47
|
+
properties: properties,
|
|
48
|
+
initialResponses: { 0: value === 'up' ? 1 : 2 },
|
|
49
|
+
position: displayPosition,
|
|
50
|
+
selector: "[".concat(TRIGGER_ATTR, "=\"").concat(triggerValue, "\"]"),
|
|
51
|
+
});
|
|
52
|
+
}, [posthog, surveyId, displayPosition, properties, responded, onResponse, triggerValue]);
|
|
53
|
+
return { respond: respond, response: responded, triggerRef: triggerRef };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.useThumbSurvey = useThumbSurvey;
|
|
57
|
+
|
|
58
|
+
}));
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/context/PostHogContext.ts","../../../src/hooks/usePostHog.ts","../../../src/hooks/useThumbSurvey.ts"],"sourcesContent":[null,null,null],"names":["createContext","posthogJs","useContext","SurveyPosition","useState","useMemo","useRef","useCallback","DisplaySurveyType"],"mappings":";;;;;;;;;;IAKO,IAAM,cAAc,GAAGA,mBAAa,CAAmD;IAC1F,IAAA,MAAM,EAAEC,6BAAS;IACjB,IAAA,SAAS,EAAE,SAAS;IACvB,CAAA,CAAC;;ICLK,IAAM,UAAU,GAAG,YAAA;IACd,IAAA,IAAA,MAAM,GAAKC,gBAAU,CAAC,cAAc,CAAC,OAA/B,CAA+B;IAC7C,IAAA,OAAO,MAAM,CAAA;IACjB,CAAC;;ICkBD,IAAM,YAAY,GAAG,8BAA8B,CAAA;IA0C7C,SAAU,cAAc,CAAC,EAKP,EAAA;IAJpB,IAAA,IAAA,QAAQ,GAAA,EAAA,CAAA,QAAA,EACR,EAA8C,GAAA,EAAA,CAAA,eAAA,EAA9C,eAAe,GAAG,EAAA,KAAA,KAAA,CAAA,GAAAC,wBAAc,CAAC,aAAa,KAAA,EAC9C,UAAU,GAAA,EAAA,CAAA,UAAA,EACV,UAAU,GAAA,EAAA,CAAA,UAAA,CAAA;IAEV,IAAA,IAAM,OAAO,GAAG,UAAU,EAAE,CAAA;QACtB,IAAA,EAAA,GAA4BC,cAAQ,CAAuB,IAAI,CAAC,EAA/D,SAAS,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,YAAY,GAAA,EAAA,CAAA,CAAA,CAAwC,CAAA;QAC/D,IAAA,UAAU,GAAIA,cAAQ,CAAC,YAAA,EAAM,OAAA,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAtC,EAAsC,CAAC,CAAA,CAAA,CAA1D,CAA0D;Q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AAC,GAAG,CAAC,EAAE;IAC/C,YAAA,QAAQ,EAAE,eAAe;IACzB,YAAA,QAAQ,EAAE,GAAA,CAAA,MAAA,CAAI,YAAY,EAAA,KAAA,CAAA,CAAA,MAAA,CAAK,YAAY,EAAI,KAAA,CAAA;IAClD,SAAA,CAAC,CAAA;IACN,KAAC,EACD,CAAC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,CAAC,CACxF,CAAA;QAED,OAAO,EAAE,OAAO,EAAA,OAAA,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAA,UAAA,EAAE,CAAA;IACvD;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Provides components and hooks for React integrations of PostHog.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"types": "dist/types/index.d.ts",
|
|
16
16
|
"files": [
|
|
17
17
|
"dist",
|
|
18
|
-
"src"
|
|
18
|
+
"src",
|
|
19
|
+
"surveys"
|
|
19
20
|
],
|
|
20
21
|
"peerDependencies": {
|
|
21
22
|
"@types/react": ">=16.8.0",
|
|
@@ -47,7 +48,7 @@
|
|
|
47
48
|
"tslib": "^2.5.0",
|
|
48
49
|
"typescript": "5.8.2",
|
|
49
50
|
"@posthog-tooling/rollup-utils": "1.1.1",
|
|
50
|
-
"posthog-js": "1.
|
|
51
|
+
"posthog-js": "1.332.0"
|
|
51
52
|
},
|
|
52
53
|
"scripts": {
|
|
53
54
|
"clean": "rimraf dist",
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useState, useCallback, useRef, useMemo, type RefCallback } from 'react'
|
|
2
|
+
import { usePostHog } from './usePostHog'
|
|
3
|
+
import { DisplaySurveyType, SurveyPosition } from 'posthog-js'
|
|
4
|
+
|
|
5
|
+
export interface UseThumbSurveyOptions {
|
|
6
|
+
/** ID of the target PostHog survey */
|
|
7
|
+
surveyId: string
|
|
8
|
+
/** Configure the position of the pop-up for followup questions, if applicable. Defaults to SurveyPosition.NextToTrigger */
|
|
9
|
+
displayPosition?: SurveyPosition
|
|
10
|
+
/** Custom event properties to pass with each survey result */
|
|
11
|
+
properties?: Record<string, any>
|
|
12
|
+
/** Callback on thumb button click */
|
|
13
|
+
onResponse?: (response: 'up' | 'down') => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface UseThumbSurveyResult {
|
|
17
|
+
/** Call this to submit a survey response, with value 'up' or 'down' */
|
|
18
|
+
respond: (value: 'up' | 'down') => void
|
|
19
|
+
/** User's response value, available after submission */
|
|
20
|
+
response: 'up' | 'down' | null
|
|
21
|
+
/** Ref to attach to the trigger element for positioning the followup survey popup */
|
|
22
|
+
triggerRef: RefCallback<HTMLElement>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const TRIGGER_ATTR = 'data-ph-thumb-survey-trigger'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Convenience hook for managing a "thumb" (1-2 rating scale) survey.
|
|
29
|
+
*
|
|
30
|
+
* Pre-requisites:
|
|
31
|
+
* 1) Ensure surveys are not disabled in your PostHog config (`disable_surveys: false`)
|
|
32
|
+
* 2) Ensure surveys are enabled in your PostHog project (Settings > Surveys > Enable surveys)
|
|
33
|
+
*
|
|
34
|
+
* How-to:
|
|
35
|
+
* 1) Create an API survey in PostHog (New survey > Presentation > API)
|
|
36
|
+
* 2) Set the first question to a thumb rating scale (Question type: Rating -> Display type: Emoji -> Scale: 1-2 (thumbs up/down))
|
|
37
|
+
* 3) Set the thumb question to "Automatically submit on selection"
|
|
38
|
+
* 4) Optionally add follow-up questions
|
|
39
|
+
* 5) Use the hook:
|
|
40
|
+
*
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const { respond, response, triggerRef } = useThumbSurvey({
|
|
43
|
+
* surveyId: 'my-survey-id',
|
|
44
|
+
* properties: { foo: 'bar' }, // optional custom properties to pass along with the survey responses
|
|
45
|
+
* onResponse: (response) => { console.log(response) }, // optional callback on submission
|
|
46
|
+
* })
|
|
47
|
+
*
|
|
48
|
+
* return (
|
|
49
|
+
* <div ref={triggerRef}>
|
|
50
|
+
* <button onClick={() => respond('up')} style={{ color: response === 'up' ? 'green' : undefined }}>👍</button>
|
|
51
|
+
* <button onClick={() => respond('down')} style={{ color: response === 'down' ? 'red' : undefined }}>👎</button>
|
|
52
|
+
* </div>
|
|
53
|
+
* )
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* 6) If your survey has further questions, the survey will automatically display as a popover, either:
|
|
57
|
+
* - [default] next to the triggerRef element,
|
|
58
|
+
* - OR wherever you specify in options.position
|
|
59
|
+
*
|
|
60
|
+
* Notes:
|
|
61
|
+
* - The thumbs up/down response will ALWAYS be recorded, whether your survey is set to collect partial responses or not.
|
|
62
|
+
* - By default, followup questions will be displayed as a pop-up next to the triggerRef. Use options.position to change the position.
|
|
63
|
+
*
|
|
64
|
+
* @param options UseThumbSurveyOptions
|
|
65
|
+
* @returns UseThumbSurveyResult
|
|
66
|
+
*/
|
|
67
|
+
export function useThumbSurvey({
|
|
68
|
+
surveyId,
|
|
69
|
+
displayPosition = SurveyPosition.NextToTrigger,
|
|
70
|
+
properties,
|
|
71
|
+
onResponse,
|
|
72
|
+
}: UseThumbSurveyOptions): UseThumbSurveyResult {
|
|
73
|
+
const posthog = usePostHog()
|
|
74
|
+
const [responded, setResponded] = useState<'up' | 'down' | null>(null)
|
|
75
|
+
const [instanceId] = useState(() => Math.random().toString(36).slice(2, 9))
|
|
76
|
+
const triggerValue = useMemo(() => `${surveyId}-${instanceId}`, [surveyId, instanceId])
|
|
77
|
+
|
|
78
|
+
const elementRef = useRef<HTMLElement | null>(null)
|
|
79
|
+
const triggerRef = useCallback(
|
|
80
|
+
(el: HTMLElement | null) => {
|
|
81
|
+
if (elementRef.current) {
|
|
82
|
+
elementRef.current.removeAttribute(TRIGGER_ATTR)
|
|
83
|
+
}
|
|
84
|
+
elementRef.current = el
|
|
85
|
+
if (el) {
|
|
86
|
+
el.setAttribute(TRIGGER_ATTR, triggerValue)
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
[triggerValue]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
const respond = useCallback(
|
|
93
|
+
(value: 'up' | 'down') => {
|
|
94
|
+
if (!posthog?.surveys || responded) return
|
|
95
|
+
|
|
96
|
+
setResponded(value)
|
|
97
|
+
onResponse?.(value)
|
|
98
|
+
|
|
99
|
+
posthog.surveys.displaySurvey(surveyId, {
|
|
100
|
+
displayType: DisplaySurveyType.Popover,
|
|
101
|
+
ignoreConditions: true,
|
|
102
|
+
ignoreDelay: true,
|
|
103
|
+
properties,
|
|
104
|
+
initialResponses: { 0: value === 'up' ? 1 : 2 },
|
|
105
|
+
position: displayPosition,
|
|
106
|
+
selector: `[${TRIGGER_ATTR}="${triggerValue}"]`,
|
|
107
|
+
})
|
|
108
|
+
},
|
|
109
|
+
[posthog, surveyId, displayPosition, properties, responded, onResponse, triggerValue]
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
return { respond, response: responded, triggerRef }
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../hooks/useThumbSurvey'
|