API Connect Analytics stores the API event data for all the API calls to the gateway. An API event is logged each time an API operation is invoked on the gateway. The API event record contains information about the API call and the content of the record depends on the logging policy that is set for the operation.
There are cases where we look to enrich event data being sent to analytics service and hence look forward customizing it.
Example – We modify the request msg before invoke and wanted to capture this modified request msg being sent to backend along with or without original request received for later debugging or any other purposes.
Similarly, there can be scenario where we need to modify (say enrichment) response message received from backend before sending it as response of API call and needed to capture the actual response received from backend for later debugging or any other purposes.
Today we are going to see how we can customize the api event data being sent to analytics for logging.
Below is a screenshot of an API designed where a request is being received, which is being parsed and modified at “modify req msg” and then modified request message being logged using custom api event creation in next steps.

For custom api event data to send to analytics add a Log policy as part of your API flow and set the mode to gather-only

Then Add a gateway script policy to modify api event data or add custom data to be sent to analytics.
var logs = context.get('log');
if (logs) {
if (!logs.custom_data) {
logs.custom_data = {};
logs.custom_data.invoke = {};
}
if (!logs.custom_data.invoke) {
logs.custom_data.invoke = {};
}
logs.custom_data.invoke.request = context.get('message.body')
context.set('log', logs);
}

Note: You can also use Set-Variable policy instead of Gateway Script to achieve the same but setting the custom field in log context object ( E.g – Set log.custom_data.invoke.request with required value)
In the Analytics you can see the API Event data containing the custom fields added.
