Phoenix Analytics Documentation

repository·master·Indexed 19 days ago

https://github.com/lalabuy948/phoenixanalytics

An embedded, plug-and-play analytics tool for Phoenix applications designed to track user behavior and application performance. It supports multiple database backends via Ecto, including PostgreSQL, MySQL, and SQLite. The library provides a request tracking plug, a built-in analytics dashboard with time-range shortcuts, and utilities for querying popular pages, external referrers, 404 errors, user agents, and device types.

Tokens
6.7K
Snippets
48
Records
49
Agent score
64%

What's inside Phoenix Analytics

  1. Enable request tracking in Phoenix

    master

    To start collecting analytics data, add the PhoenixAnalytics.Plugs.RequestTracker plug to your endpoint.ex file.

    Important: You must add this plug immediately after Plug.Static to ensure it captures requests correctly.

    plug PhoenixAnalytics.Plugs.RequestTracker
  2. Run the PhoenixSqlite example server

    master

    To start the PhoenixSqlite development server, follow these steps:

    1. Install dependencies: Run mix setup to install and configure all necessary project dependencies.
    2. Start the server: You can start the Phoenix endpoint using the standard Mix task:
      mix phx.server
      Alternatively, start it within an interactive Elixir shell (IEx) to allow for real-time debugging:
      iex -S mix phx.server
    3. Access the application: Once the server is running, visit http://localhost:4000 in your web browser.
    mix setup
    mix phx.server
  3. Start the PhoenixPostgres server

    master

    To set up and run the PhoenixPostgres example server locally, follow these steps:

    1. Install dependencies: Run mix setup to install and configure all necessary project dependencies.
    2. Start the server: You can start the Phoenix endpoint using the standard Phoenix command:
      mix phx.server
      Alternatively, if you want to start the server within an interactive Elixir shell (IEx) for debugging or manual interaction, use:
      iex -S mix phx.server
    3. Access the application: Once the server is running, visit http://localhost:4000 in your web browser.
    mix setup
    mix phx.server
  4. Run Phoenix Analytics migrations

    master

    You must create the necessary analytics tables in your database. You can do this using standard Ecto migrations or by running the migration module directly in an interactive shell.

    Using Ecto Migrations

    1. Generate a migration:
      mix ecto.gen.migration add_phoenix_analytics
    2. Implement the up and down functions using PhoenixAnalytics.Migration:
      defmodule MyApp.Repo.Migrations.AddPhoenixAnalytics do
        use Ecto.Migration
      
        def up, do: PhoenixAnalytics.Migration.up()
        def down, do: PhoenixAnalytics.Migration.down()
      end
    3. Create indexes (Note: SQLite3 does not support these indexes):
      defmodule MyApp.Repo.Migrations.AddPhoenixAnalyticsIndexes do
        use Ecto.Migration
        def change do
          PhoenixAnalytics.Migration.add_indexes()
        end
      end
    4. Run the migration:
      mix ecto.migrate

    Direct Migration (Alternative)

    If you do not use migrations, run this in iex:

    PhoenixAnalytics.Migration.up()
    mix ecto.gen.migration add_phoenix_analytics
  5. Run the PhoenixMysql example server

    master

    To start the Phoenix server for the PhoenixMysql example, follow these steps in your terminal:

    1. Install and set up all necessary dependencies using mix setup.
    2. Start the Phoenix endpoint using mix phx.server or start an interactive Elixir shell with the server running using iex -S mix phx.server.

    Once started, the application is accessible at http://localhost:4000.

    mix setup
    mix phx.server
  6. Configure Phoenix Analytics

    master

    Phoenix Analytics uses your existing Ecto repository. Configure the :phoenix_analytics application in your configuration files (e.g., config/dev.exs) with the following keys:

    • repo: Your application's Ecto Repo module.
    • app_domain: The domain of your application (defaults to example.com).
    • cache_ttl: Cache time-to-live in seconds (defaults to 60).
    # config/dev.exs
    config :phoenix_analytics,
      repo: MyApp.Repo,
      app_domain: System.get_env("PHX_HOST") || "example.com",
      cache_ttl: System.get_env("CACHE_TTL") || 60
  7. Configure Phoenix Analytics in your application

    master

    To use Phoenix Analytics, you must configure the :phoenix_analytics application in your project's configuration files (e.g., config/config.exs or config/dev.exs).

    At a minimum, you must provide your application's Ecto repository module. You can also optionally configure the application domain for filtering external referrers, the cache TTL, and the OTP application name.

    config :phoenix_analytics,
      repo: MyApp.Repo,
      app_domain: "example.com",
      cache_ttl: 60,
      otp_app: :my_app
  8. Add the Phoenix Analytics dashboard route

    master

    To access the analytics dashboard, add the phoenix_analytics_dashboard/2 macro to your router.ex file. This requires using the PhoenixAnalytics.Web, :router module.

    use PhoenixAnalytics.Web, :router
    
    phoenix_analytics_dashboard "/analytics"
  9. Configure PostgreSQL via Docker Compose

    master

    The postgres service uses the postgres:17-alpine image. You can configure the database name, user, and password using the following environment variables:

    • POSTGRES_DB: The name of the database to create (default: phoenix_postgres_dev).
    • POSTGRES_USER: The superuser name (default: postgres).
    • POSTGRES_PASSWORD: The superuser password (default: postgres).

    The service maps port 5432 on the host to 5432 in the container and uses a named volume postgres_data for persistent storage.

    postgres:
      image: postgres:17-alpine
      environment:
        POSTGRES_DB: phoenix_postgres_dev
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
      ports:
        - "5432:5432"
      volumes:
        - postgres_data:/var/lib/postgresql/data
  10. Configure MySQL via Docker Compose

    master

    The mysql service uses the mysql:8.4 image. You can configure the database using the following environment variables:

    • MYSQL_DATABASE: The name of the database to create (default: phoenix_mysql_dev).
    • MYSQL_ALLOW_EMPTY_PASSWORD: Set to true to allow connections without a password.

    The service maps port 3306 on the host to 3306 in the container and uses a named volume mysql_data for persistent storage.

    mysql:
      image: mysql:8.4
      environment:
        MYSQL_DATABASE: phoenix_mysql_dev
        MYSQL_ALLOW_EMPTY_PASSWORD: true
      ports:
        - "3306:3306"
      volumes:
        - mysql_data:/var/lib/mysql
  11. Dashboard time range shortcuts

    master

    The Phoenix Analytics dashboard supports keyboard shortcuts for quickly filtering data by time ranges:

    ShortcutRange
    ttoday
    ctrl+tyesterday
    wlast week
    mlast 30 days
    qlast 90 days
    ylast 12 months
    ctrl+wprevious week
    ctrl+mprevious month
    ctrl+qprevious quarter
    ctrl+yprevious year
    aall time