AnalyticsEx

Heads up: This library is no longer actively maintained.

I built this library before Plausible.io became a thing and I switched to that wonderful service by now. You can still use this library and report issues and I’ll happily fix them. However, please don’t expect this library to work forever or have features added to it.

If you want to take over this library, please send me a message on Twitter or open an issue with your contact details here. I’ll be happy to help you get started.

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.2.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

(Optional) Add Analytics to LiveDashboard

You can add an overview of your analytics to your Phoenix LiveDashboard by adding the following code to the live_dashboard route in your router.ex:

    live_dashboard "/dashboard",
      additional_pages: [
        {:analytics, AnalyticsEx.LiveDashboard.AnalyticsPage}
      ]

Afterwards, you will have daily, weekly, and monthly summaries of your counted requests in the Analytics tab of your LiveDashboard.

If you want to exclude paths from the analytics in the dashboard, simply add them to router.ex:

    live_dashboard "/dashboard",
      additional_pages: [
        {:analytics, {AnalyticsEx.LiveDashboard.AnalyticsPage, ignore: ["/this-route", "/and-that-route-including-subpaths"]}
      ]

You will then not see any paths which match your ignore-rules in the Analytics overview.

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.