Skip to main content

Statistics API reference

Swetrix provides the ability to retrieve aggregated data not only through the UI, but also programmatically using API queries.

The statistics API provides several GET endpoints. These endpoints require some query parameters and return standard HTTP responses and a JSON-encoded body. API requests without a valid API key will fail (except for the publicly available API endpoints).

Each request must be authenticated with an API key using X-Api-Key HTTP header. You can obtain an API key in your Swetrix account settings.

Rate limit for the API depends on your plan, you can find more information on the billing (or the main) page. As of 9 February 2023, the rate limits are as follows:

  • Free plan: 600 requests per hour;
  • Any paid plan: 600 requests per hour.

If you have special needs for more requests, please contact us to request more capacity.

Concepts

Time buckets

  • minute - 1 minute;
  • hour - 1 hour;
  • day - 1 day;
  • week - 1 week;
  • month - 1 month;
  • year - 1 year.

Periods

  • today - today (i.e. the time starting from 12:00 AM of the current day);
  • yesterday - yesterday (i.e. the time starting from 12:00 AM of the previous day);
  • 1d - last 24 hours;
  • 7d - last 7 days (i.e. current_day - 7 days);
  • 4w - last 4 weeks;
  • 3M - last 3 months;
  • 12M - last 12 months;
  • 24M - last 24 months.
  • all - all time (i.e. the time starting from the project creation date).
info

Time buckets and periods are predefined values that you can use in your API requests. The API won't accept any other values. If you want to specify a custom time range, use from and to parameters in your aggregation requests.

Measures

Measures are an aggregate functions that are used for performance-related endpoints. They allow you to select a function that will be used for aggregation of your metrics. It supports the following values:

  1. median (default) - the middle value of a set of numbers (i.e. 50th percentile).
  2. average - the arithmetic mean value.
  3. p95 - the 95th quantile.
  4. quantiles - it's a special measure, because instead of the regular metrics (e.g. dns, tls, etc.) it will return load time (the sum on all metrics) across 3 qunatiles: p50, p75 and p95.

Filters

Filters are used to aggregate data by specific parameters. For example, you can filter the data by country, browser, operating system, etc.

'Filter' is an object with the following structure:

{
"column": "cc",
"filter": "GB",
"isExclusive": false
}
  • column - the column to filter by. Possible values are: cc, pg, lc, br, brv, os, osv, dv, ref, so, me, ca. You can find more detailed information about columns here.
  • filter - the value to filter by. For example, if you want to filter by country, the value should be a country code (e.g. GB for Great Britain).
  • isExclusive - whether to include or exclude the specified value. If isExclusive is true, the data will be filtered by the specified value. If isExclusive is false, the data will be filtered by all values except the specified one.

Mode

The mode parameter specifies how the data is aggregated. Possible values are:

  • periodic - data is aggregated by the specified time bucket (e.g. by day, week, etc.);
  • cumulative - data is aggregated cumulatively (e.g. the number of visits for the current day is the sum of the number of visits for all previous days and the current day).

Metrics

Metrics are a set of rules that allow you to aggregate your custom events as decimals or integers. They're extremely useful for e-commerce platforms, allowing you to calculate your sales, for example. This parameter is only supported for traffic analytics endpoint.

'Metric' is an object with the following structure:

{
"customEventName": "sale",
"metaKey": "currency",
"metaValue": "USD",
"metricKey": "amount",
"metaValueType": "float"
}

Where:

  1. customEventName (required): is the name of the custom event you want to apply your metric to.
  2. metaKey (optional): Custom event metadata key to filter (for example, "currency").
  3. metaValue (optional): Custom event metadata value to filter (for example, "GBP").
  4. metricKey (required): Metadata key to aggregate (for example, "amount").
  5. metaValueType (required): Specifies how to interpret the custom metric value. For example, float will convert "15.99" to 15.99, while integer will interpret it as 15. (supported values are: integer, float. If set to string, the whole metric will be ignored.)

Endpoints

GET /v1/log

This endpoint returns the aggregated traffic data for your project. This is the exact same data you see in the Traffic tab of your Dashboard, but represented as a JSON entity.

curl 'https://api.swetrix.com/v1/log?pid=YOUR_PROJECT_ID&timeBucket=day&period=7d'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"params": {
"cc": [
{
"name": "UA",
"count": 3
},
{
"name": "CZ",
"count": 1
}
],
"rg": [
{
"name": "Kyiv Oblast'",
"cc": "UA",
"count": 3
},
{
"name": "Prague",
"cc": "CZ",
"count": 1
}
],
"ct": [
{
"name": "Kyiv",
"cc": "UA",
"count": 3
},
{
"name": "Prague",
"cc": "CZ",
"count": 1
}
],
"pg": [
{
"name": "/",
"count": 4
}
],
"lc": [
{
"name": "sk",
"count": 1
},
{
"name": "en-GB",
"count": 3
}
],
"br": [
{
"name": "Chrome",
"count": 20
}
],
"brv": [
{
"name": "129.0",
"br": "Chrome",
"count": 20
}
],
"os": [
{
"name": "Windows",
"count": 32
}
],
"osv": [
{
"name": "10.0",
"os": "Windows",
"count": 32
}
],
"dv": [
{
"name": "desktop",
"count": 33
}
],
"ref": [],
"so": [],
"me": [],
"ca": [],
"te": [],
"co": []
},
"chart": {
"x": [
"2023-02-02 00:00:00", "2023-02-03 00:00:00", "2023-02-04 00:00:00", "2023-02-05 00:00:00", "2023-02-06 00:00:00", "2023-02-07 00:00:00", "2023-02-08 00:00:00", "2023-02-09 00:00:00"
],
"visits": [
0, 2, 9, 12, 4, 7, 4, 0
],
"uniques": [
0, 2, 9, 10, 4, 7, 4, 0
],
"sdur": [
0, 54, 2, 126, 8, 8, 2
]
},
"customs": {
"sale": 16,
"signup": 10
},
"properties": {
"author": 30
},
"appliedFilters": []
}

Parameters


pid (required)

The project ID.


timeBucket (required)

See time buckets.


period (required)

See periods.


from / to

Instead of specifying a fixed period, you can specify a custom time range using from and to parameters. Both parameters are optional, but if you specify from, you must also specify to. The format is YYYY-MM-DD.


timezone

The timezone to use for the time range. The default is Etc/GMT. You can use any timezone supported by day.js library.


filters

An array of filter objects.


mode

Mode used to aggregate the data. The default is periodic.


metrics

An optional array of metric objects, with up to 3 metrics allowed. If the metrics array is provided and it's valid, it will be calculated on the backend and an additional field called meta will be returned in the response.

The filters and timezone parameters provided within the same request are also used when calculating custom metrics.

The meta is an array of objects like:

{
"key": "amount", // equals to "metricKey" provided
"current": { // for the current period or from / to pair
"sum": 100, // the sum of all custom metrics
"avg": 20, // the averae of all custom metrics
},
"previous": { // for the previous period of the same length as the current one
"sum": 80,
"avg": 30,
},
}

GET /v1/log/performance

This endpoint accepts the same parameters as the /log endpoint. The response is almost the same as for the /log endpoint, but with additional performance metrics.

The chart object contains performance metrics represented as an array of values for each time bucket. The x array contains the time bucket labels, and the other arrays contain the values for each metric. The index of each value corresponds to the index of the time bucket label in the x array.

The metric values are in seconds.

The metrics are:

  • dns - DNS Resolution time;
  • tls - TLS Setup time;
  • conn - Connection time;
  • response - Response time;
  • render - Browser render time;
  • domLoad - DOM Content Load time;
  • ttfb - Time to First Byte.

This endpoint also accepts measures parameter.

