Legato provides query access through the official Google Analytics Reporting API v4
Available in Hex, the package can be installed as:
- Add
legato
to your list of dependencies inmix.exs
:
```elixir
def deps do
[{:legato, "~> 0.2.0"}]
end
```
- Ensure
legato
is started before your application:
```elixir
def application do
[applications: [:legato]]
end
```
Get an oauth access token from Google
"Authorization: Bearer token_here"
HTTPoison.post "https://analyticsreporting.googleapis.com/v4/reports:batchGet", "{}", [{"Authorization", "Bearer token_here"}]
- Collect data into Query struct
- Convert query into Request JSON, encode with Poison
- Send request to GA
- Decode response
- Parse data into struct
- support metric expression strings
- add filters to Query
- add date ranges to Query
- add order by to Query
- add segment_id to Query
- add Sampling
- put report struct into named struct
- add segments to Query (long goal)
profile = %Legato.Profile{access_token: oauth2_access_token, view_id: view_id}
defmodule ExitReport do
defstruct :exits, :pageviews, :country
end
import Legato.Query
alias Legato.Request
alias Legato.Report
profile |>
metrics([:exits, :pageviews]) |>
dimensions([:country]) |>
filter(:exits, :gt, 10) |>
between(start_date, end_date) |> # first date range for the query
between(another_start_date, another_end_date) |> # adds subsequent date ranges
order_by(:pageviews, :descending) |>
segment(-3) |>
sampling(:small) |>
Request.all |>
Report.as(ExitReport)
If you'd like to use relative dates, I suggest trying timex
.
segment
with an integer will clear any segments, cannot be mixed with dynamic segments.