posthog-js 1.151.2 → 1.152.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/array.full.js +2 -2
- package/dist/array.full.js.map +1 -1
- package/dist/array.js +1 -1
- package/dist/array.js.map +1 -1
- package/dist/lib/src/extensions/surveys/components/PostHogLogo.d.ts +1 -3
- package/dist/lib/src/extensions/surveys/surveys-utils.d.ts +2 -1
- package/dist/lib/src/extensions/surveys.d.ts +11 -3
- package/dist/lib/src/posthog-core.d.ts +4 -0
- package/dist/lib/src/posthog-surveys-types.d.ts +4 -0
- package/dist/lib/src/posthog-surveys.d.ts +3 -0
- package/dist/main.js +1 -1
- package/dist/main.js.map +1 -1
- package/dist/module.d.ts +12 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/surveys-preview.js +1 -1
- package/dist/surveys-preview.js.map +1 -1
- package/dist/surveys.js +1 -1
- package/dist/surveys.js.map +1 -1
- package/lib/package.json +1 -1
- package/lib/src/extensions/surveys/components/BottomSection.jsx +4 -4
- package/lib/src/extensions/surveys/components/BottomSection.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/ConfirmationMessage.jsx +5 -2
- package/lib/src/extensions/surveys/components/ConfirmationMessage.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/PostHogLogo.d.ts +1 -3
- package/lib/src/extensions/surveys/components/PostHogLogo.jsx +2 -5
- package/lib/src/extensions/surveys/components/PostHogLogo.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/QuestionHeader.jsx +2 -1
- package/lib/src/extensions/surveys/components/QuestionHeader.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/QuestionTypes.jsx +9 -17
- package/lib/src/extensions/surveys/components/QuestionTypes.jsx.map +1 -1
- package/lib/src/extensions/surveys/surveys-utils.d.ts +2 -1
- package/lib/src/extensions/surveys/surveys-utils.jsx +4 -2
- package/lib/src/extensions/surveys/surveys-utils.jsx.map +1 -1
- package/lib/src/extensions/surveys.d.ts +11 -3
- package/lib/src/extensions/surveys.jsx +62 -7
- package/lib/src/extensions/surveys.jsx.map +1 -1
- package/lib/src/posthog-core.d.ts +4 -0
- package/lib/src/posthog-core.js +8 -0
- package/lib/src/posthog-core.js.map +1 -1
- package/lib/src/posthog-surveys-types.d.ts +4 -0
- package/lib/src/posthog-surveys-types.js.map +1 -1
- package/lib/src/posthog-surveys.d.ts +3 -0
- package/lib/src/posthog-surveys.js +23 -1
- package/lib/src/posthog-surveys.js.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"posthog-surveys-types.js","sourceRoot":"","sources":["../../src/posthog-surveys-types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAoCH,MAAM,CAAN,IAAY,UAIX;AAJD,WAAY,UAAU;IAClB,iCAAmB,CAAA;IACnB,yBAAW,CAAA;IACX,+BAAiB,CAAA;AACrB,CAAC,EAJW,UAAU,KAAV,UAAU,QAIrB;AAwCD,MAAM,CAAN,IAAY,kBAMX;AAND,WAAY,kBAAkB;IAC1B,mCAAa,CAAA;IACb,wDAAkC,CAAA;IAClC,oDAA8B,CAAA;IAC9B,uCAAiB,CAAA;IACjB,mCAAa,CAAA;AACjB,CAAC,EANW,kBAAkB,KAAlB,kBAAkB,QAM7B;AAED,MAAM,CAAN,IAAY,2BAKX;AALD,WAAY,2BAA2B;IACnC,6DAA8B,CAAA;IAC9B,0CAAW,CAAA;IACX,+DAAgC,CAAA;IAChC,qEAAsC,CAAA;AAC1C,CAAC,EALW,2BAA2B,KAA3B,2BAA2B,QAKtC","sourcesContent":["/**\n * Having Survey types in types.ts was confusing tsc\n * and generating an invalid module.d.ts\n * See https://github.com/PostHog/posthog-js/issues/698\n */\n\nexport interface SurveyAppearance {\n // keep in sync with frontend/src/types.ts -> SurveyAppearance\n backgroundColor?: string\n submitButtonColor?: string\n // text color is deprecated, use auto contrast text color instead\n textColor?: string\n // deprecate submit button text eventually\n submitButtonText?: string\n descriptionTextColor?: string\n ratingButtonColor?: string\n ratingButtonActiveColor?: string\n ratingButtonHoverColor?: string\n whiteLabel?: boolean\n autoDisappear?: boolean\n displayThankYouMessage?: boolean\n thankYouMessageHeader?: string\n thankYouMessageDescription?: string\n thankYouMessageDescriptionContentType?: SurveyQuestionDescriptionContentType\n thankYouMessageCloseButtonText?: string\n borderColor?: string\n position?: 'left' | 'right' | 'center'\n placeholder?: string\n shuffleQuestions?: boolean\n surveyPopupDelaySeconds?: number\n // widget options\n widgetType?: 'button' | 'tab' | 'selector'\n widgetSelector?: string\n widgetLabel?: string\n widgetColor?: string\n // questionable: Not in frontend/src/types.ts -> SurveyAppearance, but used in site app\n maxWidth?: string\n zIndex?: string\n}\n\nexport enum SurveyType {\n Popover = 'popover',\n API = 'api',\n Widget = 'widget',\n}\n\nexport type SurveyQuestion = BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion\n\nexport type SurveyQuestionDescriptionContentType = 'html' | 'text'\n\ninterface SurveyQuestionBase {\n question: string\n description?: string | null\n descriptionContentType?: SurveyQuestionDescriptionContentType\n optional?: boolean\n buttonText?: string\n originalQuestionIndex: number\n branching?: NextQuestionBranching | EndBranching | ResponseBasedBranching | SpecificQuestionBranching\n}\n\nexport interface BasicSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Open\n}\n\nexport interface LinkSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Link\n link?: string | null\n}\n\nexport interface RatingSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Rating\n display: 'number' | 'emoji'\n scale: 3 | 5 | 7 | 10\n lowerBoundLabel: string\n upperBoundLabel: string\n}\n\nexport interface MultipleSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice\n choices: string[]\n hasOpenChoice?: boolean\n shuffleOptions?: boolean\n}\n\nexport enum SurveyQuestionType {\n Open = 'open',\n MultipleChoice = 'multiple_choice',\n SingleChoice = 'single_choice',\n Rating = 'rating',\n Link = 'link',\n}\n\nexport enum SurveyQuestionBranchingType {\n NextQuestion = 'next_question',\n End = 'end',\n ResponseBased = 'response_based',\n SpecificQuestion = 'specific_question',\n}\n\ninterface NextQuestionBranching {\n type: SurveyQuestionBranchingType.NextQuestion\n}\n\ninterface EndBranching {\n type: SurveyQuestionBranchingType.End\n}\n\ninterface ResponseBasedBranching {\n type: SurveyQuestionBranchingType.ResponseBased\n responseValues: Record<string, any>\n}\n\ninterface SpecificQuestionBranching {\n type: SurveyQuestionBranchingType.SpecificQuestion\n index: number\n}\n\nexport interface SurveyResponse {\n surveys: Survey[]\n}\n\nexport type SurveyCallback = (surveys: Survey[]) => void\n\nexport type SurveyUrlMatchType = 'regex' | 'not_regex' | 'exact' | 'is_not' | 'icontains' | 'not_icontains'\n\nexport interface SurveyElement {\n text?: string\n $el_text?: string\n tag_name?: string\n href?: string\n attr_id?: string\n attr_class?: string[]\n nth_child?: number\n nth_of_type?: number\n attributes?: Record<string, any>\n event_id?: number\n order?: number\n group_id?: number\n}\nexport interface Survey {\n // Sync this with the backend's SurveyAPISerializer!\n id: string\n name: string\n description: string\n type: SurveyType\n linked_flag_key: string | null\n targeting_flag_key: string | null\n internal_targeting_flag_key: string | null\n questions: SurveyQuestion[]\n appearance: SurveyAppearance | null\n conditions: {\n url?: string\n selector?: string\n seenSurveyWaitPeriodInDays?: number\n urlMatchType?: SurveyUrlMatchType\n events: {\n repeatedActivation?: boolean\n values: {\n name: string\n }[]\n } | null\n actions: {\n values: ActionType[]\n } | null\n } | null\n start_date: string | null\n end_date: string | null\n current_iteration: number | null\n current_iteration_start_date: string | null\n}\n\nexport interface ActionType {\n count?: number\n created_at: string\n deleted?: boolean\n id: number\n name: string | null\n steps?: ActionStepType[]\n tags?: string[]\n is_action?: true\n action_id?: number // alias of id to make it compatible with event definitions uuid\n}\n\n/** Sync with plugin-server/src/types.ts */\nexport type ActionStepStringMatching = 'contains' | 'exact' | 'regex'\n\nexport interface ActionStepType {\n event?: string | null\n selector?: string | null\n /** @deprecated Only `selector` should be used now. */\n tag_name?: string\n text?: string | null\n /** @default StringMatching.Exact */\n text_matching?: ActionStepStringMatching | null\n href?: string | null\n /** @default ActionStepStringMatching.Exact */\n href_matching?: ActionStepStringMatching | null\n url?: string | null\n /** @default StringMatching.Contains */\n url_matching?: ActionStepStringMatching | null\n}\n"]}
|
|
1
|
+
{"version":3,"file":"posthog-surveys-types.js","sourceRoot":"","sources":["../../src/posthog-surveys-types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAoCH,MAAM,CAAN,IAAY,UAIX;AAJD,WAAY,UAAU;IAClB,iCAAmB,CAAA;IACnB,yBAAW,CAAA;IACX,+BAAiB,CAAA;AACrB,CAAC,EAJW,UAAU,KAAV,UAAU,QAIrB;AAwCD,MAAM,CAAN,IAAY,kBAMX;AAND,WAAY,kBAAkB;IAC1B,mCAAa,CAAA;IACb,wDAAkC,CAAA;IAClC,oDAA8B,CAAA;IAC9B,uCAAiB,CAAA;IACjB,mCAAa,CAAA;AACjB,CAAC,EANW,kBAAkB,KAAlB,kBAAkB,QAM7B;AAED,MAAM,CAAN,IAAY,2BAKX;AALD,WAAY,2BAA2B;IACnC,6DAA8B,CAAA;IAC9B,0CAAW,CAAA;IACX,+DAAgC,CAAA;IAChC,qEAAsC,CAAA;AAC1C,CAAC,EALW,2BAA2B,KAA3B,2BAA2B,QAKtC","sourcesContent":["/**\n * Having Survey types in types.ts was confusing tsc\n * and generating an invalid module.d.ts\n * See https://github.com/PostHog/posthog-js/issues/698\n */\n\nexport interface SurveyAppearance {\n // keep in sync with frontend/src/types.ts -> SurveyAppearance\n backgroundColor?: string\n submitButtonColor?: string\n // text color is deprecated, use auto contrast text color instead\n textColor?: string\n // deprecate submit button text eventually\n submitButtonText?: string\n descriptionTextColor?: string\n ratingButtonColor?: string\n ratingButtonActiveColor?: string\n ratingButtonHoverColor?: string\n whiteLabel?: boolean\n autoDisappear?: boolean\n displayThankYouMessage?: boolean\n thankYouMessageHeader?: string\n thankYouMessageDescription?: string\n thankYouMessageDescriptionContentType?: SurveyQuestionDescriptionContentType\n thankYouMessageCloseButtonText?: string\n borderColor?: string\n position?: 'left' | 'right' | 'center'\n placeholder?: string\n shuffleQuestions?: boolean\n surveyPopupDelaySeconds?: number\n // widget options\n widgetType?: 'button' | 'tab' | 'selector'\n widgetSelector?: string\n widgetLabel?: string\n widgetColor?: string\n // questionable: Not in frontend/src/types.ts -> SurveyAppearance, but used in site app\n maxWidth?: string\n zIndex?: string\n}\n\nexport enum SurveyType {\n Popover = 'popover',\n API = 'api',\n Widget = 'widget',\n}\n\nexport type SurveyQuestion = BasicSurveyQuestion | LinkSurveyQuestion | RatingSurveyQuestion | MultipleSurveyQuestion\n\nexport type SurveyQuestionDescriptionContentType = 'html' | 'text'\n\ninterface SurveyQuestionBase {\n question: string\n description?: string | null\n descriptionContentType?: SurveyQuestionDescriptionContentType\n optional?: boolean\n buttonText?: string\n originalQuestionIndex: number\n branching?: NextQuestionBranching | EndBranching | ResponseBasedBranching | SpecificQuestionBranching\n}\n\nexport interface BasicSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Open\n}\n\nexport interface LinkSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Link\n link?: string | null\n}\n\nexport interface RatingSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.Rating\n display: 'number' | 'emoji'\n scale: 3 | 5 | 7 | 10\n lowerBoundLabel: string\n upperBoundLabel: string\n}\n\nexport interface MultipleSurveyQuestion extends SurveyQuestionBase {\n type: SurveyQuestionType.SingleChoice | SurveyQuestionType.MultipleChoice\n choices: string[]\n hasOpenChoice?: boolean\n shuffleOptions?: boolean\n}\n\nexport enum SurveyQuestionType {\n Open = 'open',\n MultipleChoice = 'multiple_choice',\n SingleChoice = 'single_choice',\n Rating = 'rating',\n Link = 'link',\n}\n\nexport enum SurveyQuestionBranchingType {\n NextQuestion = 'next_question',\n End = 'end',\n ResponseBased = 'response_based',\n SpecificQuestion = 'specific_question',\n}\n\ninterface NextQuestionBranching {\n type: SurveyQuestionBranchingType.NextQuestion\n}\n\ninterface EndBranching {\n type: SurveyQuestionBranchingType.End\n}\n\ninterface ResponseBasedBranching {\n type: SurveyQuestionBranchingType.ResponseBased\n responseValues: Record<string, any>\n}\n\ninterface SpecificQuestionBranching {\n type: SurveyQuestionBranchingType.SpecificQuestion\n index: number\n}\n\nexport interface SurveyResponse {\n surveys: Survey[]\n}\n\nexport type SurveyCallback = (surveys: Survey[]) => void\n\nexport type SurveyUrlMatchType = 'regex' | 'not_regex' | 'exact' | 'is_not' | 'icontains' | 'not_icontains'\n\nexport interface SurveyElement {\n text?: string\n $el_text?: string\n tag_name?: string\n href?: string\n attr_id?: string\n attr_class?: string[]\n nth_child?: number\n nth_of_type?: number\n attributes?: Record<string, any>\n event_id?: number\n order?: number\n group_id?: number\n}\nexport interface SurveyRenderReason {\n visible: boolean\n disabledReason?: string\n}\n\nexport interface Survey {\n // Sync this with the backend's SurveyAPISerializer!\n id: string\n name: string\n description: string\n type: SurveyType\n linked_flag_key: string | null\n targeting_flag_key: string | null\n internal_targeting_flag_key: string | null\n questions: SurveyQuestion[]\n appearance: SurveyAppearance | null\n conditions: {\n url?: string\n selector?: string\n seenSurveyWaitPeriodInDays?: number\n urlMatchType?: SurveyUrlMatchType\n events: {\n repeatedActivation?: boolean\n values: {\n name: string\n }[]\n } | null\n actions: {\n values: ActionType[]\n } | null\n } | null\n start_date: string | null\n end_date: string | null\n current_iteration: number | null\n current_iteration_start_date: string | null\n}\n\nexport interface ActionType {\n count?: number\n created_at: string\n deleted?: boolean\n id: number\n name: string | null\n steps?: ActionStepType[]\n tags?: string[]\n is_action?: true\n action_id?: number // alias of id to make it compatible with event definitions uuid\n}\n\n/** Sync with plugin-server/src/types.ts */\nexport type ActionStepStringMatching = 'contains' | 'exact' | 'regex'\n\nexport interface ActionStepType {\n event?: string | null\n selector?: string | null\n /** @deprecated Only `selector` should be used now. */\n tag_name?: string\n text?: string | null\n /** @default StringMatching.Exact */\n text_matching?: ActionStepStringMatching | null\n href?: string | null\n /** @default ActionStepStringMatching.Exact */\n href_matching?: ActionStepStringMatching | null\n url?: string | null\n /** @default StringMatching.Contains */\n url_matching?: ActionStepStringMatching | null\n}\n"]}
|
|
@@ -7,6 +7,7 @@ export declare class PostHogSurveys {
|
|
|
7
7
|
private readonly instance;
|
|
8
8
|
private _decideServerResponse?;
|
|
9
9
|
_surveyEventReceiver: SurveyEventReceiver | null;
|
|
10
|
+
private _surveyManager;
|
|
10
11
|
constructor(instance: PostHog);
|
|
11
12
|
afterDecideResponse(response: DecideResponse): void;
|
|
12
13
|
loadIfEnabled(): void;
|
|
@@ -14,4 +15,6 @@ export declare class PostHogSurveys {
|
|
|
14
15
|
getActiveMatchingSurveys(callback: SurveyCallback, forceReload?: boolean): void;
|
|
15
16
|
getNextSurveyStep(survey: Survey, currentQuestionIndex: number, response: string | string[] | number | null): any;
|
|
16
17
|
private _canActivateRepeatedly;
|
|
18
|
+
canRenderSurvey(surveyId: string): void;
|
|
19
|
+
renderSurvey(surveyId: string, selector: string): void;
|
|
17
20
|
}
|
|
@@ -61,7 +61,7 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
61
61
|
if (err) {
|
|
62
62
|
return logger.error(LOGGER_PREFIX, 'Could not load surveys script', err);
|
|
63
63
|
}
|
|
64
|
-
assignableWindow.extendPostHogWithSurveys(_this.instance);
|
|
64
|
+
_this._surveyManager = assignableWindow.extendPostHogWithSurveys(_this.instance);
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
67
|
};
|
|
@@ -226,6 +226,28 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
226
226
|
}
|
|
227
227
|
return assignableWindow.__PosthogExtensions__.canActivateRepeatedly(survey);
|
|
228
228
|
};
|
|
229
|
+
PostHogSurveys.prototype.canRenderSurvey = function (surveyId) {
|
|
230
|
+
var _this = this;
|
|
231
|
+
if (isNullish(this._surveyManager)) {
|
|
232
|
+
logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
this.getSurveys(function (surveys) {
|
|
236
|
+
var survey = surveys.filter(function (x) { return x.id === surveyId; })[0];
|
|
237
|
+
_this._surveyManager.canRenderSurvey(survey);
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
PostHogSurveys.prototype.renderSurvey = function (surveyId, selector) {
|
|
241
|
+
var _this = this;
|
|
242
|
+
if (isNullish(this._surveyManager)) {
|
|
243
|
+
logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling');
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
this.getSurveys(function (surveys) {
|
|
247
|
+
var survey = surveys.filter(function (x) { return x.id === surveyId; })[0];
|
|
248
|
+
_this._surveyManager.renderSurvey(survey, document === null || document === void 0 ? void 0 : document.querySelector(selector));
|
|
249
|
+
});
|
|
250
|
+
};
|
|
229
251
|
return PostHogSurveys;
|
|
230
252
|
}());
|
|
231
253
|
export { PostHogSurveys };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"posthog-surveys.js","sourceRoot":"","sources":["../../src/posthog-surveys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAGH,2BAA2B,EAC3B,kBAAkB,GAErB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AACnE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAEpE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAE9C,IAAM,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oBAED,OAAO,iBAAiB,CAAA;iBAC3B;aACJ;YAED,OAAO,iBAAiB,CAAA;SAC3B;QAED,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,sEAAsE,CAAC,CAAA;QAClG,OAAO,iBAAiB,CAAA;IAC5B,CAAC;IAED,wHAAwH;IAChH,+CAAsB,GAA9B,UAA+B,MAAc;QACzC,IAAI,SAAS,CAAC,gBAAgB,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,EAAE;YACzE,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,gEAAgE,CAAC,CAAA;SAC/F;QACD,OAAO,gBAAgB,CAAC,qBAAqB,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAA;IAC/E,CAAC;IACL,qBAAC;AAAD,CAAC,AA1ND,IA0NC","sourcesContent":["import { PostHog } from './posthog-core'\nimport { SURVEYS } from './constants'\nimport {\n Survey,\n SurveyCallback,\n SurveyQuestionBranchingType,\n SurveyQuestionType,\n SurveyUrlMatchType,\n} from './posthog-surveys-types'\nimport { isUrlMatchingRegex } from './utils/request-utils'\nimport { SurveyEventReceiver } from './utils/survey-event-receiver'\nimport { assignableWindow, document, window } from './utils/globals'\nimport { DecideResponse } from './types'\nimport { logger } from './utils/logger'\nimport { isNullish } from './utils/type-utils'\n\nconst LOGGER_PREFIX = '[Surveys]'\n\nexport const surveyUrlValidationMap: Record<SurveyUrlMatchType, (conditionsUrl: string) => boolean> = {\n icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1,\n not_icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) === -1,\n regex: (conditionsUrl) => !!window && isUrlMatchingRegex(window.location.href, conditionsUrl),\n not_regex: (conditionsUrl) => !!window && !isUrlMatchingRegex(window.location.href, conditionsUrl),\n exact: (conditionsUrl) => window?.location.href === conditionsUrl,\n is_not: (conditionsUrl) => window?.location.href !== conditionsUrl,\n}\n\nfunction getRatingBucketForResponseValue(responseValue: number, scale: number) {\n if (scale === 3) {\n if (responseValue < 1 || responseValue > 3) {\n throw new Error('The response must be in range 1-3')\n }\n\n return responseValue === 1 ? 'negative' : responseValue === 2 ? 'neutral' : 'positive'\n } else if (scale === 5) {\n if (responseValue < 1 || responseValue > 5) {\n throw new Error('The response must be in range 1-5')\n }\n\n return responseValue <= 2 ? 'negative' : responseValue === 3 ? 'neutral' : 'positive'\n } else if (scale === 10) {\n if (responseValue < 0 || responseValue > 10) {\n throw new Error('The response must be in range 0-10')\n }\n\n return responseValue <= 6 ? 'detractors' : responseValue <= 8 ? 'passives' : 'promoters'\n }\n\n throw new Error('The scale must be one of: 3, 5, 10')\n}\n\nexport class PostHogSurveys {\n private _decideServerResponse?: boolean\n public _surveyEventReceiver: SurveyEventReceiver | null\n\n constructor(private readonly instance: PostHog) {\n // we set this to undefined here because we need the persistence storage for this type\n // but that's not initialized until loadIfEnabled is called.\n this._surveyEventReceiver = null\n }\n\n afterDecideResponse(response: DecideResponse) {\n this._decideServerResponse = !!response['surveys']\n this.loadIfEnabled()\n }\n\n loadIfEnabled() {\n const surveysGenerator = assignableWindow?.extendPostHogWithSurveys\n\n if (!this.instance.config.disable_surveys && this._decideServerResponse && !surveysGenerator) {\n if (this._surveyEventReceiver == null) {\n this._surveyEventReceiver = new SurveyEventReceiver(this.instance)\n }\n this.instance.requestRouter.loadScript('/static/surveys.js', (err) => {\n if (err) {\n return logger.error(LOGGER_PREFIX, 'Could not load surveys script', err)\n }\n\n assignableWindow.extendPostHogWithSurveys(this.instance)\n })\n }\n }\n\n getSurveys(callback: SurveyCallback, forceReload = false) {\n // In case we manage to load the surveys script, but config says not to load surveys\n // then we shouldn't return survey data\n if (this.instance.config.disable_surveys) {\n return callback([])\n }\n\n if (this._surveyEventReceiver == null) {\n this._surveyEventReceiver = new SurveyEventReceiver(this.instance)\n }\n\n const existingSurveys = this.instance.get_property(SURVEYS)\n if (!existingSurveys || forceReload) {\n this.instance._send_request({\n url: this.instance.requestRouter.endpointFor(\n 'api',\n `/api/surveys/?token=${this.instance.config.token}`\n ),\n method: 'GET',\n transport: 'XHR',\n callback: (response) => {\n if (response.statusCode !== 200 || !response.json) {\n return callback([])\n }\n const surveys = response.json.surveys || []\n\n const eventOrActionBasedSurveys = surveys.filter(\n (survey: Survey) =>\n (survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events?.values?.length > 0) ||\n (survey.conditions?.actions &&\n survey.conditions?.actions?.values &&\n survey.conditions?.actions?.values?.length > 0)\n )\n\n if (eventOrActionBasedSurveys.length > 0) {\n this._surveyEventReceiver?.register(eventOrActionBasedSurveys)\n }\n\n this.instance.persistence?.register({ [SURVEYS]: surveys })\n return callback(surveys)\n },\n })\n } else {\n return callback(existingSurveys)\n }\n }\n\n getActiveMatchingSurveys(callback: SurveyCallback, forceReload = false) {\n this.getSurveys((surveys) => {\n const activeSurveys = surveys.filter((survey) => {\n return !!(survey.start_date && !survey.end_date)\n })\n\n const conditionMatchedSurveys = activeSurveys.filter((survey) => {\n if (!survey.conditions) {\n return true\n }\n\n // use urlMatchType to validate url condition, fallback to contains for backwards compatibility\n const urlCheck = survey.conditions?.url\n ? surveyUrlValidationMap[survey.conditions?.urlMatchType ?? 'icontains'](survey.conditions.url)\n : true\n const selectorCheck = survey.conditions?.selector\n ? document?.querySelector(survey.conditions.selector)\n : true\n return urlCheck && selectorCheck\n })\n\n // get all the surveys that have been activated so far with user actions.\n const activatedSurveys: string[] | undefined = this._surveyEventReceiver?.getSurveys()\n const targetingMatchedSurveys = conditionMatchedSurveys.filter((survey) => {\n if (!survey.linked_flag_key && !survey.targeting_flag_key && !survey.internal_targeting_flag_key) {\n return true\n }\n const linkedFlagCheck = survey.linked_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.linked_flag_key)\n : true\n const targetingFlagCheck = survey.targeting_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.targeting_flag_key)\n : true\n\n const hasEvents =\n survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events?.values.length > 0\n\n const hasActions =\n survey.conditions?.actions &&\n survey.conditions?.actions?.values &&\n survey.conditions?.actions?.values.length > 0\n const eventBasedTargetingFlagCheck =\n hasEvents || hasActions ? activatedSurveys?.includes(survey.id) : true\n\n const overrideInternalTargetingFlagCheck = this._canActivateRepeatedly(survey)\n const internalTargetingFlagCheck =\n survey.internal_targeting_flag_key && !overrideInternalTargetingFlagCheck\n ? this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)\n : true\n\n return (\n linkedFlagCheck && targetingFlagCheck && internalTargetingFlagCheck && eventBasedTargetingFlagCheck\n )\n })\n\n return callback(targetingMatchedSurveys)\n }, forceReload)\n }\n\n getNextSurveyStep(survey: Survey, currentQuestionIndex: number, response: string | string[] | number | null) {\n const question = survey.questions[currentQuestionIndex]\n const nextQuestionIndex = currentQuestionIndex + 1\n\n if (!question.branching?.type) {\n if (currentQuestionIndex === survey.questions.length - 1) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n\n if (question.branching.type === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n } else if (question.branching.type === SurveyQuestionBranchingType.SpecificQuestion) {\n if (Number.isInteger(question.branching.index)) {\n return question.branching.index\n }\n } else if (question.branching.type === SurveyQuestionBranchingType.ResponseBased) {\n // Single choice\n if (question.type === SurveyQuestionType.SingleChoice) {\n // :KLUDGE: for now, look up the choiceIndex based on the response\n // TODO: once QuestionTypes.MultipleChoiceQuestion is refactored, pass the selected choiceIndex into this method\n const selectedChoiceIndex = question.choices.indexOf(`${response}`)\n\n if (question.branching?.responseValues?.hasOwnProperty(selectedChoiceIndex)) {\n const nextStep = question.branching.responseValues[selectedChoiceIndex]\n\n // Specific question\n if (Number.isInteger(nextStep)) {\n return nextStep\n }\n\n if (nextStep === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n } else if (question.type === SurveyQuestionType.Rating) {\n if (typeof response !== 'number' || !Number.isInteger(response)) {\n throw new Error('The response type must be an integer')\n }\n\n const ratingBucket = getRatingBucketForResponseValue(response, question.scale)\n\n if (question.branching?.responseValues?.hasOwnProperty(ratingBucket)) {\n const nextStep = question.branching.responseValues[ratingBucket]\n\n // Specific question\n if (Number.isInteger(nextStep)) {\n return nextStep\n }\n\n if (nextStep === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n }\n\n return nextQuestionIndex\n }\n\n logger.warn(LOGGER_PREFIX, 'Falling back to next question index due to unexpected branching type')\n return nextQuestionIndex\n }\n\n // this method is lazily loaded onto the window to avoid loading preact and other dependencies if surveys is not enabled\n private _canActivateRepeatedly(survey: Survey) {\n if (isNullish(assignableWindow.__PosthogExtensions__.canActivateRepeatedly)) {\n logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling')\n }\n return assignableWindow.__PosthogExtensions__.canActivateRepeatedly(survey)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"posthog-surveys.js","sourceRoot":"","sources":["../../src/posthog-surveys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAGH,2BAA2B,EAC3B,kBAAkB,GAErB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AACnE,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAEpE,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAE9C,IAAM,aAAa,GAAG,WAAW,CAAA;AAEjC,MAAM,CAAC,IAAM,sBAAsB,GAAmE;IAClG,SAAS,EAAE,UA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,CAAA;YAE1D,KAAI,CAAC,cAAc,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAA;QAC/E,CAAC,CAAC,CAAA;IACN,CAAC;IACL,qBAAC;AAAD,CAAC,AAnPD,IAmPC","sourcesContent":["import { PostHog } from './posthog-core'\nimport { SURVEYS } from './constants'\nimport {\n Survey,\n SurveyCallback,\n SurveyQuestionBranchingType,\n SurveyQuestionType,\n SurveyUrlMatchType,\n} from './posthog-surveys-types'\nimport { isUrlMatchingRegex } from './utils/request-utils'\nimport { SurveyEventReceiver } from './utils/survey-event-receiver'\nimport { assignableWindow, document, window } from './utils/globals'\nimport { DecideResponse } from './types'\nimport { logger } from './utils/logger'\nimport { isNullish } from './utils/type-utils'\n\nconst LOGGER_PREFIX = '[Surveys]'\n\nexport const surveyUrlValidationMap: Record<SurveyUrlMatchType, (conditionsUrl: string) => boolean> = {\n icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1,\n not_icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) === -1,\n regex: (conditionsUrl) => !!window && isUrlMatchingRegex(window.location.href, conditionsUrl),\n not_regex: (conditionsUrl) => !!window && !isUrlMatchingRegex(window.location.href, conditionsUrl),\n exact: (conditionsUrl) => window?.location.href === conditionsUrl,\n is_not: (conditionsUrl) => window?.location.href !== conditionsUrl,\n}\n\nfunction getRatingBucketForResponseValue(responseValue: number, scale: number) {\n if (scale === 3) {\n if (responseValue < 1 || responseValue > 3) {\n throw new Error('The response must be in range 1-3')\n }\n\n return responseValue === 1 ? 'negative' : responseValue === 2 ? 'neutral' : 'positive'\n } else if (scale === 5) {\n if (responseValue < 1 || responseValue > 5) {\n throw new Error('The response must be in range 1-5')\n }\n\n return responseValue <= 2 ? 'negative' : responseValue === 3 ? 'neutral' : 'positive'\n } else if (scale === 10) {\n if (responseValue < 0 || responseValue > 10) {\n throw new Error('The response must be in range 0-10')\n }\n\n return responseValue <= 6 ? 'detractors' : responseValue <= 8 ? 'passives' : 'promoters'\n }\n\n throw new Error('The scale must be one of: 3, 5, 10')\n}\n\nexport class PostHogSurveys {\n private _decideServerResponse?: boolean\n public _surveyEventReceiver: SurveyEventReceiver | null\n private _surveyManager: any\n\n constructor(private readonly instance: PostHog) {\n // we set this to undefined here because we need the persistence storage for this type\n // but that's not initialized until loadIfEnabled is called.\n this._surveyEventReceiver = null\n }\n\n afterDecideResponse(response: DecideResponse) {\n this._decideServerResponse = !!response['surveys']\n this.loadIfEnabled()\n }\n\n loadIfEnabled() {\n const surveysGenerator = assignableWindow?.extendPostHogWithSurveys\n\n if (!this.instance.config.disable_surveys && this._decideServerResponse && !surveysGenerator) {\n if (this._surveyEventReceiver == null) {\n this._surveyEventReceiver = new SurveyEventReceiver(this.instance)\n }\n this.instance.requestRouter.loadScript('/static/surveys.js', (err) => {\n if (err) {\n return logger.error(LOGGER_PREFIX, 'Could not load surveys script', err)\n }\n\n this._surveyManager = assignableWindow.extendPostHogWithSurveys(this.instance)\n })\n }\n }\n\n getSurveys(callback: SurveyCallback, forceReload = false) {\n // In case we manage to load the surveys script, but config says not to load surveys\n // then we shouldn't return survey data\n if (this.instance.config.disable_surveys) {\n return callback([])\n }\n\n if (this._surveyEventReceiver == null) {\n this._surveyEventReceiver = new SurveyEventReceiver(this.instance)\n }\n\n const existingSurveys = this.instance.get_property(SURVEYS)\n if (!existingSurveys || forceReload) {\n this.instance._send_request({\n url: this.instance.requestRouter.endpointFor(\n 'api',\n `/api/surveys/?token=${this.instance.config.token}`\n ),\n method: 'GET',\n transport: 'XHR',\n callback: (response) => {\n if (response.statusCode !== 200 || !response.json) {\n return callback([])\n }\n const surveys = response.json.surveys || []\n\n const eventOrActionBasedSurveys = surveys.filter(\n (survey: Survey) =>\n (survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events?.values?.length > 0) ||\n (survey.conditions?.actions &&\n survey.conditions?.actions?.values &&\n survey.conditions?.actions?.values?.length > 0)\n )\n\n if (eventOrActionBasedSurveys.length > 0) {\n this._surveyEventReceiver?.register(eventOrActionBasedSurveys)\n }\n\n this.instance.persistence?.register({ [SURVEYS]: surveys })\n return callback(surveys)\n },\n })\n } else {\n return callback(existingSurveys)\n }\n }\n\n getActiveMatchingSurveys(callback: SurveyCallback, forceReload = false) {\n this.getSurveys((surveys) => {\n const activeSurveys = surveys.filter((survey) => {\n return !!(survey.start_date && !survey.end_date)\n })\n\n const conditionMatchedSurveys = activeSurveys.filter((survey) => {\n if (!survey.conditions) {\n return true\n }\n\n // use urlMatchType to validate url condition, fallback to contains for backwards compatibility\n const urlCheck = survey.conditions?.url\n ? surveyUrlValidationMap[survey.conditions?.urlMatchType ?? 'icontains'](survey.conditions.url)\n : true\n const selectorCheck = survey.conditions?.selector\n ? document?.querySelector(survey.conditions.selector)\n : true\n return urlCheck && selectorCheck\n })\n\n // get all the surveys that have been activated so far with user actions.\n const activatedSurveys: string[] | undefined = this._surveyEventReceiver?.getSurveys()\n const targetingMatchedSurveys = conditionMatchedSurveys.filter((survey) => {\n if (!survey.linked_flag_key && !survey.targeting_flag_key && !survey.internal_targeting_flag_key) {\n return true\n }\n const linkedFlagCheck = survey.linked_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.linked_flag_key)\n : true\n const targetingFlagCheck = survey.targeting_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.targeting_flag_key)\n : true\n\n const hasEvents =\n survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events?.values.length > 0\n\n const hasActions =\n survey.conditions?.actions &&\n survey.conditions?.actions?.values &&\n survey.conditions?.actions?.values.length > 0\n const eventBasedTargetingFlagCheck =\n hasEvents || hasActions ? activatedSurveys?.includes(survey.id) : true\n\n const overrideInternalTargetingFlagCheck = this._canActivateRepeatedly(survey)\n const internalTargetingFlagCheck =\n survey.internal_targeting_flag_key && !overrideInternalTargetingFlagCheck\n ? this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)\n : true\n\n return (\n linkedFlagCheck && targetingFlagCheck && internalTargetingFlagCheck && eventBasedTargetingFlagCheck\n )\n })\n\n return callback(targetingMatchedSurveys)\n }, forceReload)\n }\n\n getNextSurveyStep(survey: Survey, currentQuestionIndex: number, response: string | string[] | number | null) {\n const question = survey.questions[currentQuestionIndex]\n const nextQuestionIndex = currentQuestionIndex + 1\n\n if (!question.branching?.type) {\n if (currentQuestionIndex === survey.questions.length - 1) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n\n if (question.branching.type === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n } else if (question.branching.type === SurveyQuestionBranchingType.SpecificQuestion) {\n if (Number.isInteger(question.branching.index)) {\n return question.branching.index\n }\n } else if (question.branching.type === SurveyQuestionBranchingType.ResponseBased) {\n // Single choice\n if (question.type === SurveyQuestionType.SingleChoice) {\n // :KLUDGE: for now, look up the choiceIndex based on the response\n // TODO: once QuestionTypes.MultipleChoiceQuestion is refactored, pass the selected choiceIndex into this method\n const selectedChoiceIndex = question.choices.indexOf(`${response}`)\n\n if (question.branching?.responseValues?.hasOwnProperty(selectedChoiceIndex)) {\n const nextStep = question.branching.responseValues[selectedChoiceIndex]\n\n // Specific question\n if (Number.isInteger(nextStep)) {\n return nextStep\n }\n\n if (nextStep === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n } else if (question.type === SurveyQuestionType.Rating) {\n if (typeof response !== 'number' || !Number.isInteger(response)) {\n throw new Error('The response type must be an integer')\n }\n\n const ratingBucket = getRatingBucketForResponseValue(response, question.scale)\n\n if (question.branching?.responseValues?.hasOwnProperty(ratingBucket)) {\n const nextStep = question.branching.responseValues[ratingBucket]\n\n // Specific question\n if (Number.isInteger(nextStep)) {\n return nextStep\n }\n\n if (nextStep === SurveyQuestionBranchingType.End) {\n return SurveyQuestionBranchingType.End\n }\n\n return nextQuestionIndex\n }\n }\n\n return nextQuestionIndex\n }\n\n logger.warn(LOGGER_PREFIX, 'Falling back to next question index due to unexpected branching type')\n return nextQuestionIndex\n }\n\n // this method is lazily loaded onto the window to avoid loading preact and other dependencies if surveys is not enabled\n private _canActivateRepeatedly(survey: Survey) {\n if (isNullish(assignableWindow.__PosthogExtensions__.canActivateRepeatedly)) {\n logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling')\n }\n return assignableWindow.__PosthogExtensions__.canActivateRepeatedly(survey)\n }\n\n canRenderSurvey(surveyId: string) {\n if (isNullish(this._surveyManager)) {\n logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling')\n return\n }\n this.getSurveys((surveys) => {\n const survey = surveys.filter((x) => x.id === surveyId)[0]\n\n this._surveyManager.canRenderSurvey(survey)\n })\n }\n\n renderSurvey(surveyId: string, selector: string) {\n if (isNullish(this._surveyManager)) {\n logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling')\n return\n }\n this.getSurveys((surveys) => {\n const survey = surveys.filter((x) => x.id === surveyId)[0]\n\n this._surveyManager.renderSurvey(survey, document?.querySelector(selector))\n })\n }\n}\n"]}
|
package/package.json
CHANGED