| 1 |
# WPMUDEV Analytics |
| 2 |
|
| 3 |
This module: |
| 4 |
|
| 5 |
### Tracks events: |
| 6 |
The module counts the number of plugin’s events triggered in a day. |
| 7 |
|
| 8 |
### Sets a limit: |
| 9 |
Each plugin can set a limit. |
| 10 |
|
| 11 |
### Prevents excess events: |
| 12 |
|
| 13 |
Once the limit is reached: |
| 14 |
|
| 15 |
The module triggers a new event `exceeded_daily_limit` to Mixpanel. |
| 16 |
|
| 17 |
The module stops sending all Mixpanel events from that site for 24 hours. |
| 18 |
|
| 19 |
## Devs |
| 20 |
|
| 21 |
This module exposes a single class `WPMUDEV_Analytics` which is a thin wrapper over the MixPanel class from the MixPanel composer dependency. |
| 22 |
|
| 23 |
This means that all the methods that can be called on an instance of the MixPanel class can also be called on an instance of WPMUDEV_Analytics class. |
| 24 |
|
| 25 |
Internally WPMUDEV_Analytics keeps track of event counts and stops sending events if the limit is exceeded. |
| 26 |
|
| 27 |
Please note that a scoped version of the composer dependency is already included within this package so it does not need to be included by plugins. |
| 28 |
|
| 29 |
Example usage: |
| 30 |
``` |
| 31 |
if ( ! class_exists( 'WPMUDEV_Analytics' ) ) { |
| 32 |
require_once YOUR_SUBMODULES_DIR . '/wpmudev-analytics/autoload.php'; |
| 33 |
} |
| 34 |
$analytics = new WPMUDEV_Analytics( 'slug', 'Plugin Name', $event_limit, $token, $mixpanel_options ); |
| 35 |
$analytics->identify( 'unique_id' ); |
| 36 |
$analytics->registerAll( $super_properties ); |
| 37 |
$analytics->track($event, $properties); |
| 38 |
``` |
| 39 |
|
| 40 |
## QA Testing |
| 41 |
|
| 42 |
In production the limit will be high and when it is reached, the site will stop sending events for 24 hours. |
| 43 |
|
| 44 |
To make testing convenient two constants have been included that can override the default behavior: |
| 45 |
|
| 46 |
*WPMUDEV_ANALYTICS_EVENT_LIMIT* |
| 47 |
|
| 48 |
This overrides the limit. |
| 49 |
|
| 50 |
*WPMUDEV_ANALYTICS_TIME_WINDOW_SECONDS* |
| 51 |
|
| 52 |
This overrides the time duration for which the site will be blocked from sending new events. |
| 53 |
|
| 54 |
Let's say the following constants are defined: |
| 55 |
``` |
| 56 |
define( 'WPMUDEV_ANALYTICS_TIME_WINDOW_SECONDS', 5 * 60 ); |
| 57 |
define( 'WPMUDEV_ANALYTICS_EVENT_LIMIT', 3 ); |
| 58 |
``` |
| 59 |
If more than 3 events are generated within 5 minutes then only 3 of those events will be tracked and one new `exceeded_daily_limit` event will be generated. |
| 60 |
|
| 61 |
The site will not send any new events for the rest of the current 5-minute window. |
| 62 |
|
| 63 |
In other words when the above constants are defined then this module ensures that 3 _or fewer_ events are generated every 5 minutes. |
| 64 |
|