Skip to content

Commit 882ff15

Browse files
committed
Add documentation for recording metrics with custom attributes
1 parent c932148 commit 882ff15

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Track page views, API calls, user signups, or any other countable events.
3030
- [Recording with Categories](#recording-with-categories)
3131
- [Recording with Dates](#recording-with-dates)
3232
- [Recording for Models](#recording-for-models)
33+
- [Recording with Custom Attributes](#recording-with-custom-attributes)
3334
- [Capturing & Committing](#capturing--committing)
3435
- [Querying Metrics](#querying-metrics)
3536
- [Testing](#testing)
@@ -254,6 +255,105 @@ $ordersThisMonth = $customer->metrics()
254255
->sum('value');
255256
```
256257

258+
### Recording with Custom Attributes
259+
260+
Store additional context with your metrics by adding custom attributes. This is useful for segmenting metrics by various dimensions like source, country, device type, or any other custom data.
261+
262+
First, create a migration to add custom columns to the `metrics` table:
263+
264+
```php
265+
use Illuminate\Database\Schema\Blueprint;
266+
use Illuminate\Support\Facades\Schema;
267+
268+
Schema::table('metrics', function (Blueprint $table) {
269+
$table->string('source')->nullable()->index();
270+
$table->string('country')->nullable()->index();
271+
$table->string('device')->nullable();
272+
});
273+
```
274+
275+
Then, use the `with()` method to record metrics with custom attributes:
276+
277+
```php
278+
// Track page views with traffic source
279+
metric('page_views')
280+
->with(['source' => 'google'])
281+
->record();
282+
283+
// Track conversions with multiple attributes
284+
metric('conversions')
285+
->with([
286+
'source' => 'facebook',
287+
'country' => 'US',
288+
'device' => 'mobile',
289+
])
290+
->record();
291+
292+
// Combine with other methods
293+
metric('api:requests')
294+
->category('users')
295+
->with(['client_id' => 'abc123'])
296+
->record();
297+
```
298+
299+
Custom attributes are included in the metric's uniqueness check, meaning metrics with different attribute values are stored separately:
300+
301+
```php
302+
metric('page_views')->with(['source' => 'google'])->record(); // Creates metric #1
303+
metric('page_views')->with(['source' => 'facebook'])->record(); // Creates metric #2
304+
metric('page_views')->with(['source' => 'google'])->record(); // Increments metric #1
305+
```
306+
307+
This allows you to segment and analyze metrics by any dimension:
308+
309+
```php
310+
// Get page views by source
311+
$googleViews = Metric::where('name', 'page_views')
312+
->where('source', 'google')
313+
->sum('value');
314+
315+
// Get conversions by country this month
316+
$conversions = Metric::thisMonth()
317+
->where('name', 'conversions')
318+
->get()
319+
->groupBy('country')
320+
->map->sum('value');
321+
322+
// Get mobile vs desktop traffic
323+
$mobileViews = Metric::today()
324+
->where('name', 'page_views')
325+
->where('device', 'mobile')
326+
->sum('value');
327+
```
328+
329+
You can also use custom attributes with the `MetricData` class:
330+
331+
```php
332+
use DirectoryTree\Metrics\MetricData;
333+
use DirectoryTree\Metrics\Facades\Metrics;
334+
335+
Metrics::record(new MetricData(
336+
name: 'page_views',
337+
additional: [
338+
'source' => 'google',
339+
'country' => 'US',
340+
]
341+
));
342+
```
343+
344+
Or with the `PendingMetric` class:
345+
346+
```php
347+
use DirectoryTree\Metrics\PendingMetric;
348+
349+
PendingMetric::make('page_views')
350+
->with(['source' => 'google', 'country' => 'US'])
351+
->record();
352+
```
353+
354+
> [!important]
355+
> Core metric attributes (`name`, `category`, `year`, `month`, `day`, `measurable_type`, `measurable_id`, `value`) cannot be overridden via custom attributes. They are protected and will always use the values set through their respective methods.
356+
257357
### Capturing & Committing
258358

259359
For high-performance scenarios, you may capture metrics in memory and commit them in batches:

0 commit comments

Comments
 (0)