curl 'https://api.swetrix.com/v1/log/performance?pid=YOUR_PROJECT_ID&timeBucket=day&period=7d&measure=average'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"params": {
"dv": [
{
"name": "desktop",
"count": 0.08
}
],
"pg": [
{
"name": "/",
"count": 0.08
}
],
"ct": [],
"cc": [
{
"name": "IS",
"count": 0.1
},
{
"name": "GB",
"count": 0.06
}
],
"brv": [
{
"name": "129.0",
"br": "Chrome",
"count": 0.21
},
{
"name": "132.0",
"br": "Firefox",
"count": 0.08
}
],
"br": [
{
"name": "Firefox",
"count": 0.1
},
{
"name": "Chrome",
"count": 0.06
}
],
"rg": []
},
"chart": {
"x": [
"2023-02-02 00:00:00", "2023-02-03 00:00:00", "2023-02-04 00:00:00", "2023-02-05 00:00:00", "2023-02-06 00:00:00", "2023-02-07 00:00:00", "2023-02-08 00:00:00", "2023-02-09 00:00:00"
],
"dns": [
0, 0, 0.09, 0.05, 0.02, 0.05, 0.06, 0
],
"tls": [
0, 0.13, 0.06, 0.06, 0.07, 0.33, 0.34, 0
],
"conn": [
0, 0, 0.04, 0.03, 0.01, 0.02, 0.02, 0
],
"response": [
0, 0.01, 0, 0, 0, 0.37, 0, 0
],
"render": [
0, 0.01, 0.38, 0.17, 0.63, 0.07, 0.07, 0
],
"domLoad": [
0, 0.91, 0.48, 0.54, 0.69, 0.94, 1.22, 0
],
"ttfb": [
0, 0.06, 0.07, 0.07, 0.05, 0.1, 1.11, 0
]
},
"appliedFilters": []
}

GET /v1/log/birdseye

This endpoint returns a summary of the log data for the specified projects. The response is an object with project IDs as keys and objects with the following properties as values:

{
current: { // data for the selected period
all: number // pageviews
unique: number // unique visitors
bounceRate: number // bounce rate
sdur: number // average session duration
},
previous: { // data for the previous period
all: number
unique: number
bounceRate: number
sdur: number
},
change: number, // change in the number of pageviews
uniqueChange: number, // change in the number of unique visitors
bounceRateChange: number, // change in the bounce rate
sdurChange: number, // change in the average session duration
}
curl 'https://api.swetrix.com/v1/log/birdseye?pids=["YOUR_PROJECT_ID"]&period=7d'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"YOUR_PROJECT_ID": {
"current": {
"all": 1633,
"unique": 650,
"sdur": 1722.293846153846,
"bounceRate": 39.8
},
"previous": {
"all": 1785,
"unique": 653,
"sdur": 1618.4303215926493,
"bounceRate": 36.6
},
"change": -152,
"uniqueChange": -3,
"bounceRateChange": -3.1999999999999957,
"sdurChange": 103.86352456119675
}
}

Parameters


pids (required)

An array of project IDs to return summary data for.


pid

A single project ID to return summary data for. You can use either pids or pid parameter, but not both.


period (required)

See periods.


from / to

Instead of specifying a fixed period, you can specify a custom time range using from and to parameters. Both parameters are optional, but if you specify from, you must also specify to. The format is YYYY-MM-DD.


timezone

The timezone to use for the time range. The default is Etc/GMT. You can use any timezone supported by day.js library.


filters

An array of filter objects.


GET /v1/log/performance/birdseye

This endpoint returns a summary of the performance data for the specified projects. This endpoint also accepts measures parameter. The response is an object with project IDs as keys and objects with the following properties as values:

{
current: { // data for the selected period
frontend: number // a sum of such metrics as browser render time and DOM Content Load time
backend: number // TTFB
network: number // a sum of such metrics as DNS Resolution time, TLS Setup time, Connection time and Response time
},
previous: { // data for the previous period
frontend: number
backend: number
network: number
},
frontendChange: number, // change in the number of frontend metrics
networkChange: number, // change in the number of network metrics
backendChange: number, // change in the number of backend metrics
}
curl 'https://api.swetrix.com/v1/log/performance/birdseye?pids=["YOUR_PROJECT_ID"]&period=7d'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"YOUR_PROJECT_ID": {
"current": {
"frontend": 1.3297172264355364,
"network": 0.4225330444203684,
"backend": 0.14972047670639219
},
"previous": {
"frontend": 1.1174158607350095,
"network": 0.1695841392649903,
"backend": 0.11855705996131527
},
"frontendChange": 0.21230136570052663,
"networkChange": 0.25294890515537805,
"backendChange": 0.031163416745076916
}
}

Parameters

Accepts the same parameters as the /log/birdseye endpoint.

GET /v1/log/captcha/birdseye

This endpoint returns a summary of the CAPTCHA data for the specified projects. The response is an object with project IDs as keys and objects with the following properties as values:

{
current: { // data for the selected period
all: number // number of CAPTCHA completions
},
previous: { // data for the previous period
all: number
},
change: number, // change in the number of CAPTCHA completions
}
curl 'https://api.swetrix.com/v1/log/captcha/birdseye?pids=["YOUR_PROJECT_ID"]&period=7d'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"YOUR_PROJECT_ID": {
"current": {
"all": 75
},
"previous": {
"all": 60
},
"change": 15
}
}

Parameters

Accepts the same parameters as the /log/birdseye endpoint.

GET /v1/log/liveVisitors

This endpoint returns a list of currently active visitors on the specified project. The psid is a unique identifier for the session, which can be used to get more information about the session using the /log/session endpoint.

curl 'https://api.swetrix.com/v1/log/liveVisitors?pid=YOUR_PROJECT_ID'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
[
{
"dv": "desktop",
"br": "Firefox",
"os": "Windows",
"cc": "GB",
"psid": "9165978030580383830"
},
{
"dv": "mobile",
"br": "Chrome",
"os": "Android",
"cc": "UA",
"psid": "9357848011560553100"
}
]

Parameters


pid (required)

The project ID to return live visitors for.


GET /v1/log/hb

This endpoint returns a list of heartbeat events for the specified project.

curl 'https://api.swetrix.com/log/hb?pids=["YOUR_PROJECT_ID"]'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"YOUR_PROJECT_ID": 2
}

Parameters


pids (required)

An array of project IDs to return heartbeat events for.


pid

A single project ID to return heartbeat events for. You can use either pids or pid parameter, but not both.


GET /v1/log/meta

This endpoint returns an array of custom event metadata.

curl 'https://api.swetrix.com/v1/log/meta?pid=YOUR_PROJECT_ID&timeBucket=day&period=7d&event=signup'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"result": [
{
"key": "Affiliate",
"value": "Yes",
"count": 1
},
{
"key": "Affiliate",
"value": "No",
"count": 4
},
{
"key": "OtherMetadata",
"value": "AnyString",
"count": 12
}
],
"appliedFilters": []
}

Parameters

The parameters are the same as for the /log endpoint, except additionally you must pass the following:

event

The name of the custom event to return metadata for.

GET /v1/log/property

This endpoint returns an array of page tags for a particular custom property. For example, if you have a blog, you may want to pass some properties, such as "author", to our API along with your pageviews. The 'author' property can have multiple values, such as 'John', 'Tom' or 'Andrew'. You can then aggregate on these properties.

curl 'https://api.swetrix.com/v1/log/property?pid=YOUR_PROJECT_ID&timeBucket=day&period=7d&property=author'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"result": [
{
"key": "author",
"value": "Andrew",
"count": 17
},
{
"key": "author",
"value": "John",
"count": 8
},
{
"key": "author",
"value": "Tom",
"count": 3
}
],
"appliedFilters": []
}

Parameters

The parameters are the same as for the /log endpoint, except additionally you must pass the following:

property

The name of the property (tag) to return details for.

GET /v1/log/sessions

This endpoint returns an array of individual sessions.

curl 'https://api.swetrix.com/v1/log/sessions?pid=YOUR_PROJECT_ID&period=7d&take=30&skip=0'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"sessions": [
{
"psid": "14287087333426119778",
"active": 1,
"cc": null,
"os": "Windows",
"br": "Firefox",
"pageviews": 4,
"created": "2023-12-20 16:53:59"
},
{
"psid": "3653378394154006361",
"active": 0,
"cc": "GB",
"os": "Mac OS",
"br": "Safari",
"pageviews": 1,
"created": "2023-12-16 19:53:27"
},
{
"psid": "1777747620282809424",
"active": 0,
"cc": "GB",
"os": "Mac OS",
"br": "Chrome",
"pageviews": 3,
"created": "2023-12-16 19:53:21"
}
],
"appliedFilters": [],
"take": 30,
"skip": 0
}

Parameters


pid (required)

The project ID.


period (required)

See periods.


from / to

Instead of specifying a fixed period, you can specify a custom time range using from and to parameters. Both parameters are optional, but if you specify from, you must also specify to. The format is YYYY-MM-DD.


timezone

The timezone to use for the time range. The default is Etc/GMT. You can use any timezone supported by day.js library.


filters

An array of filter objects.


take

The number of sessions to return. The default is 30, max is 150.


skip

