Skip to content

Commit

Permalink
Allow overriding api_host for analytics/session recording
Browse files Browse the repository at this point in the history
Currently we can override the endpoint path but not the hostname
  • Loading branch information
frankh committed Dec 20, 2023
1 parent 958fec4 commit fb27b65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -202,6 +203,7 @@ export class SessionRecording {
this.instance = instance
this._captureStarted = false
this._endpoint = BASE_ENDPOINT
this._api_host = null

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Cypress

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Test with React

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Cypress

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Test on Chrome

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Lint

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Test on IE11

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 206 in src/extensions/replay/sessionrecording.ts

View workflow job for this annotation

GitHub Actions / Test on Safari

Type 'null' is not assignable to type 'string | undefined'.
this.stopRrweb = undefined
this.receivedDecide = false

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: {
Expand Down
8 changes: 7 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export class PostHog {
__autocapture: boolean | AutocaptureConfig | undefined
decideEndpointWasHit: boolean
analyticsDefaultEndpoint: string
analyticsDefaultApiHost?: string
elementsChainAsString: boolean

SentryIntegration: typeof SentryIntegration
Expand All @@ -321,6 +322,7 @@ export class PostHog {
this.__autocapture = undefined
this._jsc = function () {} as JSC
this.analyticsDefaultEndpoint = '/e/'
this.analyticsDefaultApiHost = null

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Cypress

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Test with React

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Cypress

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Test on Chrome

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Lint

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Test on IE11

Type 'null' is not assignable to type 'string | undefined'.

Check failure on line 325 in src/posthog-core.ts

View workflow job for this annotation

GitHub Actions / Test on Safari

Type 'null' is not assignable to type 'string | undefined'.
this.elementsChainAsString = false

this.featureFlags = new PostHogFeatureFlags(this)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit fb27b65

Please sign in to comment.