Say you want to capture analytics across a user's API lifecycle, for all their API usage, etc.
This is easily achieved using Analytics Tags.
Determine what is the "unique" user ID you want to aggregate requests by.
For our use, we will be using a Claim in a JWT. (Optional, only if you want to inject claims from a JWT or other request parameters) Enable Context Variables in API
Inject the unique user ID in a header. If it already exists, you can skip this step.
For our JWT example, given the following JWT:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
This produces these claims:
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
We will inject the sub
as a user
header using the special Tyk context variable field:
$tyk_context.jwt_claims_sub
This is the same as setting in API definiton:
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
},
"global_headers": {
"user": "$tyk_context.jwt_claims_sub"
},
Now, we will capture this "Header" for Analytics purposes:
This is the same as setting in api definition:
"tag_headers": [
"user"
],
That's it!
Now we can search/filter analytics by the user's ID by searching:
<headerName>-<user-id>
in our case:
user-1234567891
you can even cUrl this to get the same info:
$ curl 'http://localhost:3000/api/usage/17/1/2022/24/1/2022?res=day&p=-1&tags=user-1234567891' -H "Authorization: 8faed65837ab440d6ac18c87f924a55a"
{"data":[{"id":{"day":17,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":18,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":19,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":20,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":5,"success":5,"error":0,"request_time":3.6,"latency":3.6,"upstream_latency":3,"max_upstream_latency":7,"min_upstream_latency":1,"max_latency":8,"min_latency":2,"last_hit":"2022-01-20T22:54:34Z"},{"id":{"day":21,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":22,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":23,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":0,"success":0,"error":0,"request_time":0,"latency":0,"upstream_latency":0,"max_upstream_latency":0,"min_upstream_latency":0,"max_latency":0,"min_latency":0,"last_hit":"0001-01-01T00:00:00Z"},{"id":{"day":24,"month":1,"year":2022,"hour":0,"code":0,"path":"","key":"","alias":"","url":"","iso_country":"","api_id":"","api_name":""},"hits":1,"success":1,"error":0,"request_time":8,"latency":8,"upstream_latency":7,"max_upstream_latency":7,"min_upstream_latency":7,"max_latency":8,"min_latency":8,"last_hit":"2022-01-24T09:23:23Z"}],"pages":0}%
Finally, if you're integating with another Analytics sink via Tyk Pump, the tag record will also flow into it.
CHeers!