-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture action plugin metrics (#971)
Just like how we capture metrics relating to performance of classic integrations, this patch now enables capturing of metrics related to action destinations as well.
- Loading branch information
Showing
4 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-next': minor | ||
--- | ||
|
||
Capture action plugin metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/browser/src/plugins/remote-loader/__tests__/action-destination.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import unfetch from 'unfetch' | ||
import { PluginFactory } from '..' | ||
|
||
import { AnalyticsBrowser } from '../../../browser' | ||
import { createSuccess } from '../../../test-helpers/factories' | ||
|
||
jest.mock('unfetch') | ||
jest.mocked(unfetch).mockImplementation(() => | ||
createSuccess({ | ||
integrations: {}, | ||
remotePlugins: [ | ||
{ | ||
name: 'testDestination', | ||
libraryName: 'testDestination', | ||
settings: { | ||
subscriptions: [ | ||
{ | ||
name: 'Track Calls', | ||
enabled: true, | ||
partnerAction: 'trackEvent', | ||
subscribe: 'type = "track"', | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}) | ||
) | ||
|
||
const testDestination: PluginFactory = () => { | ||
return { | ||
name: 'testDestination', | ||
version: '1.0.0', | ||
type: 'destination', | ||
isLoaded: () => true, | ||
load: () => Promise.resolve(), | ||
track: (ctx) => Promise.resolve(ctx), | ||
} | ||
} | ||
|
||
testDestination.pluginName = 'testDestination' | ||
|
||
describe('ActionDestination', () => { | ||
it('captures essential metrics when invoking methods on an action plugin', async () => { | ||
const ajs = AnalyticsBrowser.load({ | ||
writeKey: 'abc', | ||
plugins: [testDestination], | ||
}) | ||
|
||
await ajs.ready() | ||
|
||
expect(ajs.ctx?.stats.metrics[0]).toMatchObject( | ||
expect.objectContaining({ | ||
metric: 'analytics_js.action_plugin.invoke', | ||
tags: ['method:load', 'action_plugin_name:testDestination'], | ||
}) | ||
) | ||
|
||
const trackCtx = await ajs.track('test') | ||
|
||
const actionInvokeMetric = trackCtx.stats.metrics.find( | ||
(m) => m.metric === 'analytics_js.action_plugin.invoke' | ||
) | ||
|
||
expect(actionInvokeMetric).toMatchObject( | ||
expect.objectContaining({ | ||
metric: 'analytics_js.action_plugin.invoke', | ||
tags: ['method:track', 'action_plugin_name:testDestination'], | ||
}) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters