AnalyticsEx

A library tracking how many visitors a Phoenix-based application receives per day without collecting any data from the user. It encapsulates the Homemade Analytics code of José Valim and simply counts the page-requests per path per day. The collected data is stored in a configurable Ecto-repository.

Installation

1. Add the analytics_ex dependency

Add the following to your mix.exs and run mix deps.get:

def deps do
  [
    {:analytics_ex, "~> 0.1.1"}
  ]
end

2. Generate and run the Ecto Migration

In order to create the metrics table, please run the following commands:

mix analytics.gen.migration
mix ecto.migrate

This will generate an Ecto.Migration which creates the metrics table into which we store the analytics data.

3. Configure which Repo to use

Tell the library which Repo to use for storing the analytics data.

# In config.exs

config :analytics_ex, repo: MyApp.Repo

4. Add the Plug to your router.ex

In your router.ex, add the AnalyticsEx.Plugs.CountRequestsPerPath-Plug to any pipeline which is used by the routes which you want to track.

  pipeline :browser do
    ...
    plug(AnalyticsEx.Plugs.CountRequestsPerPath)
  end

(Optional) Bump the metric manually in LiveViews

The CountRequestsPerPath-Plug will not pick up requests if the push_patch/2 function is used since LiveView updates the url in the address bar without a full page reload, that is without calling the CountRequestsPerPath-Plug again. If you want to track these requests as well, you have to manually bump the path metric in the handle_params/3 callback:

def handle_params(params, uri, socket) do
  AnalyticsEx.Metrics.bump_with_uri(uri)

  # Do other stuff here
end

Thanks

José Valim

Special thanks to José Valim not only for creating Elixir, Ecto, and many more useful things, but also for writing the Homemade analytics with Ecto and Elixir blog post on the Dashbit website.

kipcole9

Thanks to kip for his answer on ElixirForum and his implementation of how to generate an Ecto migration in the application which uses the library.