Simple analytics backed by MongoDB. It gets you from having bunch of events occuring within few minutes to being able to say what happened on 19th August 2023.
For comprehensive guides, API reference, and examples, visit trifle.io/trifle-stats-ex
Add trifle_stats to your list of dependencies in mix.exs:
def deps do
[
{:trifle_stats, "~> 1.0"}
]
endThen run:
$ mix deps.get# config/config.exs
config :trifle_stats,
driver: Trifle.Stats.Driver.Mongo,
granularities: [:minute, :hour, :day, :week, :month, :quarter, :year]
# Configure MongoDB connection
config :trifle_stats, Trifle.Stats.Driver.Mongo,
hostname: "localhost",
database: "trifle_stats",
port: 27017Trifle.Stats.track("event::logs", DateTime.utc_now(), %{count: 1, duration: 2.11})Trifle.Stats.values("event::logs", DateTime.utc_now |> DateTime.add(-30, :day), DateTime.utc_now(), :day)
#=> %{
# at: [~U[2023-08-19 00:00:00Z]],
# values: [%{"count" => 1, "duration" => 2.11}]
# }- Multiple time granularities - Track data across different time periods
- MongoDB backend - Reliable document-based storage
- Phoenix integration - Easy integration with Phoenix applications
- Performance optimized - Efficient storage and retrieval patterns
- Elixir native - Built for the Elixir/OTP ecosystem
Currently supports:
- MongoDB - Document database with aggregation pipeline support
Configure your application in config/config.exs:
config :trifle_stats,
driver: Trifle.Stats.Driver.Mongo,
granularities: [:minute, :hour, :day, :week, :month, :quarter, :year]Tests verify tracking functionality and data retrieval across time granularities. To run the test suite:
$ mix testEnsure MongoDB is running locally for tests to pass.
Tests are meant to be simple and isolated. Every test should be independent and able to run in any order. Tests should be self-contained and set up their own configuration.
Use single layer testing to focus on testing a specific module or function in isolation. Use appropriate mocking for external dependencies when testing higher-level operations.
Repeat yourself in test setup for clarity rather than complex shared setups that can hide dependencies.
Tests verify that events are properly tracked, time granularities are correctly calculated, and data retrieval returns expected results. Database tests use test-specific collections to avoid conflicts.
Bug reports and pull requests are welcome on GitHub at https://github.com/trifle-io/trifle_stats.
The package is available as open source under the terms of the MIT License.