Custom Enrichment Plugin
The Custom Enrichment plugin runs an enrichment function against every event the Browser SDK tracks, before the SDK uploads the event. Amplitude stores that function in your project's remote configuration, so you can change what it does without shipping a new build of your site.
The plugin is off by default. Enable it with the customEnrichment configuration option.
This plugin doesn't affect plugins you write
This plugin is separate from the custom plugins you add withamplitude.add(). Enabling or disabling customEnrichment has no effect on your own plugins, and your own plugins keep working whether or not you enable this one.Requirements
@amplitude/analytics-browserversion 2.37.0 or later.- Remote configuration enabled. The SDK fetches remote configuration by default. If you set
remoteConfig.fetchRemoteConfigtofalse, the plugin loads but never receives a function, so it leaves every event unchanged. Refer to Remote configuration. - An enrichment function defined in your project's remote configuration. The SDK doesn't read a function from your local configuration.
Enable the plugin
Set customEnrichment to true when you initialize the SDK:
import * as amplitude from "@amplitude/analytics-browser";
amplitude.init(AMPLITUDE_API_KEY, {
customEnrichment: true,
});
| Name | Description | Default value |
|---|---|---|
customEnrichment | boolean. Whether the SDK adds the Custom Enrichment plugin. | false |
Amplitude's remote configuration can also turn the plugin on, with one exception: when you set customEnrichment: false at initialization, remote configuration can't re-enable it. Omit the option instead of setting it to false if you want remote configuration to control the plugin.
The SDK reads the enrichment function only from remote configuration. Passing a function body in local configuration has no effect.
How the plugin works
- On setup, the plugin subscribes to the
configs.analyticsSDK.browserSDK.customEnrichmentkey in your project's remote configuration. - When remote configuration delivers a function body, the plugin evaluates it into a function that takes an event and returns an event.
- The plugin runs that function against every event, including autocaptured events, as the event passes through the SDK's enrichment stage.
- When remote configuration delivers no valid function body, the plugin passes every event through unchanged.
Because the plugin subscribes to remote configuration rather than reading it once, an update to the enrichment function reaches active sessions without a page reload.
Return values
What the enrichment function returns determines what Amplitude receives:
- Returns an event: the SDK uploads the returned event. This is how the function adds, changes, or removes properties.
- Returns nothing: the SDK drops the event, and the event never reaches Amplitude. An enrichment function that forgets to return the event silently drops all traffic.
- Throws an error: the SDK logs the error and uploads the original, unenriched event.
If the configured function body doesn't evaluate to a function, the plugin logs an error and passes every event through unchanged.
Content Security Policy
The plugin evaluates the enrichment function from remote configuration at runtime. If your site sets a Content Security Policy that restricts script-src, the policy must allow unsafe-eval for the plugin to build the function. Without it, the browser blocks the evaluation, the plugin logs an error, and events pass through unchanged.
Troubleshooting
Set logLevel to Debug to log what the plugin does:
import * as amplitude from "@amplitude/analytics-browser";
amplitude.init(AMPLITUDE_API_KEY, {
customEnrichment: true,
logLevel: amplitude.Types.LogLevel.Debug,
});
| Log message | What it means |
|---|---|
Adding custom enrichment plugin | The SDK enabled the plugin and added it. |
Installing @amplitude/plugin-custom-enrichment-browser | The plugin started its setup. |
Remote config client is not provided, skipping remote config fetch | The plugin can't reach remote configuration, so it leaves events unchanged. Check that remote configuration is on. |
Custom enrichment body did not evaluate to a function | Remote configuration delivered a body that isn't a function. Events pass through unchanged. |
Could not create custom enrichment function | The plugin couldn't evaluate the body. Check your Content Security Policy. |
Could not execute custom enrichment function | The enrichment function threw an error. The SDK uploads the original event. |
If the log doesn't include Adding custom enrichment plugin, the SDK didn't enable the plugin. Confirm that customEnrichment isn't set to false in your initialization options.
Google Tag Manager
The Amplitude Browser SDK GTM template exposes this plugin as the Enable Custom Enrichment Plugin checkbox on theinit tag. Refer to Google Tag Manager (client).Was this helpful?