@posthog/core 1.54.5 → 1.55.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/error-tracking/coercers/dom-exception-coercer.d.ts.map +1 -1
  2. package/dist/error-tracking/coercers/error-coercer.d.ts.map +1 -1
  3. package/dist/error-tracking/coercers/error-event-coercer.d.ts.map +1 -1
  4. package/dist/error-tracking/coercers/event-coercer.d.ts.map +1 -1
  5. package/dist/error-tracking/coercers/object-coercer.d.ts.map +1 -1
  6. package/dist/error-tracking/coercers/primitive-coercer.d.ts.map +1 -1
  7. package/dist/error-tracking/coercers/promise-rejection-event.d.ts.map +1 -1
  8. package/dist/error-tracking/coercers/string-coercer.d.ts.map +1 -1
  9. package/dist/error-tracking/error-properties-builder.d.ts.map +1 -1
  10. package/dist/error-tracking/exception-steps.d.ts +2 -2
  11. package/dist/error-tracking/exception-steps.d.ts.map +1 -1
  12. package/dist/error-tracking/parsers/safari.d.ts.map +1 -1
  13. package/dist/error-tracking/types.d.ts +1 -1
  14. package/dist/error-tracking/types.d.ts.map +1 -1
  15. package/dist/error-tracking/utils.d.ts.map +1 -1
  16. package/dist/eventemitter.d.ts.map +1 -1
  17. package/dist/featureFlagLocalEvaluation.d.ts +2 -0
  18. package/dist/featureFlagLocalEvaluation.d.ts.map +1 -1
  19. package/dist/featureFlagLocalEvaluation.js +5 -1
  20. package/dist/featureFlagLocalEvaluation.mjs +5 -1
  21. package/dist/featureFlagUtils.d.ts +5 -5
  22. package/dist/featureFlagUtils.d.ts.map +1 -1
  23. package/dist/gzip.d.ts.map +1 -1
  24. package/dist/logs/index.d.ts.map +1 -1
  25. package/dist/metrics/index.d.ts.map +1 -1
  26. package/dist/posthog-core-stateless.d.ts.map +1 -1
  27. package/dist/posthog-core.d.ts.map +1 -1
  28. package/dist/surveys/events.d.ts +16 -0
  29. package/dist/surveys/events.d.ts.map +1 -1
  30. package/dist/surveys/events.js +34 -0
  31. package/dist/surveys/events.mjs +29 -1
  32. package/dist/surveys/index.d.ts +1 -1
  33. package/dist/surveys/index.d.ts.map +1 -1
  34. package/dist/surveys/index.js +6 -0
  35. package/dist/surveys/index.mjs +1 -1
  36. package/dist/surveys/property-matching.d.ts.map +1 -1
  37. package/dist/surveys/shuffling.d.ts +1 -1
  38. package/dist/surveys/shuffling.d.ts.map +1 -1
  39. package/dist/surveys/translations.d.ts +1 -1
  40. package/dist/surveys/translations.d.ts.map +1 -1
  41. package/dist/testing/PostHogCoreTestClient.d.ts.map +1 -1
  42. package/dist/testing/test-utils.d.ts.map +1 -1
  43. package/dist/traces/context.d.ts.map +1 -1
  44. package/dist/traces/index.d.ts.map +1 -1
  45. package/dist/traces/index.js +1 -1
  46. package/dist/traces/index.mjs +1 -1
  47. package/dist/traces/span.d.ts.map +1 -1
  48. package/dist/traces/types.d.ts +2 -2
  49. package/dist/traces/types.d.ts.map +1 -1
  50. package/dist/tracing-headers.d.ts.map +1 -1
  51. package/dist/types.d.ts +53 -51
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/types.js +1 -0
  54. package/dist/types.mjs +1 -0
  55. package/dist/utils/bot-detection.d.ts.map +1 -1
  56. package/dist/utils/bucketed-rate-limiter.d.ts.map +1 -1
  57. package/dist/utils/flush-timer.d.ts.map +1 -1
  58. package/dist/utils/index.d.ts.map +1 -1
  59. package/dist/utils/logger.d.ts.map +1 -1
  60. package/dist/utils/promise-queue.d.ts.map +1 -1
  61. package/dist/utils/retry-after.d.ts.map +1 -1
  62. package/dist/utils/string-utils.d.ts.map +1 -1
  63. package/dist/utils/type-utils.d.ts.map +1 -1
  64. package/dist/utils/user-agent-utils.d.ts.map +1 -1
  65. package/dist/vendor/uuidv7.d.ts.map +1 -1
  66. package/package.json +3 -2
  67. package/src/featureFlagLocalEvaluation.ts +12 -1
  68. package/src/surveys/events.spec.ts +90 -0
  69. package/src/surveys/events.ts +42 -0
  70. package/src/surveys/index.ts +2 -0
  71. package/src/traces/index.spec.ts +19 -6
  72. package/src/traces/index.ts +6 -3
  73. package/src/traces/types.ts +2 -2
  74. package/src/types.ts +2 -0
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, it } from 'vitest'
2
2
  import {
3
3
  buildSurveyResponseProperties,
4
+ buildSurveyResponseEventProperties,
5
+ recordSurveyAnswer,
4
6
  getSurveyInteractionProperty,
5
7
  getSurveyResponseKey,
6
8
  getSurveyResponseValue,
@@ -91,3 +93,91 @@ describe('survey event helpers', () => {
91
93
  expect(getSurveyInteractionProperty(survey, 'responded')).toBe('$survey_responded/survey-1/2')
92
94
  })
93
95
  })
