From 1337b1791d10e5d136ac226913fd81cec5bda3e5 Mon Sep 17 00:00:00 2001 From: Neek Sandhu Date: Fri, 27 Oct 2023 12:47:15 -0700 Subject: [PATCH] Update integration metrics capturing 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. --- .../src/plugins/ajs-destination/index.ts | 2 ++ .../__tests__/action-destination.test.ts | 18 +++++++--- .../src/plugins/remote-loader/index.ts | 36 +++++++++---------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/packages/browser/src/plugins/ajs-destination/index.ts b/packages/browser/src/plugins/ajs-destination/index.ts index ac2a938cf..798d067b2 100644 --- a/packages/browser/src/plugins/ajs-destination/index.ts +++ b/packages/browser/src/plugins/ajs-destination/index.ts @@ -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 { @@ -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 } diff --git a/packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts b/packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts index 805933d2d..73ebcfd08 100644 --- a/packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts +++ b/packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts @@ -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', + ], }) ) }) diff --git a/packages/browser/src/plugins/remote-loader/index.ts b/packages/browser/src/plugins/remote-loader/index.ts index 55af1c2de..85490628d 100644 --- a/packages/browser/src/plugins/remote-loader/index.ts +++ b/packages/browser/src/plugins/remote-loader/index.ts @@ -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 } @@ -117,18 +109,10 @@ export class ActionDestination implements DestinationPlugin { async load(ctx: Context, analytics: Analytics): Promise { 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 } } @@ -136,6 +120,18 @@ export class ActionDestination implements DestinationPlugin { unload(ctx: Context, analytics: Analytics): Promise | 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 = {