posthog-js 1.136.8 → 1.138.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 +1 -1
- package/dist/array.full.js.map +1 -1
- package/dist/array.js +1 -1
- package/dist/array.js.map +1 -1
- package/dist/es.js +1 -1
- package/dist/es.js.map +1 -1
- package/dist/lib/src/constants.d.ts +1 -0
- package/dist/lib/src/extensions/surveys/components/ConfirmationMessage.d.ts +6 -3
- package/dist/lib/src/extensions/surveys/components/QuestionHeader.d.ts +7 -3
- package/dist/lib/src/extensions/surveys/components/QuestionTypes.d.ts +8 -4
- package/dist/lib/src/extensions/surveys/surveys-utils.d.ts +13 -0
- package/dist/lib/src/extensions/surveys.d.ts +17 -5
- package/dist/lib/src/posthog-surveys-types.d.ts +8 -0
- package/dist/lib/src/posthog-surveys.d.ts +2 -0
- package/dist/lib/src/utils/survey-event-receiver.d.ts +12 -0
- package/dist/module.d.ts +21 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/surveys-module-previews.js +1 -1
- package/dist/surveys-module-previews.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/constants.d.ts +1 -0
- package/lib/src/constants.js +1 -0
- package/lib/src/constants.js.map +1 -1
- package/lib/src/extensions/surveys/components/ConfirmationMessage.d.ts +6 -3
- package/lib/src/extensions/surveys/components/ConfirmationMessage.jsx +10 -3
- package/lib/src/extensions/surveys/components/ConfirmationMessage.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/QuestionHeader.d.ts +7 -3
- package/lib/src/extensions/surveys/components/QuestionHeader.jsx +9 -3
- package/lib/src/extensions/surveys/components/QuestionHeader.jsx.map +1 -1
- package/lib/src/extensions/surveys/components/QuestionTypes.d.ts +8 -4
- package/lib/src/extensions/surveys/components/QuestionTypes.jsx +8 -8
- package/lib/src/extensions/surveys/components/QuestionTypes.jsx.map +1 -1
- package/lib/src/extensions/surveys/surveys-utils.d.ts +13 -0
- package/lib/src/extensions/surveys/surveys-utils.jsx +14 -2
- package/lib/src/extensions/surveys/surveys-utils.jsx.map +1 -1
- package/lib/src/extensions/surveys.d.ts +17 -5
- package/lib/src/extensions/surveys.jsx +35 -22
- package/lib/src/extensions/surveys.jsx.map +1 -1
- package/lib/src/posthog-surveys-types.d.ts +8 -0
- package/lib/src/posthog-surveys-types.js.map +1 -1
- package/lib/src/posthog-surveys.d.ts +2 -0
- package/lib/src/posthog-surveys.js +37 -4
- package/lib/src/posthog-surveys.js.map +1 -1
- package/lib/src/utils/survey-event-receiver.d.ts +12 -0
- package/lib/src/utils/survey-event-receiver.js +75 -0
- package/lib/src/utils/survey-event-receiver.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { SURVEYS } from './constants';
|
|
2
2
|
import { isUrlMatchingRegex } from './utils/request-utils';
|
|
3
|
-
import {
|
|
3
|
+
import { SurveyEventReceiver } from './utils/survey-event-receiver';
|
|
4
|
+
import { assignableWindow, document, window } from './utils/globals';
|
|
4
5
|
import { loadScript } from './utils';
|
|
5
6
|
import { logger } from './utils/logger';
|
|
7
|
+
import { isUndefined } from './utils/type-utils';
|
|
6
8
|
export var surveyUrlValidationMap = {
|
|
7
9
|
icontains: function (conditionsUrl) {
|
|
8
10
|
return !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1;
|
|
@@ -13,6 +15,9 @@ export var surveyUrlValidationMap = {
|
|
|
13
15
|
var PostHogSurveys = /** @class */ (function () {
|
|
14
16
|
function PostHogSurveys(instance) {
|
|
15
17
|
this.instance = instance;
|
|
18
|
+
// we set this to undefined here because we need the persistence storage for this type
|
|
19
|
+
// but that's not initialized until loadIfEnabled is called.
|
|
20
|
+
this._surveyEventReceiver = null;
|
|
16
21
|
}
|
|
17
22
|
PostHogSurveys.prototype.afterDecideResponse = function (response) {
|
|
18
23
|
this._decideServerResponse = !!response['surveys'];
|
|
@@ -22,6 +27,9 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
22
27
|
var _this = this;
|
|
23
28
|
var surveysGenerator = assignableWindow === null || assignableWindow === void 0 ? void 0 : assignableWindow.extendPostHogWithSurveys;
|
|
24
29
|
if (!this.instance.config.disable_surveys && this._decideServerResponse && !surveysGenerator) {
|
|
30
|
+
if (this._surveyEventReceiver == null) {
|
|
31
|
+
this._surveyEventReceiver = new SurveyEventReceiver(this.instance.persistence);
|
|
32
|
+
}
|
|
25
33
|
loadScript(this.instance.requestRouter.endpointFor('assets', '/static/surveys.js'), function (err) {
|
|
26
34
|
if (err) {
|
|
27
35
|
return logger.error("Could not load surveys script", err);
|
|
@@ -38,6 +46,9 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
38
46
|
if (this.instance.config.disable_surveys) {
|
|
39
47
|
return callback([]);
|
|
40
48
|
}
|
|
49
|
+
if (this._surveyEventReceiver == null) {
|
|
50
|
+
this._surveyEventReceiver = new SurveyEventReceiver(this.instance.persistence);
|
|
51
|
+
}
|
|
41
52
|
var existingSurveys = this.instance.get_property(SURVEYS);
|
|
42
53
|
if (!existingSurveys || forceReload) {
|
|
43
54
|
this.instance._send_request({
|
|
@@ -46,12 +57,26 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
46
57
|
transport: 'XHR',
|
|
47
58
|
callback: function (response) {
|
|
48
59
|
var _a;
|
|
49
|
-
var _b;
|
|
60
|
+
var _b, _c;
|
|
50
61
|
if (response.statusCode !== 200 || !response.json) {
|
|
51
62
|
return callback([]);
|
|
52
63
|
}
|
|
53
64
|
var surveys = response.json.surveys || [];
|
|
54
|
-
|
|
65
|
+
var eventBasedSurveys = surveys.filter(function (survey) {
|
|
66
|
+
var _a, _b, _c, _d, _e, _f;
|
|
67
|
+
return ((_a = survey.conditions) === null || _a === void 0 ? void 0 : _a.events) &&
|
|
68
|
+
((_c = (_b = survey.conditions) === null || _b === void 0 ? void 0 : _b.events) === null || _c === void 0 ? void 0 : _c.values) &&
|
|
69
|
+
((_f = (_e = (_d = survey.conditions) === null || _d === void 0 ? void 0 : _d.events) === null || _e === void 0 ? void 0 : _e.values) === null || _f === void 0 ? void 0 : _f.length) > 0;
|
|
70
|
+
});
|
|
71
|
+
if (eventBasedSurveys.length > 0 && !isUndefined(_this.instance._addCaptureHook)) {
|
|
72
|
+
(_b = _this._surveyEventReceiver) === null || _b === void 0 ? void 0 : _b.register(eventBasedSurveys);
|
|
73
|
+
var onEventName = function (eventName) {
|
|
74
|
+
var _a;
|
|
75
|
+
(_a = _this._surveyEventReceiver) === null || _a === void 0 ? void 0 : _a.on(eventName);
|
|
76
|
+
};
|
|
77
|
+
_this.instance._addCaptureHook(onEventName);
|
|
78
|
+
}
|
|
79
|
+
(_c = _this.instance.persistence) === null || _c === void 0 ? void 0 : _c.register((_a = {}, _a[SURVEYS] = surveys, _a));
|
|
55
80
|
return callback(surveys);
|
|
56
81
|
},
|
|
57
82
|
});
|
|
@@ -64,6 +89,7 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
64
89
|
var _this = this;
|
|
65
90
|
if (forceReload === void 0) { forceReload = false; }
|
|
66
91
|
this.getSurveys(function (surveys) {
|
|
92
|
+
var _a;
|
|
67
93
|
var activeSurveys = surveys.filter(function (survey) {
|
|
68
94
|
return !!(survey.start_date && !survey.end_date);
|
|
69
95
|
});
|
|
@@ -81,7 +107,10 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
81
107
|
: true;
|
|
82
108
|
return urlCheck && selectorCheck;
|
|
83
109
|
});
|
|
110
|
+
// get all the surveys that have been activated so far with user actions.
|
|
111
|
+
var activatedSurveys = (_a = _this._surveyEventReceiver) === null || _a === void 0 ? void 0 : _a.getSurveys();
|
|
84
112
|
var targetingMatchedSurveys = conditionMatchedSurveys.filter(function (survey) {
|
|
113
|
+
var _a, _b, _c, _d, _e;
|
|
85
114
|
if (!survey.linked_flag_key && !survey.targeting_flag_key && !survey.internal_targeting_flag_key) {
|
|
86
115
|
return true;
|
|
87
116
|
}
|
|
@@ -94,7 +123,11 @@ var PostHogSurveys = /** @class */ (function () {
|
|
|
94
123
|
var internalTargetingFlagCheck = survey.internal_targeting_flag_key
|
|
95
124
|
? _this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)
|
|
96
125
|
: true;
|
|
97
|
-
|
|
126
|
+
var hasEvents = ((_a = survey.conditions) === null || _a === void 0 ? void 0 : _a.events) &&
|
|
127
|
+
((_c = (_b = survey.conditions) === null || _b === void 0 ? void 0 : _b.events) === null || _c === void 0 ? void 0 : _c.values) &&
|
|
128
|
+
((_e = (_d = survey.conditions) === null || _d === void 0 ? void 0 : _d.events) === null || _e === void 0 ? void 0 : _e.values.length) > 0;
|
|
129
|
+
var eventBasedTargetingFlagCheck = hasEvents ? activatedSurveys === null || activatedSurveys === void 0 ? void 0 : activatedSurveys.includes(survey.id) : true;
|
|
130
|
+
return (linkedFlagCheck && targetingFlagCheck && internalTargetingFlagCheck && eventBasedTargetingFlagCheck);
|
|
98
131
|
});
|
|
99
132
|
return callback(targetingMatchedSurveys);
|
|
100
133
|
}, forceReload);
|
|
@@ -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;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEpE,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAEvC,MAAM,CAAC,IAAM,sBAAsB,GAAmE;IAClG,SAAS,EAAE,UAAC,aAAa;QACrB,OAAA,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WA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CAAC,YAAY,CAAC,gBAAgB,CAAC,MAAM,CAAC,kBAAkB,CAAC;oBACxE,CAAC,CAAC,IAAI,CAAA;gBAEV,IAAM,0BAA0B,GAAG,MAAM,CAAC,2BAA2B;oBACjE,CAAC,CAAC,KAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,gBAAgB,CAAC,MAAM,CAAC,2BAA2B,CAAC;oBACjF,CAAC,CAAC,IAAI,CAAA;gBAEV,OAAO,eAAe,IAAI,kBAAkB,IAAI,0BAA0B,CAAA;YAC9E,CAAC,CAAC,CAAA;YAEF,OAAO,QAAQ,CAAC,uBAAuB,CAAC,CAAA;QAC5C,CAAC,EAAE,WAAW,CAAC,CAAA;IACnB,CAAC;IACL,qBAAC;AAAD,CAAC,AAhGD,IAgGC","sourcesContent":["import { PostHog } from './posthog-core'\nimport { SURVEYS } from './constants'\nimport { SurveyCallback, SurveyUrlMatchType } from './posthog-surveys-types'\nimport { isUrlMatchingRegex } from './utils/request-utils'\nimport { window, document, assignableWindow } from './utils/globals'\nimport { DecideResponse } from './types'\nimport { loadScript } from './utils'\nimport { logger } from './utils/logger'\n\nexport const surveyUrlValidationMap: Record<SurveyUrlMatchType, (conditionsUrl: string) => boolean> = {\n icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1,\n regex: (conditionsUrl) => !!window && isUrlMatchingRegex(window.location.href, conditionsUrl),\n exact: (conditionsUrl) => window?.location.href === conditionsUrl,\n}\n\nexport class PostHogSurveys {\n instance: PostHog\n private _decideServerResponse?: boolean\n\n constructor(instance: PostHog) {\n this.instance = instance\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 loadScript(this.instance.requestRouter.endpointFor('assets', '/static/surveys.js'), (err) => {\n if (err) {\n return logger.error(`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 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 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 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 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 internalTargetingFlagCheck = survey.internal_targeting_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)\n : true\n\n return linkedFlagCheck && targetingFlagCheck && internalTargetingFlagCheck\n })\n\n return callback(targetingMatchedSurveys)\n }, forceReload)\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;AAErC,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,UAAU,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEhD,MAAM,CAAC,IAAM,sBAAsB,GAAmE;IAClG,SAAS,EAAE,UAAC,aAAa;QACrB,OAAA,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC;IAAxF,CAAwF;IAC5F,KAAK,EAAE,UAAC,aAAa,IAAK,OAAA,CAAC,CAAC,MAAM,IAAI,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,EAAnE,CAAmE;IAC7F,KAAK,EAAE,UAAC,aAAa,IAAK,OAAA,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,QAAQ,CAAC,IAAI,MAAK,aAAa,EAAvC,CAAuC;CACpE,CAAA;AAED;IAKI,wBAAY,QAAiB;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,sFAAsF;QACtF,4DAA4D;QAC5D,IAAI,CAAC,oBAAoB,GAAG,IA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AAhB,gBAAgB,uBAAhB,gBAAgB,CAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;gBAC7F,OAAO,CACH,eAAe,IAAI,kBAAkB,IAAI,0BAA0B,IAAI,4BAA4B,CACtG,CAAA;YACL,CAAC,CAAC,CAAA;YAEF,OAAO,QAAQ,CAAC,uBAAuB,CAAC,CAAA;QAC5C,CAAC,EAAE,WAAW,CAAC,CAAA;IACnB,CAAC;IACL,qBAAC;AAAD,CAAC,AAvID,IAuIC","sourcesContent":["import { PostHog } from './posthog-core'\nimport { SURVEYS } from './constants'\nimport { Survey, SurveyCallback, SurveyUrlMatchType } 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 { loadScript } from './utils'\nimport { logger } from './utils/logger'\nimport { isUndefined } from './utils/type-utils'\n\nexport const surveyUrlValidationMap: Record<SurveyUrlMatchType, (conditionsUrl: string) => boolean> = {\n icontains: (conditionsUrl) =>\n !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1,\n regex: (conditionsUrl) => !!window && isUrlMatchingRegex(window.location.href, conditionsUrl),\n exact: (conditionsUrl) => window?.location.href === conditionsUrl,\n}\n\nexport class PostHogSurveys {\n instance: PostHog\n private _decideServerResponse?: boolean\n public _surveyEventReceiver: SurveyEventReceiver | null\n\n constructor(instance: PostHog) {\n this.instance = instance\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.persistence)\n }\n loadScript(this.instance.requestRouter.endpointFor('assets', '/static/surveys.js'), (err) => {\n if (err) {\n return logger.error(`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.persistence)\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 eventBasedSurveys = surveys.filter(\n (survey: Survey) =>\n survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events?.values?.length > 0\n )\n\n if (eventBasedSurveys.length > 0 && !isUndefined(this.instance._addCaptureHook)) {\n this._surveyEventReceiver?.register(eventBasedSurveys)\n const onEventName = (eventName: string) => {\n this._surveyEventReceiver?.on(eventName)\n }\n this.instance._addCaptureHook(onEventName)\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 internalTargetingFlagCheck = survey.internal_targeting_flag_key\n ? this.instance.featureFlags.isFeatureEnabled(survey.internal_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 const eventBasedTargetingFlagCheck = hasEvents ? activatedSurveys?.includes(survey.id) : true\n return (\n linkedFlagCheck && targetingFlagCheck && internalTargetingFlagCheck && eventBasedTargetingFlagCheck\n )\n })\n\n return callback(targetingMatchedSurveys)\n }, forceReload)\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Survey } from '../posthog-surveys-types';
|
|
2
|
+
import { PostHogPersistence } from '../posthog-persistence';
|
|
3
|
+
export declare class SurveyEventReceiver {
|
|
4
|
+
private readonly eventRegistry;
|
|
5
|
+
private readonly persistence?;
|
|
6
|
+
constructor(persistence?: PostHogPersistence);
|
|
7
|
+
register(surveys: Survey[]): void;
|
|
8
|
+
on(event: string): void;
|
|
9
|
+
getSurveys(): string[];
|
|
10
|
+
getEventRegistry(): Map<string, string[]>;
|
|
11
|
+
private _saveSurveysToStorage;
|
|
12
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
+
if (!m) return o;
|
|
4
|
+
var i = m.call(o), r, ar = [], e;
|
|
5
|
+
try {
|
|
6
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
+
}
|
|
8
|
+
catch (error) { e = { error: error }; }
|
|
9
|
+
finally {
|
|
10
|
+
try {
|
|
11
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
+
}
|
|
13
|
+
finally { if (e) throw e.error; }
|
|
14
|
+
}
|
|
15
|
+
return ar;
|
|
16
|
+
};
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
+
};
|
|
26
|
+
import { SURVEYS_ACTIVATED } from '../constants';
|
|
27
|
+
var SurveyEventReceiver = /** @class */ (function () {
|
|
28
|
+
function SurveyEventReceiver(persistence) {
|
|
29
|
+
this.persistence = persistence;
|
|
30
|
+
this.eventRegistry = new Map();
|
|
31
|
+
}
|
|
32
|
+
SurveyEventReceiver.prototype.register = function (surveys) {
|
|
33
|
+
var _this = this;
|
|
34
|
+
surveys.forEach(function (survey) {
|
|
35
|
+
var _a, _b, _c, _d, _e;
|
|
36
|
+
if (((_a = survey.conditions) === null || _a === void 0 ? void 0 : _a.events) &&
|
|
37
|
+
((_c = (_b = survey.conditions) === null || _b === void 0 ? void 0 : _b.events) === null || _c === void 0 ? void 0 : _c.values) &&
|
|
38
|
+
((_d = survey.conditions) === null || _d === void 0 ? void 0 : _d.events.values.length) > 0) {
|
|
39
|
+
_this.eventRegistry.set(survey.id, (_e = survey.conditions) === null || _e === void 0 ? void 0 : _e.events.values.map(function (e) { return e.name; }));
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
SurveyEventReceiver.prototype.on = function (event) {
|
|
44
|
+
var _a;
|
|
45
|
+
var activatedSurveys = [];
|
|
46
|
+
this.eventRegistry.forEach(function (events, surveyID) {
|
|
47
|
+
if (events.includes(event)) {
|
|
48
|
+
activatedSurveys.push(surveyID);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
var existingActivatedSurveys = (_a = this.persistence) === null || _a === void 0 ? void 0 : _a.props[SURVEYS_ACTIVATED];
|
|
52
|
+
var existingSurveys = existingActivatedSurveys ? existingActivatedSurveys : [];
|
|
53
|
+
var updatedSurveys = existingSurveys.concat(activatedSurveys);
|
|
54
|
+
this._saveSurveysToStorage(updatedSurveys);
|
|
55
|
+
};
|
|
56
|
+
SurveyEventReceiver.prototype.getSurveys = function () {
|
|
57
|
+
var _a;
|
|
58
|
+
var existingActivatedSurveys = (_a = this.persistence) === null || _a === void 0 ? void 0 : _a.props[SURVEYS_ACTIVATED];
|
|
59
|
+
return existingActivatedSurveys ? existingActivatedSurveys : [];
|
|
60
|
+
};
|
|
61
|
+
SurveyEventReceiver.prototype.getEventRegistry = function () {
|
|
62
|
+
return this.eventRegistry;
|
|
63
|
+
};
|
|
64
|
+
SurveyEventReceiver.prototype._saveSurveysToStorage = function (surveys) {
|
|
65
|
+
var _a;
|
|
66
|
+
var _b;
|
|
67
|
+
// we use a new Set here to remove duplicates.
|
|
68
|
+
(_b = this.persistence) === null || _b === void 0 ? void 0 : _b.register((_a = {},
|
|
69
|
+
_a[SURVEYS_ACTIVATED] = __spreadArray([], __read(new Set(surveys)), false),
|
|
70
|
+
_a));
|
|
71
|
+
};
|
|
72
|
+
return SurveyEventReceiver;
|
|
73
|
+
}());
|
|
74
|
+
export { SurveyEventReceiver };
|
|
75
|
+
//# sourceMappingURL=survey-event-receiver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,eAAe,GAAa,wBAAwB,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,EAAE,CAAA;QAC1F,IAAM,cAAc,GAAG,eAAe,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAA;QAC/D,IAAI,CAAC,qBAAqB,CAAC,cAAc,CAAC,CAAA;IAC9C,CAAC;IAED,wCAAU,GAAV;;QACI,IAAM,wBAAwB,GAAG,MAAA,IAAI,CAAC,WAAW,0CAAE,KAAK,CAAC,iBAAiB,CAAC,CAAA;QAC3E,OAAO,wBAAwB,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,EAAE,CAAA;IACnE,CAAC;IAED,8CAAgB,GAAhB;QACI,OAAO,IAAI,CAAC,aAAa,CAAA;IAC7B,CAAC;IAEO,mDAAqB,GAA7B,UAA8B,OAAiB;;;QAC3C,8CAA8C;QAC9C,MAAA,IAAI,CAAC,WAAW,0CAAE,QAAQ;YACtB,GAAC,iBAAiB,6BAAO,IAAI,GAAG,CAAC,OAAO,CAAC,SAAC;gBAC5C,CAAA;IACN,CAAC;IACL,0BAAC;AAAD,CAAC,AAtDD,IAsDC","sourcesContent":["import { Survey } from '../posthog-surveys-types'\nimport { PostHogPersistence } from '../posthog-persistence'\nimport { SURVEYS_ACTIVATED } from '../constants'\n\nexport class SurveyEventReceiver {\n private readonly eventRegistry: Map<string, string[]>\n private readonly persistence?: PostHogPersistence\n\n constructor(persistence?: PostHogPersistence) {\n this.persistence = persistence\n this.eventRegistry = new Map<string, string[]>()\n }\n\n register(surveys: Survey[]): void {\n surveys.forEach((survey) => {\n if (\n survey.conditions?.events &&\n survey.conditions?.events?.values &&\n survey.conditions?.events.values.length > 0\n ) {\n this.eventRegistry.set(\n survey.id,\n survey.conditions?.events.values.map((e) => e.name)\n )\n }\n })\n }\n\n on(event: string): void {\n const activatedSurveys: string[] = []\n\n this.eventRegistry.forEach((events, surveyID) => {\n if (events.includes(event)) {\n activatedSurveys.push(surveyID)\n }\n })\n\n const existingActivatedSurveys = this.persistence?.props[SURVEYS_ACTIVATED]\n const existingSurveys: string[] = existingActivatedSurveys ? existingActivatedSurveys : []\n const updatedSurveys = existingSurveys.concat(activatedSurveys)\n this._saveSurveysToStorage(updatedSurveys)\n }\n\n getSurveys(): string[] {\n const existingActivatedSurveys = this.persistence?.props[SURVEYS_ACTIVATED]\n return existingActivatedSurveys ? existingActivatedSurveys : []\n }\n\n getEventRegistry(): Map<string, string[]> {\n return this.eventRegistry\n }\n\n private _saveSurveysToStorage(surveys: string[]): void {\n // we use a new Set here to remove duplicates.\n this.persistence?.register({\n [SURVEYS_ACTIVATED]: [...new Set(surveys)],\n })\n }\n}\n"]}
|
package/package.json
CHANGED