# KlaviyoAPI::MetricsApi
All URIs are relative to *https://a.klaviyo.com*
| Method | HTTP request | Description |
| ------ | ------------ | ----------- |
| [**get_metric**](MetricsApi.md#get_metric) | **GET** /api/metrics/{id}/ | Get Metric |
| [**get_metrics**](MetricsApi.md#get_metrics) | **GET** /api/metrics/ | Get Metrics |
| [**query_metric_aggregates**](MetricsApi.md#query_metric_aggregates) | **POST** /api/metric-aggregates/ | Query Metric Aggregates |
## get_metric
> Hash<String, Object> get_metric(id, opts)
Get Metric
Get a metric with the given metric ID.
*Rate limits*:
Burst: `10/s`
Steady: `150/m` **Scopes:** `metrics:read`
### Examples
```ruby
require 'time'
require 'klaviyo-api-sdk'
# setup authorization
KlaviyoAPI.configure do |config|
# Configure API key authorization: Klaviyo-API-Key
config.api_key['Klaviyo-API-Key'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
# config.api_key_prefix['Klaviyo-API-Key'] = 'Bearer'
end
api_instance = KlaviyoAPI::MetricsApi.new
id = 'id_example' # String | Metric ID
opts = {
fields_metric: ['name'] # Array | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#sparse-fieldsets
}
begin
# Get Metric
result = api_instance.get_metric(id, opts)
p result
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->get_metric: #{e}"
end
```
#### Using the get_metric_with_http_info variant
This returns an Array which contains the response data, status code and headers.
> get_metric_with_http_info(id, opts)
```ruby
begin
# Get Metric
data, status_code, headers = api_instance.get_metric_with_http_info(id, opts)
p status_code # => 2xx
p headers # => { ... }
p data # => Hash<String, Object>
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->get_metric_with_http_info: #{e}"
end
```
### Parameters
| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
| **id** | **String** | Metric ID | |
| **fields_metric** | [**Array<String>**](String.md) | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#sparse-fieldsets | [optional] |
### Return type
**Hash<String, Object>**
### Authorization
[Klaviyo-API-Key](../README.md#Klaviyo-API-Key)
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: application/json
## get_metrics
> Hash<String, Object> get_metrics(opts)
Get Metrics
Get all metrics in an account. Requests can be filtered by the following fields: integration `name`, integration `category` Returns a maximum of 200 results per page.
*Rate limits*:
Burst: `10/s`
Steady: `150/m` **Scopes:** `metrics:read`
### Examples
```ruby
require 'time'
require 'klaviyo-api-sdk'
# setup authorization
KlaviyoAPI.configure do |config|
# Configure API key authorization: Klaviyo-API-Key
config.api_key['Klaviyo-API-Key'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
# config.api_key_prefix['Klaviyo-API-Key'] = 'Bearer'
end
api_instance = KlaviyoAPI::MetricsApi.new
opts = {
fields_metric: ['name'], # Array | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#sparse-fieldsets
filter: 'filter_example', # String | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#filtering
Allowed field(s)/operator(s):
`integration.name`: `equals`
`integration.category`: `equals`
page_cursor: 'page_cursor_example' # String | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#pagination
}
begin
# Get Metrics
result = api_instance.get_metrics(opts)
p result
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->get_metrics: #{e}"
end
```
#### Using the get_metrics_with_http_info variant
This returns an Array which contains the response data, status code and headers.
> get_metrics_with_http_info(opts)
```ruby
begin
# Get Metrics
data, status_code, headers = api_instance.get_metrics_with_http_info(opts)
p status_code # => 2xx
p headers # => { ... }
p data # => Hash<String, Object>
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->get_metrics_with_http_info: #{e}"
end
```
### Parameters
| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
| **fields_metric** | [**Array<String>**](String.md) | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#sparse-fieldsets | [optional] |
| **filter** | **String** | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#filtering<br>Allowed field(s)/operator(s):<br>`integration.name`: `equals`<br>`integration.category`: `equals` | [optional] |
| **page_cursor** | **String** | For more information please visit https://developers.klaviyo.com/en/v2023-10-15/reference/api-overview#pagination | [optional] |
### Return type
**Hash<String, Object>**
### Authorization
[Klaviyo-API-Key](../README.md#Klaviyo-API-Key)
### HTTP request headers
- **Content-Type**: Not defined
- **Accept**: application/json
## query_metric_aggregates
> Hash<String, Object> query_metric_aggregates(metric_aggregate_query)
Query Metric Aggregates
Query and aggregate event data associated with a metric, including native Klaviyo metrics, integration-specific metrics, and custom events. Queries must be passed in the JSON body of your `POST` request. Results can be filtered and grouped by time, event, or profile dimensions. To learn more about how to use this endpoint, check out our new [Using the Query Metric Aggregates Endpoint guide](https://developers.klaviyo.com/en/docs/using-the-query-metric-aggregates-endpoint). **Request body parameters** (nested under `attributes`): * `return_fields`: request specific fields using [sparse fieldsets](https://developers.klaviyo.com/en/reference/api_overview#sparse-fieldsets) * `sort`: sort results by a specified field, such as `\"-timestamp\"` * `page_cursor`: results can be paginated with [cursor-based pagination](https://developers.klaviyo.com/en/reference/api_overview#pagination) * `page_size`: limit the number of returned results per page * `by`: optional attributes used to group by the aggregation function * When using `by` attributes, an empty `dimensions` response is expected when the counts for the events do not have the associated dimension requested by the set `by` attribute. For example, a query including `\"by\": [\"$flow\"]` will return an empty dimensions response for counts of metrics not associated with a `$flow` * `measurement`: the measurement key supports the following values: * `\"sum_value\"`: perform a summation of the `_Event Value_`, optionally partitioned over any dimension provided in the `by` field * `\"count\"`: counts the number of events associated to a metric, optionally partitioned over any dimension provided in the `by` field * `\"unique\"` counts the number of unique customers associated to a metric, optionally partitioned over any dimension provided in the `by` field * `interval`: aggregation interval, such as `\"hour\"`,`\"day\"`,`\"week\"`, and `\"month\"` * `metric_id`: the metric ID used in the aggregation * `filter`: list of filters for specific fields, must include time range using ISO 8601 format (`\"YYYY-MM-DDTHH:MM:SS.mmmmmm\"`) * The time range can be filtered by providing a `greater-or-equal` filter on the datetime field, such as `\"greater-or-equal(datetime,2021-07-01T00:00:00)\"` and a `less-than` filter on the same datetime field, such as `\"less-than(datetime,2022-07-01T00:00:00)\"` * The time range may span a maximum of one year. Time range dates may be set to a maximum of 5 years prior to the current date * Filter the list of supported aggregate dimensions using the common filter syntax, such as `\"equals(URL,\\\"https://www.klaviyo.com/\\\")\"` * `timezone`: the timezone used when processing the query. Case sensitive. This field is validated against a list of common timezones from the [IANA Time Zone Database](https://www.iana.org/time-zones) * While the payload accepts a timezone, the response datetimes returned will be in UTC. For a comprehensive list of native Klaviyo metrics and their associated attributes for grouping and filtering, please refer to the [metrics attributes guide](https://developers.klaviyo.com/en/docs/supported_metrics_and_attributes).
*Rate limits*:
Burst: `3/s`
Steady: `60/m` **Scopes:** `metrics:read`
### Examples
```ruby
require 'time'
require 'klaviyo-api-sdk'
# setup authorization
KlaviyoAPI.configure do |config|
# Configure API key authorization: Klaviyo-API-Key
config.api_key['Klaviyo-API-Key'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
# config.api_key_prefix['Klaviyo-API-Key'] = 'Bearer'
end
api_instance = KlaviyoAPI::MetricsApi.new
metric_aggregate_query = KlaviyoAPI::MetricAggregateQuery.new({data: KlaviyoAPI::MetricAggregateQueryResourceObject.new({type: KlaviyoAPI::MetricAggregateEnum::METRIC_AGGREGATE, attributes: KlaviyoAPI::MetricAggregateQueryResourceObjectAttributes.new({metric_id: '0rG4eQ', measurements: count, filter: ["greater-or-equal(datetime, 2022-12-01T00: 00: 00)", "less-than(datetime, 2022-12-08T00: 00: 00"]})})}) # MetricAggregateQuery | Retrieve Metric Aggregations
begin
# Query Metric Aggregates
result = api_instance.query_metric_aggregates(metric_aggregate_query)
p result
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->query_metric_aggregates: #{e}"
end
```
#### Using the query_metric_aggregates_with_http_info variant
This returns an Array which contains the response data, status code and headers.
> query_metric_aggregates_with_http_info(metric_aggregate_query)
```ruby
begin
# Query Metric Aggregates
data, status_code, headers = api_instance.query_metric_aggregates_with_http_info(metric_aggregate_query)
p status_code # => 2xx
p headers # => { ... }
p data # => Hash<String, Object>
rescue KlaviyoAPI::ApiError => e
puts "Error when calling MetricsApi->query_metric_aggregates_with_http_info: #{e}"
end
```
### Parameters
| Name | Type | Description | Notes |
| ---- | ---- | ----------- | ----- |
| **metric_aggregate_query** | [**MetricAggregateQuery**](MetricAggregateQuery.md) | Retrieve Metric Aggregations | |
### Return type
**Hash<String, Object>**
### Authorization
[Klaviyo-API-Key](../README.md#Klaviyo-API-Key)
### HTTP request headers
- **Content-Type**: application/json
- **Accept**: application/json