posthog-js 1.148.1 → 1.148.2

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/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js",
3
- "version": "1.148.1",
3
+ "version": "1.148.2",
4
4
  "description": "Posthog-js allows you to automatically capture usage and send events to PostHog.",
5
5
  "repository": "https://github.com/PostHog/posthog-js",
6
6
  "author": "hey@posthog.com",
@@ -1,7 +1,10 @@
1
1
  import { generateSurveys } from '../extensions/surveys';
2
2
  import { window } from '../utils/globals';
3
+ import { canActivateRepeatedly } from '../extensions/surveys/surveys-utils';
3
4
  if (window) {
4
5
  ;
6
+ window.__PosthogExtensions__ = window.__PosthogExtensions__ || {};
7
+ window.__PosthogExtensions__.canActivateRepeatedly = canActivateRepeatedly;
5
8
  window.extendPostHogWithSurveys = generateSurveys;
6
9
  }
7
10
  export default generateSurveys;
@@ -1 +1 @@
1
- {"version":3,"file":"surveys.js","sourceRoot":"","sources":["../../../src/entrypoints/surveys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEzC,IAAI,MAAM,EAAE;IACR,CAAC;IAAC,MAAc,CAAC,wBAAwB,GAAG,eAAe,CAAA;CAC9D;AAED,eAAe,eAAe,CAAA","sourcesContent":["import { generateSurveys } from '../extensions/surveys'\n\nimport { window } from '../utils/globals'\n\nif (window) {\n ;(window as any).extendPostHogWithSurveys = generateSurveys\n}\n\nexport default generateSurveys\n"]}
1
+ {"version":3,"file":"surveys.js","sourceRoot":"","sources":["../../../src/entrypoints/surveys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAEvD,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAE3E,IAAI,MAAM,EAAE;IACR,CAAC;IAAC,MAAc,CAAC,qBAAqB,GAAI,MAAc,CAAC,qBAAqB,IAAI,EAAE,CACnF;IAAC,MAAc,CAAC,qBAAqB,CAAC,qBAAqB,GAAG,qBAAqB,CACnF;IAAC,MAAc,CAAC,wBAAwB,GAAG,eAAe,CAAA;CAC9D;AAED,eAAe,eAAe,CAAA","sourcesContent":["import { generateSurveys } from '../extensions/surveys'\n\nimport { window } from '../utils/globals'\nimport { canActivateRepeatedly } from '../extensions/surveys/surveys-utils'\n\nif (window) {\n ;(window as any).__PosthogExtensions__ = (window as any).__PosthogExtensions__ || {}\n ;(window as any).__PosthogExtensions__.canActivateRepeatedly = canActivateRepeatedly\n ;(window as any).extendPostHogWithSurveys = generateSurveys\n}\n\nexport default generateSurveys\n"]}
@@ -4,7 +4,7 @@ import { SurveyEventReceiver } from './utils/survey-event-receiver';
4
4
  import { DecideResponse } from './types';
5
5
  export declare const surveyUrlValidationMap: Record<SurveyUrlMatchType, (conditionsUrl: string) => boolean>;
6
6
  export declare class PostHogSurveys {
7
- instance: PostHog;
7
+ private readonly instance;
8
8
  private _decideServerResponse?;
9
9
  _surveyEventReceiver: SurveyEventReceiver | null;
10
10
  constructor(instance: PostHog);
@@ -13,4 +13,5 @@ export declare class PostHogSurveys {
13
13
  getSurveys(callback: SurveyCallback, forceReload?: boolean): void;
14
14
  getActiveMatchingSurveys(callback: SurveyCallback, forceReload?: boolean): void;
15
15
  getNextSurveyStep(survey: Survey, currentQuestionIndex: number, response: string | string[] | number | null): any;
16
+ private _canActivateRepeatedly;
16
17
  }
@@ -4,7 +4,8 @@ import { isUrlMatchingRegex } from './utils/request-utils';
4
4
  import { SurveyEventReceiver } from './utils/survey-event-receiver';
5
5
  import { assignableWindow, document, window } from './utils/globals';
6
6
  import { logger } from './utils/logger';
7
- import { canActivateRepeatedly } from './extensions/surveys/surveys-utils';
7
+ import { isNullish } from './utils/type-utils';
8
+ var LOGGER_PREFIX = '[Surveys]';
8
9
  export var surveyUrlValidationMap = {
9
10
  icontains: function (conditionsUrl) {
10
11
  return !!window && window.location.href.toLowerCase().indexOf(conditionsUrl.toLowerCase()) > -1;
@@ -58,7 +59,7 @@ var PostHogSurveys = /** @class */ (function () {
58
59
  }
59
60
  this.instance.requestRouter.loadScript('/static/surveys.js', function (err) {
60
61
  if (err) {
61
- return logger.error("Could not load surveys script", err);
62
+ return logger.error(LOGGER_PREFIX, 'Could not load surveys script', err);
62
63
  }
63
64
  assignableWindow.extendPostHogWithSurveys(_this.instance);
64
65
  });
@@ -151,7 +152,7 @@ var PostHogSurveys = /** @class */ (function () {
151
152
  ((_h = (_g = survey.conditions) === null || _g === void 0 ? void 0 : _g.actions) === null || _h === void 0 ? void 0 : _h.values) &&
152
153
  ((_k = (_j = survey.conditions) === null || _j === void 0 ? void 0 : _j.actions) === null || _k === void 0 ? void 0 : _k.values.length) > 0;
153
154
  var eventBasedTargetingFlagCheck = hasEvents || hasActions ? activatedSurveys === null || activatedSurveys === void 0 ? void 0 : activatedSurveys.includes(survey.id) : true;
154
- var overrideInternalTargetingFlagCheck = canActivateRepeatedly(survey);
155
+ var overrideInternalTargetingFlagCheck = _this._canActivateRepeatedly(survey);
155
156
  var internalTargetingFlagCheck = survey.internal_targeting_flag_key && !overrideInternalTargetingFlagCheck
156
157
  ? _this.instance.featureFlags.isFeatureEnabled(survey.internal_targeting_flag_key)
157
158
  : true;
@@ -215,9 +216,16 @@ var PostHogSurveys = /** @class */ (function () {
215
216
  }
216
217
  return nextQuestionIndex;
217
218
  }
218
- console.warn('Falling back to next question index due to unexpected branching type'); // eslint-disable-line no-console
219
+ logger.warn(LOGGER_PREFIX, 'Falling back to next question index due to unexpected branching type');
219
220
  return nextQuestionIndex;
220
221
  };
222
+ // this method is lazily loaded onto the window to avoid loading preact and other dependencies if surveys is not enabled
223
+ PostHogSurveys.prototype._canActivateRepeatedly = function (survey) {
224
+ if (isNullish(assignableWindow.__PosthogExtensions__.canActivateRepeatedly)) {
225
+ logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling');
226
+ }
227
+ return assignableWindow.__PosthogExtensions__.canActivateRepeatedly(survey);
228
+ };
221
229
  return PostHogSurveys;
222
230
  }());
223
231
  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,qBAAqB,EAAE,MAAM,oCAAoC,CAAA;AAE1E,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,aAAa,EAAE,UAAC,aAAa;QACzB,OAAA,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC;IAA1F,CAA0F;IAC9F,KAAK,EAAE,UAAC,aAAa,IAAK,OAAA,CAAC,CAAC,MAAM,IAAI,kBAAkB,CAAC,MAAM,CAAC,QAAQ,CAAC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kBAAkB,CAAC,MAAM,EAAE;gBACpD,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE;oBAC7D,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAA;iBAC1D;gBAED,IAAM,YAAY,GAAG,+BAA+B,CAAC,QAAQ,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;gBAE9E,IAAI,MAAA,MAAA,QAAQ,CAAC,SAAS,0CAAE,cAAc,0CAAE,cAAc,CAAC,YAAY,CAAC,EAAE;oBAClE,IAAM,QAAQ,GAAG,QAAQ,CAAC,SAAS,CAAC,cAAc,CAAC,YAAY,CAAC,CAAA;oBAEhE,oBAAoB;oBACpB,IAAI,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE;wBAC5B,OAAO,QAAQ,CAAA;qBAClB;oBAED,IAAI,QAAQ,KAAK,2BAA2B,CAAC,GAAG,EAAE;wBAC9C,OAAO,2BAA2B,CAAC,GAAG,CAAA;qBACzC;oBAED,OAAO,iBAAiB,CAAA;iBAC3B;aACJ;YAED,OAAO,iBAAiB,CAAA;SAC3B;QAED,OAAO,CAAC,IAAI,CAAC,sEAAsE,CAAC,CAAA,CAAC,iCAAiC;QACtH,OAAO,iBAAiB,CAAA;IAC5B,CAAC;IACL,qBAAC;AAAD,CAAC,AApND,IAoNC","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 { canActivateRepeatedly } from './extensions/surveys/surveys-utils'\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 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)\n }\n this.instance.requestRouter.loadScript('/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)\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 = 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 console.warn('Falling back to next question index due to unexpected branching type') // eslint-disable-line no-console\n return nextQuestionIndex\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,EA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AO,2BAA2B,CAAC,GAAG,CAAA;qBACzC;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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js",
3
- "version": "1.148.1",
3
+ "version": "1.148.2",
4
4
  "description": "Posthog-js allows you to automatically capture usage and send events to PostHog.",
5
5
  "repository": "https://github.com/PostHog/posthog-js",
6
6
  "author": "hey@posthog.com",