Extending enrichment

Adapt collection to your application's vocabulary: exclude internal activity, normalize routes, derive dimensions, or replace a classifier.

Configure extensions before opening the engine.

Choose an extension point

Extension Use it for
IEventFilter Deciding whether an event should be collected
IEventEnricher Adding, changing, or removing dimensions
IBotDetector Recognizing automated clients
IUserAgentParser Browser, operating-system, and device classification
IGeoLocator Country, region, and city lookup; the engine has a built-in one for MMDB databases

Each interface has a helper factory, such as EventFilter.Create or EventEnricher.Create, for a lambda implementation.

Normalize pages for useful reports

Routes containing resource IDs can produce thousands of separate rows. Normalize them into a common route and derive a section for grouping:

options.Enrichers.Add(EventEnricher.NormalizePage(path =>
    System.Text.RegularExpressions.Regex.Replace(path, @"/\d+(?=/|$)", "/:id")));

options.Enrichers.Add(EventEnricher.Create(context =>
{
    var page = context.GetDimension(StandardDimensions.Page);
    var parts = page?.Split('/', StringSplitOptions.RemoveEmptyEntries);
    context.SetDimension("section", parts is { Length: > 0 } ? parts[0] : null);
}));

This example turns numeric path segments into /:id. Choose a normalization rule that matches your route structure.

Resolve visitor locations

The engine resolves countries, regions and cities from visitor IP addresses with MaxMind DB (MMDB) files that it downloads and keeps current. The library ships no database; add sources:

options.Web.IpLocation.Sources.AddRange(IpLocationSources.UserCountry);

The file is saved under ip-location in the data directory. Opening the engine does not wait for it: existing files load and missing files download in the background, and until then addresses are not located. Every day a conditional request checks for a newer file, which downloads nothing when the database did not change. A download replaces its file only after it loads, and the new database is swapped in without pausing lookups. Failures are logged and retried a day later while the current database stays in use; engine.IpLocation.UpdateAsync retries at once. engine.IpLocation reports the database in use, the last check and the last failure, and WaitForDatabaseAsync waits for the first database.

Sources Locations License Size
IpLocationSources.UserCountry Country PDDL: no attribution 8 MB file, about 10 MB in memory
IpLocationSources.DbIpCity Country, region, city CC BY 4.0: show "IP Geolocation by DB-IP" with a link to db-ip.com 130 MB in two files; about 10 MB in memory for countries, 60 MB for regions and 120 MB for cities

Locations are kept to Privacy.GeoPrecision: a city database used for countries merges its ranges by country. Other MMDB files work too, in the ip-location-db layout or the MaxMind layout (GeoLite2, DB-IP); add their URLs as sources. IPv4 and IPv6 can come in one file or in separate files.

Lookups take tens of nanoseconds and do not allocate: loading flattens the database into sorted address ranges.

To share one database between engines, create the provider yourself, set it as Web.GeoLocator of each engine, and dispose it after them:

var locationOptions = new IpLocationOptions();
locationOptions.Sources.AddRange(IpLocationSources.DbIpCity);
await using var ipLocation = new IpLocationProvider(locationOptions, GeoPrecision.City, baseDirectory: "data");
options.Web.GeoLocator = ipLocation;

IpLocationDatabase.Load loads files without downloads or updates. To use a provider that is not file based, implement IGeoLocator, or pass geography as event dimensions, for example from a CDN header.

Apply collection rules

Event filters run before an event is queued. Keep them fast and base exclusions on trusted application context. They can implement an opt-out decision or exclude known internal activity.

The library does not ship a geography database. Supply a locator adapter or trusted geography dimensions when country and region reports are required.

Understand ordering

Built-in enrichment runs first. Custom enrichers then run in their configured order, followed by custom-dimension privacy policy before storage.

Session-level dimensions are captured from the first event of a session. A value supplied later does not replace that session's original context.

Handle extension failures

Extensions run concurrently and must be thread-safe. An exception is logged and counted; the event continues without the failing extension's contribution. A throwing enricher's pending changes are discarded.

Write required collection policies as reliable decisions rather than relying on an exception to reject an event.

Test the behavior you add

Application code that only tracks events can depend on IAnalyticsTracker and use a fake in unit tests.

Test enrichment and report behavior against a real engine on a temporary directory. That exercises normalization, sessions, privacy rules, and aggregation together. Changes to enrichment affect future events; they do not rewrite existing history.