Ampli DestinationPlugin doesn't support undefined return value
Hello,
There's a discrepancy between the docs and the implementation of the Ampli Plugin.
I'm trying to write a plugin to forward data to other services and in some cases not to Amplitude, just like this middleware from the docs.
Expected Behavior
According to the docs:
if used as a destination type plugin, the expected return value is undefined
Current Behavior
This is not the case in the code: https://github.com/amplitude/Amplitude-TypeScript/blob/main/packages/analytics-types/src/plugin.ts#L25
Possible Solution
Update docs / change type to allow DestinationPlugins to return undefined
Environment
- JS SDK Version: 1.6.7
- Installation Method: yarn
- Browser and Version: Brave v1.46.153
Hi @jeremybenaim, thank you for pointing this out. I would say the docs are incorrect. Destination plugin requires a return value to let the SDK know if sending to amplitude was a success or not. If you want to optimistically return, you can just return a placeholder value:
const plugin = () => {
return {
...
execute: (event) => {
return {
event,
code: 200,
message: 'success',
}
},
};
};
Does this help? I will have the docs updated.
Got it, thanks @kevinpagtakhan.
What if I want to filter out a specific event? Should I return an error for said event?
Thanks
@jeremybenaim this one is up to you. The return value is mostly surfaced to you when you await track calls, and have to write code based of off the result like logging. For example:
const result = await amplitude.track('Page View').promise;
console.log(result);
However, with multiple destination plugins added, the SDK will only return the result for the first destination plugin added which more often than not, is amplitude. This is unless you remove amplitude destination plugin, and add your custom one which bumps it as first.
@kevinpagtakhan
This is unless you remove amplitude destination plugin, and add your custom one which bumps it as first.
That sounds more like what I want to achieve, but not sure how to do that though, can you help? Thanks
@kevinpagtakhan I naively tried ampli.client.remove("amplitude"), since it seems to be the plugin name, but events are still sent to Amplitude 😕
That is the correct syntax but do you mind sharing your code on how you call load and remove methods?
@kevinpagtakhan I initialized Ampli the same way it's done as in the browser example:
// App.tsx
ampli.load({
client: {
apiKey: process.env.REACT_APP_AMPLITUDE_API_KEY || "",
},
});
ampli.client.remove("amplitude");
ampli.client.add(myCustomPlugin());
Did somebody manage to send data only to a different destination than Amplitude? We are sending data to Segment and from Segment we are piping them into Amplitude. We used the old version of analytics middleware and it was possible to don't call next and it worked fine.
So I tried to make the same approach and created EnrichmentPlugin and filtered out all events 😅 Like here https://amplitude.com/docs/sdks/sdk-plugins#drop-an-event.
import type { Types } from '@amplitude/analytics-browser';
export class StopSendingEventsPlugin implements Types.EnrichmentPlugin {
name = 'filter-events-plugin';
type = 'enrichment' as const;
async setup(_config: Types.BrowserConfig): Promise<void> {
return undefined;
}
async execute(_event: Types.Event): Promise<Types.Event | null> {
return null;
}
}
The logger now prints empty error messages index.ts:12003 Amplitude Logger [Error]: , so I wonder if somebody did it without any error messages or is it even possible?
Did somebody manage to send data only to a different destination than Amplitude? We are sending data to Segment and from Segment we are piping them into Amplitude. We used the old version of analytics middleware and it was possible to don't call
nextand it worked fine.So I tried to make the same approach and created
EnrichmentPluginand filtered out all events 😅 Like here https://amplitude.com/docs/sdks/sdk-plugins#drop-an-event.import type { Types } from '@amplitude/analytics-browser';
export class StopSendingEventsPlugin implements Types.EnrichmentPlugin { name = 'filter-events-plugin'; type = 'enrichment' as const;
async setup(_config: Types.BrowserConfig): Promise
{ return undefined; } async execute(_event: Types.Event): Promise<Types.Event | null> { return null; } } The logger now prints empty error messages
index.ts:12003 Amplitude Logger [Error]:, so I wonder if somebody did it without any error messages or is it even possible?
I have the same problem, show a console error log but in documentation said is correct implementation.