The number of sessions to skip. The default is 0.


GET /v1/log/session

This endpoint returns information about a single session.

curl 'https://api.swetrix.com/v1/log/session?pid=YOUR_PROJECT_ID&psid=SESSION_IDENTIFIER'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"pages": [
{
"type": "pageview",
"value": "/",
"created": "2023-12-20 16:53:59"
},
{
"type": "pageview",
"value": "/signup",
"created": "2023-12-20 16:55:03"
},
{
"type": "event",
"value": "SIGNUP",
"created": "2023-12-20 16:56:11"
},
{
"type": "pageview",
"value": "/dashboard",
"created": "2023-12-20 16:56:13"
},
{
"type": "pageview",
"value": "/blog/how-to-do-stuff",
"created": "2023-12-20 16:57:32",
"metadata": [
{
"key": "author",
"value": "James"
}
]
},
{
"type": "event",
"value": "SOME_EVENT",
"created": "2023-12-20 16:58:01"
}
],
"details": {
"dv": "desktop",
"br": "Firefox",
"brv": "132.0",
"os": "Windows",
"osv": "10.0",
"lc": "en-GB",
"ref": "https://example.com",
"so": null,
"me": null,
"ca": null,
"te": null,
"co": null,
"cc": "GB",
"rg": "England",
"ct": "Liverpool",
"sdur": 242
},
"psid": "14287087333426119778",
"chart": {
"x": [
"2023-12-20 16:53:00",
"2023-12-20 16:54:00",
"2023-12-20 16:55:00",
"2023-12-20 16:56:00",
"2023-12-20 16:57:00",
"2023-12-20 16:58:00"
],
"visits": [
1,
0,
1,
1,
1,
0
]
},
"timeBucket": "minute"
}

Parameters


pid (required)

The project ID.


psid (required)

The session identifier.


timezone

The timezone to use for the time range. The default is Etc/GMT. You can use any timezone supported by day.js library.


Common request examples

Data on visitors from the UK who did not visit the website using a mobile device

This example returns the data about people from the UK who did not visit the website using mobile devices.

curl 'https://api.swetrix.com/log?pid=YOUR_PROJECT_ID&timeBucket=day&period=7d&filters=[{"column":"cc","filter":"GB","isExclusive":false},{"column":"dv","filter":"mobile","isExclusive":false}]'\
-H "X-Api-Key: ${SWETRIX_API_KEY}"
Response
{
"params": {
"cc": [
{
"name": "GB",
"count": 293
}
],
"rg": [
{
"name": "England",
"cc": "GB",
"count": 293
}
],
"ct": [
{
"name": "Liverpool",
"cc": "GB",
"count": 293
}
],
"dv": [
{
"name": "desktop",
"count": 293
}
],
"brv": [
{
"name": "132.0",
"br": "Firefox",
"count": 200
},
{
"name": "120.5",
"br": "Chrome",
"count": 93
}
],
"os": [
{
"name": "Windows",
"count": 293
}
],
"osv": [
{
"name": "10.0",
"os": "Windows",
"count": 293
}
],
"lc": [
{
"name": "en-GB",
"count": 280
},
{
"name": "en-US",
"count": 13
}
],
"dv": [
{
"name": "desktop",
"count": 293
}
],
"ref": [
{
"name": "https://youtube.com/",
"count": 41
}
],
"so": [],
"me": [],
"ca": [],
"te": [],
"co": []
},
"chart": {
"x": [
"2023-02-02 00:00:00", "2023-02-03 00:00:00", "2023-02-04 00:00:00", "2023-02-05 00:00:00", "2023-02-06 00:00:00", "2023-02-07 00:00:00", "2023-02-08 00:00:00", "2023-02-09 00:00:00"
],
"visits": [
73, 55, 21, 13, 21, 11, 98, 1
],
"uniques": [
73, 55, 21, 13, 21, 11, 98, 1
],
"sdur": [
43, 28, 29, 36, 50, 51, 52, 2
]
},
"avgSdur": 43,
"customs": {
"SIGNUP": 7, "PROJECT_CREATED": 14, "ACCOUNT_DELETED": 2
},
"appliedFilters": [
{ "column": "cc", "filter": "GB", "isExclusive": false },
{ "column": "dv", "filter": "mobile", "isExclusive": true }
]
}