File tree Expand file tree Collapse file tree 4 files changed +57
-2
lines changed
Expand file tree Collapse file tree 4 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' @segment/analytics-next ' : minor
3+ ---
4+
5+ Allow custom metrics endpoint on load
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import {
2424 lowEntropyTestData ,
2525} from '../../test-helpers/fixtures/client-hints'
2626import { getGlobalAnalytics , NullAnalytics } from '../..'
27+ import { recordIntegrationMetric } from '../../core/stats/metric-helpers'
2728
2829let fetchCalls : ReturnType < typeof parseFetchCall > [ ] = [ ]
2930
@@ -655,6 +656,43 @@ describe('Dispatch', () => {
655656 ]
656657 ` )
657658 } )
659+
660+ it ( 'respects api and protocol overrides for metrics endpoint' , async ( ) => {
661+ const [ ajs ] = await AnalyticsBrowser . load (
662+ {
663+ writeKey,
664+ cdnSettings : {
665+ integrations : {
666+ 'Segment.io' : {
667+ apiHost : 'cdnSettings.api.io' ,
668+ } ,
669+ } ,
670+ metrics : {
671+ flushTimer : 0 ,
672+ } ,
673+ } ,
674+ } ,
675+ {
676+ integrations : {
677+ 'Segment.io' : {
678+ apiHost : 'new.api.io' ,
679+ protocol : 'http' ,
680+ } ,
681+ } ,
682+ }
683+ )
684+
685+ const event = await ajs . track ( 'foo' )
686+
687+ recordIntegrationMetric ( event , {
688+ integrationName : 'foo' ,
689+ methodName : 'bar' ,
690+ type : 'action' ,
691+ } )
692+
693+ await sleep ( 10 )
694+ expect ( fetchCalls [ 1 ] . url ) . toBe ( 'http://new.api.io/m' )
695+ } )
658696} )
659697
660698describe ( 'Group' , ( ) => {
Original file line number Diff line number Diff line change @@ -357,7 +357,16 @@ async function loadAnalytics(
357357 const plugins = settings . plugins ?? [ ]
358358
359359 const classicIntegrations = settings . classicIntegrations ?? [ ]
360- Stats . initRemoteMetrics ( legacySettings . metrics )
360+
361+ const segmentLoadOptions = options . integrations ?. [ 'Segment.io' ] as
362+ | SegmentioSettings
363+ | undefined
364+
365+ Stats . initRemoteMetrics ( {
366+ ...legacySettings . metrics ,
367+ host : segmentLoadOptions ?. apiHost ?? legacySettings . metrics ?. host ,
368+ protocol : segmentLoadOptions ?. protocol ,
369+ } )
361370
362371 // needs to be flushed before plugins are registered
363372 flushPreBuffer ( analytics , preInitBuffer )
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ export interface MetricsOptions {
88 sampleRate ?: number
99 flushTimer ?: number
1010 maxQueueSize ?: number
11+ protocol ?: 'http' | 'https'
1112}
1213
1314/**
@@ -56,6 +57,7 @@ export class RemoteMetrics {
5657 private host : string
5758 private flushTimer : number
5859 private maxQueueSize : number
60+ private protocol : string
5961
6062 sampleRate : number
6163 queue : RemoteMetric [ ]
@@ -65,6 +67,7 @@ export class RemoteMetrics {
6567 this . sampleRate = options ?. sampleRate ?? 1
6668 this . flushTimer = options ?. flushTimer ?? 30 * 1000 /* 30s */
6769 this . maxQueueSize = options ?. maxQueueSize ?? 20
70+ this . protocol = options ?. protocol ?? 'https'
6871
6972 this . queue = [ ]
7073
@@ -130,7 +133,7 @@ export class RemoteMetrics {
130133 this . queue = [ ]
131134
132135 const headers = { 'Content-Type' : 'text/plain' }
133- const url = `https ://${ this . host } /m`
136+ const url = `${ this . protocol } ://${ this . host } /m`
134137
135138 return fetch ( url , {
136139 headers,
You can’t perform that action at this time.
0 commit comments