- Developer guides
Analytics customization
This topic covers how to replace the default analytics registration with your own implementation, and how to register additional plugins, options, or a custom adapter. For what plugins and adapters are and which ones the Angular integration registers by default, see Plugins and Adapters.
Overriding the default implementation
The default implementation is a dependency injection (DI) provider, so you replace it the same way you replace any Angular provider: register your own SITECORE_ANALYTICS provider after provideSitecoreAngular() in the providers array. This overrides the built-in implementation.
Implement the interface
Your implementation must satisfy SitecoreAnalyticsWrapper, exported from @sitecore-content-sdk/angular. Inside it, call initContentSdk with whichever plugins you need. The following example implements a custom browser analytics provider:
Register it after provideSitecoreAngular()
provideSitecoreAngular()The default Content SDK analytics provider uses a factory that injects a provider with server or browser plugins, depending on the environment. If you implement custom behavior per environment, use a factory too:
Order matters. Registering your provider before provideSitecoreAngular() has no effect, because the SDK's own provider is registered afterwards and takes precedence.
You can also decide whether to use browser or server plugins inside the custom provider implementation itself. In that case, you don't need useFactory, and a single implementation can be injected in both environments.
Registering additional plugins and adapters
The plugins array you pass to initContentSdk is where you extend the runtime. Each plugin can carry its own adapter, so a plugin list with three plugins can carry three adapters — one per capability, each matched to the environment the code runs in.
The Content SDK ships two adapter families:
| Adapter family | Interface | Provides | Implementations |
|---|---|---|---|
| Analytics | AnalyticsAdapter | Client ID handling, bot detection, URL search params | analyticsBrowserAdapter(), analyticsServerAdapter(req, res) |
| Personalize | PersonalizeAdapter | Profile ID handling, user agent | personalizeBrowserAdapter(), personalizeServerAdapter(req, res) |
Adding personalization in the browser
The following example adds personalizeBrowserPlugin() to the browser plugin list. It carries the personalize browser adapter, while the analytics plugin keeps its own:
webPersonalization loads the Sitecore web personalization script in the browser. Pass true to use the defaults, or an object to control the async, defer, and language attributes. This option only applies in the browser.
Package dependencies
@sitecore-content-sdk/analytics-core, @sitecore-content-sdk/events, and @sitecore-content-sdk/personalize are peer dependencies of @sitecore-content-sdk/angular, so they are already installed and importable. To call initContentSdk directly, add the core package to your application:
Overriding plugins in the Express middleware
The bot tracking and personalization middleware run outside the Angular injector, so DI does not apply to them. Their plugin sets are fixed, but you can configure their behavior through options: matcher and skip on both, and personalizeService, getExtraUtmParams, extractGeoDataCb, and skipForBot on personalization. If you need a different plugin set on the server, write your own Express middleware that calls initContentSdk, and register it in place of the built-in one.