96
+
97
+ describe('recordSurveyAnswer', () => {
98
+ it.each([0, '', null, ['fast', 'clear']])('records %j without mutating previous answers or snapshots', (response) => {
99
+ const previous = {
100
+ responses: { $survey_response_q1: 5 },
101
+ questionSnapshots: { q1: 'Rate us (French)' },
102
+ }
103
+ expect(recordSurveyAnswer(previous, 'q2', response, { id: 'q2', question: 'Anything else?' })).toEqual({
104
+ responses: { $survey_response_q1: 5, $survey_response_q2: response },
105
+ questionSnapshots: { q1: 'Rate us (French)', q2: 'Anything else?' },
106
+ })
107
+ expect(previous).toEqual({
108
+ responses: { $survey_response_q1: 5 },
109
+ questionSnapshots: { q1: 'Rate us (French)' },
110
+ })
111
+ })
112
+
113
+ it('replaces the answer and its snapshot when a question is answered again', () => {
114
+ expect(
115
+ recordSurveyAnswer(
116
+ {
117
+ responses: { $survey_response_q1: 5 },
118
+ questionSnapshots: { q1: 'Rate us (French)' },
119
+ },
120
+ 'q1',
121
+ 0,
122
+ { id: 'q1', question: '' }
123
+ )
124
+ ).toEqual({
125
+ responses: { $survey_response_q1: 0 },
126
+ questionSnapshots: { q1: '' },
127
+ })
128
+ })
129
+
130
+ it('accepts progress without snapshots and does not invent missing question text', () => {
131
+ expect(recordSurveyAnswer({ responses: {} }, 'q1', 5)).toEqual({
132
+ responses: { $survey_response_q1: 5 },
133
+ questionSnapshots: {},
134
+ })
135
+ })
136
+ })
137
+
138
+ describe('buildSurveyResponseEventProperties', () => {
139
+ const survey = { questions: [{ id: 'q1', question: 'Rate us', originalQuestionIndex: 0 }] }
140
+
141
+ it.each([
142
+ ['sent', false, { $survey_completed: false }],
143
+ ['sent', true, { $survey_completed: true }],
144
+ ['dismissed', undefined, { $survey_partially_completed: true }],
145
+ ['abandoned', undefined, { $survey_partially_completed: true }],
146
+ ] as const)('builds %s event properties (completed=%s) with answer-time text', (event, completed, metadata) => {
147
+ expect(
148
+ buildSurveyResponseEventProperties({
149
+ event,
150
+ survey,
151
+ completed,
152
+ submissionId: 'submission-1',
153
+ surveyLanguage: 'fr',
154
+ responses: { $survey_response_q1: 0 },
155
+ questionSnapshots: { q1: 'Notez-nous' },
156
+ })
157
+ ).toEqual({
158
+ ...metadata,
159
+ $survey_submission_id: 'submission-1',
160
+ $survey_language: 'fr',
161
+ $survey_questions: [{ id: 'q1', question: 'Notez-nous', response: 0 }],
162
+ $survey_response_q1: 0,
163
+ $survey_response: 0,
164
+ })
165
+ })
166
+
167
+ it('keeps metadata optional for callers without submission state or a matched language', () => {
168
+ const properties = buildSurveyResponseEventProperties({ event: 'sent', survey, surveyLanguage: null })
169
+ expect(properties).not.toHaveProperty('$survey_submission_id')
170
+ expect(properties).not.toHaveProperty('$survey_completed')
171
+ expect(properties).not.toHaveProperty('$survey_partially_completed')
172
+ expect(properties).not.toHaveProperty('$survey_language')
173
+ })
174
+
175
+ it.each([{}, { $survey_response_q1: null }])(
176
+ 'does not count absent or skipped answers as partial completion',
177
+ (responses) => {
178
+ expect(buildSurveyResponseEventProperties({ event: 'dismissed', survey, responses })).toMatchObject({
179
+ $survey_partially_completed: false,
180
+ })
181
+ }
182
+ )
183
+ })
@@ -54,6 +54,48 @@ export function buildSurveyResponseProperties(
54
54
  }
