HTTP Interceptor

February 24, 2022 ยท View on GitHub

API > Interceptors

HTTP Interceptor

Description

The AnalyticsInterceptor implements Angular's HttpInterceptor interface and injects itself into your app's request-response chain. It will begin emitting events once you have initialized the library with a configuration that contains at least one destination.

The interceptor will dispatch an event for the start and completion (successful or not) for any HTTP call except for any destination URLs contained in your app configuration.

Event types

ApiStartEvent

An ApiStartEvent is dispatched when an HTTP request is sent. It is made up of the following properties.

Event propertyDescription
requestThe HttpRequest object received by the interceptor.
idA string identifier that defaults to the target URL. This can be overridden by attaching an ApiEventContext to your call.
scopesAn array of custom objects passed by attaching an ApiEventContext to your call.
eventTypeAlways set to a value of 'API_START_EVENT'.
locationThe current location found in the Angular router when the call was made.

ApiCompleteEvent

An ApiCompleteEvent is dispatched when an HTTP response is returned - successful or not. It is made up of the following properties.

Event propertyDescription
responseThe response received as a result of the request.
requestThe HttpRequest object received by the interceptor.
durationThe length of the call in milliseconds.
idA string identifier that defaults to the target URL. This can be overridden by attaching an ApiEventContext to your call.
scopesAn array of custom objects passed by attaching an ApiEventContext to your call.
eventTypeAlways set to a value of 'API_COMPLETE_EVENT'.
locationThe current location found in the Angular router when the call was made.

Providing context to an API call

When there is a need to associate more data to an API call, an ApiEventContext object can be set on the HttpRequest.context property and it will be dispatched with the different API events.

ApiEventContext

PropertyDescription
startAn ApiContext object to send with an ApiStartEvent
successAn ApiContext object to send with a successful ApiCompleteEvent
failureAn ApiContext object to send with a failed ApiCompleteEvent

ApiContext

PropertyDescription
idAn identifier to use for the API call instead of the target URL
scopesAn array of custom objects to attach to the event

Feedback

Is something not working or unclear? Please create an issue or PR.