Skip to content

Commit

Permalink
Update integration metrics capturing
Browse files Browse the repository at this point in the history
Previous attempt at capturing action metrics used new metrics key, however that requires
certain updates on the production server to work. The schedule of when those updates will
go live is uncertain, so we're back to using the existing framework without minor changes.
  • Loading branch information
zikaari committed Oct 27, 2023
1 parent fca97a2 commit 1337b17
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
2 changes: 2 additions & 0 deletions packages/browser/src/plugins/ajs-destination/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export class LegacyDestination implements DestinationPlugin {
ctx.stats.increment('analytics_js.integration.invoke', 1, [
`method:${eventType}`,
`integration_name:${this.name}`,
`type:classic-destination`,
])

try {
Expand All @@ -267,6 +268,7 @@ export class LegacyDestination implements DestinationPlugin {
ctx.stats.increment('analytics_js.integration.invoke.error', 1, [
`method:${eventType}`,
`integration_name:${this.name}`,
`type:classic-destination`,
])
throw err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,29 @@ describe('ActionDestination', () => {

expect(ajs.ctx?.stats.metrics[0]).toMatchObject(
expect.objectContaining({
metric: 'analytics_js.action_plugin.invoke',
tags: ['method:load', 'action_plugin_name:testDestination'],
metric: 'analytics_js.integration.invoke',
tags: [
'method:load',
'integration_name:testDestination',
'type:destination',
],
})
)

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

const actionInvokeMetric = trackCtx.stats.metrics.find(
(m) => m.metric === 'analytics_js.action_plugin.invoke'
(m) => m.metric === 'analytics_js.integration.invoke'
)

expect(actionInvokeMetric).toMatchObject(
expect.objectContaining({
metric: 'analytics_js.action_plugin.invoke',
tags: ['method:track', 'action_plugin_name:testDestination'],
metric: 'analytics_js.integration.invoke',
tags: [
'method:track',
'integration_name:testDestination',
'type:destination',
],
})
)
})
Expand Down
36 changes: 16 additions & 20 deletions packages/browser/src/plugins/remote-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,10 @@ export class ActionDestination implements DestinationPlugin {
}

try {
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
`method:${methodName}`,
`action_plugin_name:${this.action.name}`,
])

this.recordMetric(ctx, methodName)
await this.action[methodName]!(transformedContext)
} catch (error) {
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
`method:${methodName}`,
`action_plugin_name:${this.action.name}`,
])

this.recordMetric(ctx, methodName, true)
throw error
}

Expand All @@ -117,25 +109,29 @@ export class ActionDestination implements DestinationPlugin {

async load(ctx: Context, analytics: Analytics): Promise<unknown> {
try {
ctx.stats.increment('analytics_js.action_plugin.invoke', 1, [
`method:load`,
`action_plugin_name:${this.action.name}`,
])

this.recordMetric(ctx, 'load')
return await this.action.load(ctx, analytics)
} catch (error) {
ctx.stats.increment('analytics_js.action_plugin.invoke.error', 1, [
`method:load`,
`action_plugin_name:${this.action.name}`,
])

this.recordMetric(ctx, 'load', true)
throw error
}
}

unload(ctx: Context, analytics: Analytics): Promise<unknown> | unknown {
return this.action.unload?.(ctx, analytics)
}

private recordMetric(ctx: Context, methodName: string, errored = false) {
ctx.stats.increment(
`analytics_js.integration.invoke${errored ? '.error' : ''}`,
1,
[
`method:${methodName}`,
`integration_name:${this.action.name}`,
`type:${this.action.type}`,
]
)
}
}

export type PluginFactory = {
Expand Down

0 comments on commit 1337b17

Please sign in to comment.