legato-ex
legato-ex copied to clipboard
Google Analytics API v4 in Elixir
Legato
Legato provides query access through the official Google Analytics Reporting API v4
Installation
Available in Hex, the package can be installed as:
- Add
legatoto your list of dependencies inmix.exs:
```elixir
def deps do
[{:legato, "~> 0.2.0"}]
end
```
- Ensure
legatois 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"}]
- [x] Collect data into Query struct
- [x] Convert query into Request JSON, encode with Poison
- [x] Send request to GA
- [x] Decode response
- [x] Parse data into struct
- [x] support metric expression strings
- [x] add filters to Query
- [x] add date ranges to Query
- [x] add order by to Query
- [x] add segment_id to Query
- [x] add Sampling
- [x] 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.