Current section
Files
Jump to
Current section
Files
lib/teac/api/analytics/extensions.ex
defmodule Teac.Api.Analytics.Extensions do
alias Teac.Api
@schema NimbleOptions.new!(
extension_id: [type: :string],
type: [type: {:in, ["overview_v2"]}],
started_at: [type: :string],
end_at: [type: :string],
first: [type: :pos_integer],
after: [type: :string]
)
@doc """
Gets an analytics report for one or more extensions. Returns URLs to download CSV reports.
## Authorization
Requires a user access token with the `analytics:read:extensions` scope.
## Options
* `:extension_id` - optional. Filter to a specific extension.
* `:type` - optional. The type of report. Valid value: `"overview_v2"`.
* `:started_at` - optional. RFC3339 start date for the report.
* `:end_at` - optional. RFC3339 end date for the report.
* `:first` - optional. Maximum items to return. Max: 100.
* `:after` - optional. Cursor for forward pagination.
"""
@spec get(Teac.Client.t(), keyword()) ::
{:ok, list(map()), String.t() | nil} | {:error, Teac.Error.t()}
def get(%Teac.Client{} = client, opts \\ []) do
case NimbleOptions.validate(opts, @schema) do
{:ok, opts} ->
params =
[
extension_id: opts[:extension_id],
type: opts[:type],
started_at: opts[:started_at],
end_at: opts[:end_at],
first: opts[:first],
after: opts[:after]
]
|> Enum.reject(fn {_, v} -> is_nil(v) end)
[
base_url: Api.uri("analytics/extensions"),
params: params,
headers: Api.headers(client)
]
|> Keyword.merge(Application.get_env(:teac, :api_req_options, []))
|> Req.get!()
|> Api.handle_paginated_response()
{:error, %NimbleOptions.ValidationError{} = err} ->
{:error, err.message}
end
end
@doc "Streams all extension analytics reports, automatically following pagination cursors."
@spec stream(Teac.Client.t(), keyword()) :: Enumerable.t()
def stream(%Teac.Client{} = client, opts \\ []) do
Teac.Api.Pagination.stream(client, &get/2, opts)
end
end