Skip to content

Commit 88c91c8

Browse files
authored
remove remote metrics from context, move to stats (#743)
1 parent fed489c commit 88c91c8

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

.changeset/pink-insects-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Remove remote metrics from context and move to stats

packages/browser/src/browser/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { popSnippetWindowBuffer } from '../core/buffer/snippet'
2626
import { ClassicIntegrationSource } from '../plugins/ajs-destination/types'
2727
import { attachInspector } from '../core/inspector'
28+
import { Stats } from '../core/stats'
2829

2930
export interface LegacyIntegrationConfiguration {
3031
/* @deprecated - This does not indicate browser types anymore */
@@ -279,7 +280,7 @@ async function loadAnalytics(
279280

280281
const plugins = settings.plugins ?? []
281282
const classicIntegrations = settings.classicIntegrations ?? []
282-
Context.initRemoteMetrics(legacySettings.metrics)
283+
Stats.initRemoteMetrics(legacySettings.metrics)
283284

284285
// needs to be flushed before plugins are registered
285286
flushPreBuffer(analytics, preInitBuffer)

packages/browser/src/core/context/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,15 @@ import {
77
} from '@segment/analytics-core'
88
import { SegmentEvent } from '../events/interfaces'
99
import { Stats } from '../stats'
10-
import { MetricsOptions, RemoteMetrics } from '../stats/remote-metrics'
11-
12-
let _remoteMetrics: RemoteMetrics
1310

1411
export class Context extends CoreContext<SegmentEvent> {
1512
static override system() {
1613
return new this({ type: 'track', event: 'system' })
1714
}
18-
static initRemoteMetrics(options?: MetricsOptions) {
19-
_remoteMetrics = new RemoteMetrics(options)
20-
}
2115
constructor(event: SegmentEvent, id?: string) {
22-
super(event, id, new Stats(_remoteMetrics))
16+
super(event, id, new Stats())
2317
}
2418
}
19+
2520
export { ContextCancelation }
2621
export type { ContextFailedDelivery, SerializedContext, CancelationOptions }

packages/browser/src/core/stats/__tests__/index.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { Stats } from '..'
21
import { RemoteMetrics } from '../remote-metrics'
2+
import { Stats } from '..'
3+
4+
const spy = jest.spyOn(RemoteMetrics.prototype, 'increment')
35

46
describe(Stats, () => {
57
test('forwards increments to remote metrics endpoint', () => {
6-
const remote = new RemoteMetrics()
7-
const spy = jest.spyOn(remote, 'increment')
8+
Stats.initRemoteMetrics()
89

9-
const stats = new Stats(remote)
10+
const stats = new Stats()
1011
stats.increment('banana', 1, ['phone:1'])
1112

1213
expect(spy).toHaveBeenCalledWith('banana', ['phone:1'])
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { CoreStats } from '@segment/analytics-core'
2-
import type { RemoteMetrics } from './remote-metrics'
2+
import { MetricsOptions, RemoteMetrics } from './remote-metrics'
3+
4+
let remoteMetrics: RemoteMetrics | undefined
35

46
export class Stats extends CoreStats {
5-
constructor(private _remoteMetrics?: RemoteMetrics) {
6-
super()
7+
static initRemoteMetrics(options?: MetricsOptions) {
8+
remoteMetrics = new RemoteMetrics(options)
79
}
10+
811
override increment(metric: string, by?: number, tags?: string[]): void {
912
super.increment(metric, by, tags)
10-
this._remoteMetrics?.increment(metric, tags ?? [])
13+
remoteMetrics?.increment(metric, tags ?? [])
1114
}
1215
}

0 commit comments

Comments
 (0)