55
55
  }
56
56
 
57
+ export function recordSurveyAnswer(
58
+ { responses, questionSnapshots = {} }: { responses: SurveyResponses; questionSnapshots?: Record<string, string> },
59
+ questionId: string,
60
+ response: SurveyResponseValue,
61
+ question?: SurveyQuestionForResponses
62
+ ): { responses: SurveyResponses; questionSnapshots: Record<string, string> } {
63
+ // Snapshot the question text as it appeared to the user right now, so that
64
+ // $survey_questions[].question in sent/dismissed events reflects the language
65
+ // the user saw when they answered, not the language active at event-fire time.
66
+ return {
67
+ responses: { ...responses, [getSurveyResponseKey(questionId)]: response },
68
+ questionSnapshots: question?.id ? { ...questionSnapshots, [question.id]: question.question } : questionSnapshots,
69
+ }
70
+ }
71
+
72
+ export function buildSurveyResponseEventProperties({
73
+ event,
74
+ survey,
75
+ responses = {},
76
+ submissionId,
77
+ completed,
78
+ surveyLanguage,
79
+ questionSnapshots,
80
+ }: {
81
+ event: 'sent' | 'dismissed' | 'abandoned'
82
+ survey: SurveyForResponses
83
+ responses?: SurveyResponses
84
+ submissionId?: string
85
+ completed?: boolean
86
+ surveyLanguage?: string | null
87
+ questionSnapshots?: Record<string, string>
88
+ }): Record<string, unknown> {
89
+ return {
90
+ ...(!isUndefined(submissionId) && { $survey_submission_id: submissionId }),
91
+ ...(event === 'sent'
92
+ ? !isUndefined(completed) && { $survey_completed: completed }
93
+ : { $survey_partially_completed: surveyHasResponses(responses) }),
94
+ ...(surveyLanguage && { [SURVEY_LANGUAGE_PROPERTY]: surveyLanguage }),
95
+ ...buildSurveyResponseProperties(responses, survey, questionSnapshots),
96
+ }
97
+ }
98
+
57
99
  export function surveyHasResponses(responses: SurveyResponses = {}): boolean {
58
100
  return Object.values(responses).some((response) => !isNullish(response))
59
101
  }
@@ -1,6 +1,8 @@
1
1
  export { getValidationError, getLengthFromRules, getRequirementsHint } from './validation'
