Skip to content

Commit 083f9a1

Browse files
zikaarichrisradek
andauthored
Update integration metrics capturing (#985)
Co-authored-by: Christopher Radek <christopher.radek@segment.com>
1 parent 6863e96 commit 083f9a1

File tree

6 files changed

+93
-44
lines changed

6 files changed

+93
-44
lines changed

.changeset/poor-masks-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': minor
3+
---
4+
5+
Update integration metrics capturing strategy

packages/browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"size-limit": [
4545
{
4646
"path": "dist/umd/index.js",
47-
"limit": "29 KB"
47+
"limit": "29.1 KB"
4848
}
4949
],
5050
"dependencies": {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Context } from '../context'
2+
3+
export interface RecordIntegrationMetricProps {
4+
integrationName: string
5+
methodName: string
6+
didError?: boolean
7+
type: 'classic' | 'action'
8+
}
9+
10+
export function recordIntegrationMetric(
11+
ctx: Context,
12+
{
13+
methodName,
14+
integrationName,
15+
type,
16+
didError = false,
17+
}: RecordIntegrationMetricProps
18+
): void {
19+
ctx.stats.increment(
20+
`analytics_js.integration.invoke${didError ? '.error' : ''}`,
21+
1,
22+
[
23+
`method:${methodName}`,
24+
`integration_name:${integrationName}`,
25+
`type:${type}`,
26+
]
27+
)
28+
}

packages/browser/src/plugins/ajs-destination/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
isDisabledIntegration as shouldSkipIntegration,
3030
isInstallableIntegration,
3131
} from './utils'
32+
import { recordIntegrationMetric } from '../../core/stats/metric-helpers'
3233

3334
export type ClassType<T> = new (...args: unknown[]) => T
3435

@@ -158,18 +159,19 @@ export class LegacyDestination implements DestinationPlugin {
158159
})
159160

160161
try {
161-
ctx.stats.increment('analytics_js.integration.invoke', 1, [
162-
`method:initialize`,
163-
`integration_name:${this.name}`,
164-
])
165-
162+
recordIntegrationMetric(ctx, {
163+
integrationName: this.name,
164+
methodName: 'initialize',
165+
type: 'classic',
166+
})
166167
this.integration.initialize()
167168
} catch (error) {
168-
ctx.stats.increment('analytics_js.integration.invoke.error', 1, [
169-
`method:initialize`,
170-
`integration_name:${this.name}`,
171-
])
172-
169+
recordIntegrationMetric(ctx, {
170+
integrationName: this.name,
171+
methodName: 'initialize',
172+
type: 'classic',
173+
didError: true,
174+
})
173175
throw error
174176
}
175177
}
@@ -254,20 +256,23 @@ export class LegacyDestination implements DestinationPlugin {
254256
traverse: !this.disableAutoISOConversion,
255257
})
256258

257-
ctx.stats.increment('analytics_js.integration.invoke', 1, [
258-
`method:${eventType}`,
259-
`integration_name:${this.name}`,
260-
])
259+
recordIntegrationMetric(ctx, {
260+
integrationName: this.name,
261+
methodName: eventType,
262+
type: 'classic',
263+
})
261264

262265
try {
263266
if (this.integration) {
264267
await this.integration.invoke.call(this.integration, eventType, event)
265268
}
266269
} catch (err) {
267-
ctx.stats.increment('analytics_js.integration.invoke.error', 1, [
268-
`method:${eventType}`,
269-
`integration_name:${this.name}`,
270-
])
270+
recordIntegrationMetric(ctx, {
271+
integrationName: this.name,
272+
methodName: eventType,
273+
type: 'classic',
274+
didError: true,
275+
})
271276
throw err
272277
}
273278

packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,29 @@ describe('ActionDestination', () => {
5151

5252
expect(ajs.ctx?.stats.metrics[0]).toMatchObject(
5353
expect.objectContaining({
54-
metric: 'analytics_js.action_plugin.invoke',
55-
tags: ['method:load', 'action_plugin_name:testDestination'],
54+
metric: 'analytics_js.integration.invoke',
55+
tags: [
56+
'method:load',
57+
'integration_name:testDestination',
58+
'type:action',
59+
],
5660
})
5761
)
5862

5963
const trackCtx = await ajs.track('test')
6064

6165
const actionInvokeMetric = trackCtx.stats.metrics.find(
62-
(m) => m.metric === 'analytics_js.action_plugin.invoke'
66+
(m) => m.metric === 'analytics_js.integration.invoke'
6367
)
6468

6569
expect(actionInvokeMetric).toMatchObject(
6670
expect.objectContaining({
67-
metric: 'analytics_js.action_plugin.invoke',
68-
tags: ['method:track', 'action_plugin_name:testDestination'],
71+
metric: 'analytics_js.integration.invoke',
72+
tags: [
73+
'method:track',
74+
'integration_name:testDestination',
75+
'type:action',
76+
],
6977
})
7078
)
7179
})

packages/browser/src/plugins/remote-loader/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '../middleware'
1111
import { Context, ContextCancelation } from '../../core/context'
1212
import { Analytics } from '../../core/analytics'
13+
import { recordIntegrationMetric } from '../../core/stats/metric-helpers'
1314

1415
export interface RemotePlugin {
1516
/** The name of the remote plugin */
@@ -80,18 +81,19 @@ export class ActionDestination implements DestinationPlugin {
8081
}
8182

8283
try {
83-
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
84-
`method:${methodName}`,
85-
`action_plugin_name:${this.action.name}`,
86-
])
87-
84+
recordIntegrationMetric(ctx, {
85+
integrationName: this.action.name,
86+
methodName,
87+
type: 'action',
88+
})
8889
await this.action[methodName]!(transformedContext)
8990
} catch (error) {
90-
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
91-
`method:${methodName}`,
92-
`action_plugin_name:${this.action.name}`,
93-
])
94-
91+
recordIntegrationMetric(ctx, {
92+
integrationName: this.action.name,
93+
methodName,
94+
type: 'action',
95+
didError: true,
96+
})
9597
throw error
9698
}
9799

@@ -117,18 +119,19 @@ export class ActionDestination implements DestinationPlugin {
117119

118120
async load(ctx: Context, analytics: Analytics): Promise<unknown> {
119121
try {
120-
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
121-
`method:load`,
122-
`action_plugin_name:${this.action.name}`,
123-
])
124-
122+
recordIntegrationMetric(ctx, {
123+
integrationName: this.action.name,
124+
methodName: 'load',
125+
type: 'action',
126+
})
125127
return await this.action.load(ctx, analytics)
126128
} catch (error) {
127-
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
128-
`method:load`,
129-
`action_plugin_name:${this.action.name}`,
130-
])
131-
129+
recordIntegrationMetric(ctx, {
130+
integrationName: this.action.name,
131+
methodName: 'load',
132+
type: 'action',
133+
didError: true,
134+
})
132135
throw error
133136
}
134137
}

0 commit comments

Comments
 (0)