Cherrycake
ExamplesGithub
version 2.x alpha
version 2.x alpha
  • Introduction
  • Status
  • Changelog
  • Migration
  • Architecture
    • Basics
    • Modules
    • Classes
    • Lifecycle
      • Deep lifecycle
    • Performance
    • Security
    • Patterns
      • Files structure
    • Items
    • Server requirements
  • Guide
    • Getting started
      • Skeleton start
      • Docker start
    • Modules guide
    • Classes guide
    • Actions guide
      • Complex actions
      • Variable path components
      • Accept GET or POST parameters
      • Getting the URL of an action
      • Cached actions
      • Brute force attacks
    • Patterns guide
      • Passing variables to a pattern
      • Nested patterns
      • Cached patterns
    • Cache guide
      • Time To Live
      • Using cache
      • Lists
      • Queues
      • Pools
    • Database guide
      • Basic queries
      • Prepared queries
      • Cached queries
      • Cache key naming
      • Removing queries from cache
    • Items guide
      • Item cache
      • Item lists
      • Items custom filters
      • Items custom ordering
      • Mixing filters and ordering
      • Items with relationships
      • Items cache
    • HtmlDocument guide
    • Css and Javascript guide
      • Modules injecting CSS and JavaScript
    • Session guide
    • Login guide
      • Creating a complete login workflow
    • Locale guide
      • Multilingual texts
      • Domain based site localization
    • Log guide
      • Loading Log events from the database
    • Stats guide
      • Stats events with additional dimensions
      • Loading Stats events from the database
    • Janitor guide
      • Janitor tasks configuration files
    • Command line interface
    • Debugging
  • Reference
    • Core modules
      • Actions
        • Actions methods
      • Browser
      • Cache
        • Cache methods
      • Css
        • Css methods
      • Database
      • Email
      • Errors
      • HtmlDocument
        • HtmlDocument methods
      • ItemAdmin
      • Janitor
        • Janitor methods
      • Javascript
        • Javascript methods
      • Locale
        • Locale methods
      • Log
        • Log methods
      • Login
        • Login methods
      • Output
        • Output methods
      • Patterns
        • Patterns methods
      • Security
        • Security methods
      • Session
        • Session methods
      • Stats
        • Stats methods
      • SystemLog
      • TableAdmin
      • Translation
      • Validate
    • Core classes
      • Action
        • Action methods
        • Action properties
      • AjaxResponseJson
      • BasicObject
        • BasicObject methods
      • CacheProvider
        • CacheProvider methods
      • Color
      • DatabaseProvider
        • DatabaseProvider methods
      • DatabaseResult
        • DatabaseResult methods
        • DatabaseResult properties
      • DatabaseRow
      • Engine
        • Engine methods
        • Engine properties
      • Gradient
      • Item
        • Item methods
        • Item properties
      • Items
        • Items methods
        • Items properties
      • Image
      • JanitorTask
        • JanitorTask methods
        • JanitorTask properties
      • LogEvent
        • LogEvent methods
        • LogEvent Properties
      • LogEvents
        • LogEvents methods
      • Module
        • Module methods
        • Module properties
      • Response
      • Request
        • Request methods
      • RequestParameter
        • RequestParameter methods
      • RequestPathComponent
        • RequestPathComponent methods
      • Result
      • StatsEvent
        • StatsEvent properties
      • StatsEvents
        • StatsEvents methods
      • SystemLogEvent
        • SystemLogEvent methods
        • SystemLogEvent properties
      • SystemLogEvents
        • SystemLogEvents methods
  • Code conventions
  • License
  • Extras
Powered by GitBook
On this page
  • Setting up the Stats database table
  • Creating a StatsEvent
  • Triggering a Stats event
  • What's the difference between a LogEvent and a StatsEvent?

Was this helpful?

  1. Guide

Stats guide

PreviousLoading Log events from the databaseNextStats events with additional dimensions

Last updated 3 years ago

Was this helpful?

The module stores statistical events in a persistent log as they occur, aimed at providing insight about the activity in your app like the number of received visits in a web page, page views, clicks, hits or other similar statistical data.

Setting up the Stats database table

Events are stored in the cherrycake_stats database table using a shared-memory buffer and a programmed commit task for optimal performance, resulting in a system capable of ingesting many events per second without a noticeable performance impact.

You can create the Stats table in your database by importing the stats.sql file you'll find in the , under the install/database directory.

Creating a StatsEvent

events are stored as objects that extend the base class. You must create one class per each statistical data point you want to store.

Let's say you want to keep track of the number of views received by the home page of your web site app every day. To do so we'll create a new class called StatsEventHomeView in the classes/StatsEventHomeView.class.php file, like this:

<?php

namespace CherrycakeApp;

class StatsEventHomeView extends \Cherrycake\Stats\StatsEvent {
	protected $timeResolution = \Cherrycake\Stats\STATS_EVENT_TIME_RESOLUTION_DAY;
	protected $typeDescription = "Home view";
}

Note that we specified as the timeResolution for our StatsEventHomeView. This will cause this event to be counted in a daily basis, meaning only one row will be stored in the database for each different day, along containing a counter of the number of times the event has been triggered during that day.

You can specify other time resolutions as well, all of them are self-explanatory:

  • STATS_EVENT_TIME_RESOLUTION_MINUTE

  • STATS_EVENT_TIME_RESOLUTION_HOUR

  • STATS_EVENT_TIME_RESOLUTION_DAY

  • STATS_EVENT_TIME_RESOLUTION_MONTH

  • STATS_EVENT_TIME_RESOLUTION_YEAR

You should choose the time resolution that better fits your needs, keeping in mind that events defined with a smaller time resolution will need more rows in the database.

Triggering a Stats event

$e->Stats->trigger(new StatsEventHomeView);

What's the difference between a LogEvent and a StatsEvent?

If you need a precise log of events instead of the statistical approach of counting the times an event is triggered within a time frame, see the module instead.

In the part of your code where you want to trigger the stats event, call the method like this:

See this example working in the site.

The module stores an object in the database for each logged event, allowing individual events to hold their own unique data, but causing the database to grow rapidly when lots of objects are stored.

, in the other hand, stores a single object in the database for all the events triggered during a certain period. This doesn't allows for individual events to hold their own differentiating data because it only stores the number of times a certain event has been triggered, but makes a more suitable solution to store big amounts of events, and it's an ideal solution to store statistical data of any kind.

Log guide
Cherrycake documentation examples
Log
LogEvent
Stats
Stats
Stats
Stats
Janitor
Cherrycake skeleton repository
Stats
StatsEvent
StatsEvent
Stats::trigger
STATS_EVENT_TIME_RESOLUTION_DAY