2
2
  export {
3
3
  buildSurveyResponseProperties,
4
+ buildSurveyResponseEventProperties,
5
+ recordSurveyAnswer,
4
6
  getSurveyInteractionProperty,
5
7
  getSurveyOldResponseKey,
6
8
  getSurveyResponseKey,
@@ -3192,11 +3192,11 @@ describe('PostHogTraces', () => {
3192
3192
  })
3193
3193
 
3194
3194
  it('never exports a span evicted for exceeding maxSpanAgeMs', async () => {
3195
- const traces = createTraces({ maxSpanAgeMs: 60_000 })
3195
+ const traces = createTraces({ maxLiveSpans: 1, maxSpanAgeMs: 60_000 })
3196
3196
  const leaked = traces.startSpan('leaked')
3197
3197
 
3198
3198
  await vi.advanceTimersByTimeAsync(61_000)
3199
- // Eviction is lazy: the next startSpan sweeps.
3199
+ // Eviction is lazy: the next startSpan at the bound sweeps.
3200
3200
  traces.startSpan('later').end()
3201
3201
  leaked.end()
3202
3202
  await traces.flush()
@@ -3205,9 +3205,22 @@ describe('PostHogTraces', () => {
3205
3205
  expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('still live after 60000ms'))
3206
3206
  })
3207
3207
 
3208
+ it('exports a long span that ends while under the bound', async () => {
3209
+ const traces = createTraces({ maxSpanAgeMs: 60_000 })
3210
+ const longRunning = traces.startSpan('batch')
3211
+
3212
+ await vi.advanceTimersByTimeAsync(61_000)
3213
+ traces.startSpan('probe').end()
3214
+ longRunning.end()
3215
+ await traces.flush()
3216
+
3217
+ expect(sentSpans().map((s) => s.name)).toEqual(['probe', 'batch'])
3218
+ })
3219
+
3208
3220
  it('returns the slot on age eviction so a leak cannot disable tracing', async () => {
3209
3221
  const traces = createTraces({ maxLiveSpans: 1, maxSpanAgeMs: 60_000 })
3210
3222
  traces.startSpan('leaked-forever')
3223
+ expect(traces.startSpan('refused')).toBe(NOOP_SPAN)
3211
3224
 
3212
3225
  await vi.advanceTimersByTimeAsync(61_000)
3213
3226
  traces.startSpan('after-the-leak').end()
@@ -3217,15 +3230,15 @@ describe('PostHogTraces', () => {
3217
3230
  })
3218
3231
 
3219
3232
  it('ages from startSpan, not from a caller-supplied startTime', async () => {
3220
- const traces = createTraces({ maxSpanAgeMs: 60_000 })
3221
- // Backdated an hour: aging off the supplied time would evict it immediately.
3233
+ const traces = createTraces({ maxLiveSpans: 1, maxSpanAgeMs: 60_000 })
3234
+ // Backdated an hour: aging off the supplied time would evict it at the next sweep.
3222
3235
  const backdated = traces.startSpan('backdated', { startTime: Date.now() - 3_600_000 })
3223
3236
 
3224
- traces.startSpan('sweep-trigger').end()
3237
+ expect(traces.startSpan('sweep-trigger')).toBe(NOOP_SPAN)
3225
3238
  backdated.end()
3226
3239
  await traces.flush()
3227
3240
 
3228
- expect(sentSpans().map((s) => s.name)).toEqual(['sweep-trigger', 'backdated'])
3241
+ expect(sentSpans().map((s) => s.name)).toEqual(['backdated'])
3229
3242
  })
3230
3243
  })
3231
3244
 
@@ -237,9 +237,12 @@ export class PostHogTraces {
237
237
 
238
238
  const parent = this._resolveParent(explicitParent, options)
239
239
 
240
- // Swept before the bound is read, so a process that has leaked its way to
241
- // the bound recovers on the first `startSpan` after the leaks age out.
242
- this._evictAgedSpans()
240
+ // Swept only at the bound, so a long span that does end is still exported,
241
+ // while a process that has leaked its way to the bound recovers on the
242
+ // first `startSpan` after the leaks age out.
243
+ if (this._liveSpans.size >= this._config.maxLiveSpans) {
244
+ this._evictAgedSpans()
245
+ }
243
246
  if (this._liveSpans.size >= this._config.maxLiveSpans) {
244
247
  this._recordDrop(
245
248
  1,
@@ -133,8 +133,8 @@ export interface ResolvedTracesConfig extends TracesConfig {
133
133
  maxEventsPerSpan: number
134
134
  maxAttributesPerEvent: number
135
135
  maxAttributeValueLength: number
136
- /** Bound on spans started but not yet ended. At the bound `startSpan` returns a no-op handle. */
136
+ /** Bound on spans started but not yet ended. At the bound `startSpan` evicts aged spans, then returns a no-op handle if still at the bound. */
137
137
  maxLiveSpans: number
138
- /** How long a span may stay live before it stops being accounted for and can never export. */
138
+ /** Age at which a live span counts as leaked. Checked only at `maxLiveSpans`, where aged spans are evicted and never exported. */
139
139
  maxSpanAgeMs: number
140
140
  }
package/src/types.ts CHANGED
@@ -292,6 +292,7 @@ export enum PostHogPersistedProperty {
292
292
  // Session id for which an event trigger has activated session replay. only used by posthog-react-native
293
293
  SessionReplayEventTriggerActivatedSession = 'session_replay_event_trigger_activated_session',
294
294
  SurveyLastSeenDate = 'survey_last_seen_date', // only used by posthog-react-native
295
+ SurveysInProgress = 'surveys_in_progress', // only used by posthog-react-native
295
296
  SurveysSeen = 'surveys_seen', // only used by posthog-react-native
296
297
  Surveys = 'surveys', // only used by posthog-react-native
297
298
  RemoteConfig = 'remote_config',
@@ -912,6 +913,7 @@ export type Survey = {
912
913
  current_iteration?: number | null
913
914
  current_iteration_start_date?: string | null
914
915
  schedule?: SurveySchedule | null
916
+ enable_partial_responses?: boolean | null
915
917
  }
916
918
 
917
919
  export type SurveyActionType = {