diff --git a/src/extensions/replay/sessionrecording.ts b/src/extensions/replay/sessionrecording.ts index 99ee95e44..c0f64585a 100644 --- a/src/extensions/replay/sessionrecording.ts +++ b/src/extensions/replay/sessionrecording.ts @@ -86,6 +86,7 @@ interface SnapshotBuffer { export class SessionRecording { private instance: PostHog private _endpoint: string + private _api_host?: string private flushBufferTimer?: any private buffer?: SnapshotBuffer private mutationRateLimiter?: MutationRateLimiter @@ -202,6 +203,7 @@ export class SessionRecording { this.instance = instance this._captureStarted = false this._endpoint = BASE_ENDPOINT + this._api_host = null this.stopRrweb = undefined this.receivedDecide = false @@ -298,6 +300,10 @@ export class SessionRecording { this._endpoint = response.sessionRecording?.endpoint } + if (response.sessionRecording?.api_host) { + this._api_host = response.sessionRecording?.api_host + } + if (_isNumber(this._sampleRate)) { this.sessionManager.onSessionId((sessionId) => { this.makeSamplingDecision(sessionId) @@ -714,6 +720,7 @@ export class SessionRecording { transport: 'XHR', method: 'POST', endpoint: this._endpoint, + api_host: this._api_host, _noTruncate: true, _batchKey: SESSION_RECORDING_BATCH_KEY, _metrics: { diff --git a/src/posthog-core.ts b/src/posthog-core.ts index 55ea3f212..a977c3678 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -297,6 +297,7 @@ export class PostHog { __autocapture: boolean | AutocaptureConfig | undefined decideEndpointWasHit: boolean analyticsDefaultEndpoint: string + analyticsDefaultApiHost?: string elementsChainAsString: boolean SentryIntegration: typeof SentryIntegration @@ -321,6 +322,7 @@ export class PostHog { this.__autocapture = undefined this._jsc = function () {} as JSC this.analyticsDefaultEndpoint = '/e/' + this.analyticsDefaultApiHost = null this.elementsChainAsString = false this.featureFlags = new PostHogFeatureFlags(this) @@ -549,6 +551,10 @@ export class PostHog { this.analyticsDefaultEndpoint = response.analytics.endpoint } + if (response.analytics?.api_host) { + this.analyticsDefaultApiHost = response.analytics.api_host + } + if (response.elementsChainAsString) { this.elementsChainAsString = response.elementsChainAsString } @@ -904,7 +910,7 @@ export class PostHog { logger.info('send', data) const jsonData = JSON.stringify(data) - const url = this.config.api_host + (options.endpoint || this.analyticsDefaultEndpoint) + const url = (options.api_host || this.analyticsDefaultApiHost || this.config.api_host) + (options.endpoint || this.analyticsDefaultEndpoint) const has_unique_traits = options !== __NOOPTIONS diff --git a/src/types.ts b/src/types.ts index c64164b10..c99ecbc52 100644 --- a/src/types.ts +++ b/src/types.ts @@ -192,6 +192,7 @@ export interface CaptureOptions extends XHROptions { _metrics?: Properties _noTruncate?: boolean /** if set, overrides and disables config.properties_string_max_length */ endpoint?: string /** defaults to '/e/' */ + api_host?: string /** defaults to global api host */ send_instantly?: boolean /** if set skips the batched queue */ timestamp?: Date } @@ -230,6 +231,7 @@ export interface DecideResponse { capturePerformance?: boolean analytics?: { endpoint?: string + api_host?: string } elementsChainAsString?: boolean // this is currently in development and may have breaking changes without a major version bump @@ -241,6 +243,7 @@ export interface DecideResponse { } sessionRecording?: { endpoint?: string + api_host?: string consoleLogRecordingEnabled?: boolean recorderVersion?: 'v1' | 'v2' // the API returns a decimal between 0 and 1 as a string