## Query analytics top-N `client.AnalyticsQuery.TopN(ctx, dataset, params) (*SinglePage[AnalyticsQueryTopNResponse], error)` **post** `/accounts/{account_id}/analytics/query/{dataset}/top-n` Returns the top N results for a dataset by a specified stat. Includes an array of result rows, each containing the requested stats and group-by dimensions. ### Parameters - `dataset string` - `params AnalyticsQueryTopNParams` - `AccountID param.Field[string]` Path param: Cloudflare account identifier. - `Filters param.Field[[]AnalyticsQueryTopNParamsFilter]` Body param: Filters to apply before aggregating results. - `Name string` Specifies the column name to filter on. Requires a valid column for the target dataset (e.g. `country`, `allowed`, `appId`). - `Op string` Filter operator. Common values: `eq`, `neq`, `in`, `not_in`, `gt`, `lt`, `gte`, `lte`. - `Values []AnalyticsQueryTopNParamsFiltersValueUnion` Values to match against. Type depends on the column. - `UnionString` - `UnionBool` - `UnionFloat` - `From param.Field[Time]` Body param: The start of the query time range (inclusive). RFC3339 format with timezone is required (e.g. `2024-11-05T00:00:00Z`). - `GroupBy param.Field[[]string]` Body param: Specifies the column names to group results by. Requires valid columns for the target dataset. - `N param.Field[int64]` Body param: Maximum number of results to return. - `OrderBy param.Field[string]` Body param: Specifies the stat name for sorting results in descending order. Requires a valid stat for the target dataset. - `Stats param.Field[[]string]` Body param: Specifies the stat names to include in results. Requires valid stats for the target dataset (e.g. `attemptsTotal`, `bytesTotal`). - `To param.Field[Time]` Body param: Specifies the end of the query time range (exclusive). Requires RFC3339 format with timezone. ### Returns - `type AnalyticsQueryTopNResponse map[string, unknown]` Maps field names to values. Keys represent stat names and group-by column names. Values depend on the dataset (strings, numbers, booleans). ### Example ```go package main import ( "context" "fmt" "time" "github.com/cloudflare/cloudflare-go" "github.com/cloudflare/cloudflare-go/option" ) func main() { client := cloudflare.NewClient( option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"), ) page, err := client.AnalyticsQuery.TopN( context.TODO(), "gateway-http", cloudflare.AnalyticsQueryTopNParams{ AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"), Filters: cloudflare.F([]cloudflare.AnalyticsQueryTopNParamsFilter{}), From: cloudflare.F(time.Now()), GroupBy: cloudflare.F([]string{"appName", "appCategory"}), N: cloudflare.F(int64(10)), OrderBy: cloudflare.F("bytesTotal"), Stats: cloudflare.F([]string{"bytesTotal", "requestsTotal"}), To: cloudflare.F(time.Now()), }, ) if err != nil { panic(err.Error()) } fmt.Printf("%+v\n", page) } ``` #### Response ```json { "errors": [], "messages": [ { "code": 1000, "message": "API in beta: expect breaking changes." } ], "result": [ { "appCategory": "Collaboration", "appName": "Slack", "bytesTotal": 10485760, "requestsTotal": 1024 }, { "appCategory": "File Storage", "appName": "Dropbox", "bytesTotal": 5242880, "requestsTotal": 512 } ], "success": true } ```