You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#20 - Support creating analytics via queued job (#21)
* feat(#20): Introduce support for tracking requests via a queued job
BREAKING CHANGE: Changes to the API to support tracking requests via a queued job
* refactor(#20): Only support PHP 8.0 for this version
* refactor(#20): Update ubuntu version and node version
Copy file name to clipboardexpand all lines: README.md
+22-98
Original file line number
Diff line number
Diff line change
@@ -79,9 +79,7 @@ The default migration assumes you users are stored in a `users` table with a `BI
79
79
When logging a request, the package will use this code to insert the `user_id` into the `analytics` table:
80
80
81
81
```php
82
-
if ($user = $request->user()) {
83
-
$userId = $user->id;
84
-
}
82
+
$userId = $request->user()?->id ?? null
85
83
```
86
84
87
85
### Excluding Routes
@@ -133,104 +131,38 @@ return [
133
131
];
134
132
```
135
133
136
-
### Post Request Hooks
137
-
138
-
We provide an optional hook you can use to run custom logic after an analytics record has been created. You can provide as many hooks as you want, calling `addPostHook` will add a new hook rather than replace existing hooks.
139
-
140
-
The hook closure is based an instance of `RequestDetails` and the `Analytics` record that was created for the request. You can access both the request and response inside the `RequestDetails` class. If you're using a custom `RequestDetails` class, you'll be given an instance of your custom class.
141
-
142
-
```php
143
-
// AppServiceProvider
144
-
145
-
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
146
-
use OhSeeSoftware\LaravelServerAnalytics\RequestDetails;
147
-
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;
148
-
149
-
public function boot()
150
-
{
151
-
ServerAnalytics::addPostHook(
152
-
function (RequestDetails $requestDetails, Analytics $analytics) {
153
-
// Do whatever you want with the request, response, and created analytics record
154
-
}
155
-
);
156
-
}
157
-
```
134
+
### Request hooks
158
135
159
-
### Attach Entities to Analytics Records
136
+
We provide took optional hooks that you can use to automatically associate extra data with your analytics records: `addMetaHook` and `addRelationHook`. Both hooks return an array of data that should be associated to the analytics record. You can add as many of each as you'd like throughout your request's lifecycle.
160
137
161
-
If you want to attach your application's entities to an analytics record, you can use the `addRelation(Model $model, ?string $reason = null)` on the Analytics model in combination with a hook:
138
+
`addMetaHook` example:
162
139
163
140
```php
164
-
// AppServiceProvider
165
-
166
-
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
167
-
use OhSeeSoftware\LaravelServerAnalytics\RequestDetails;
168
-
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;
169
-
170
-
public function boot()
171
-
{
172
-
ServerAnalytics::addPostHook(
173
-
function (RequestDetails $requestDetails, Analytics $analytics) {
174
-
// Attach the logged-in user to the analytics request record
175
-
if ($user = $requestDetails->request->user()) {
176
-
$analytics->addRelation($user);
177
-
}
178
-
}
179
-
);
180
-
}
181
-
```
182
-
183
-
There's also a helper method available if you want to attach a relation without checking the request, response, or analytics record:
184
-
185
-
```php
186
-
// Controller
187
-
188
-
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
The method provides an optional second argument, `$reason`, which allows you to add a reason for the relation attachment. The `reason` column is a `string, VARCHAR(255)` column. If not passed, the value will be `null`.
199
-
200
-
### Attach Metadata to Analytics Records
201
-
202
-
In addition to attaching entities to your analytics records, you can attach custom metadata (key/value).
149
+
`addRelationHook` example:
203
150
204
151
```php
205
-
// AppServiceProvider
206
-
207
-
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
208
-
use OhSeeSoftware\LaravelServerAnalytics\RequestDetails;
209
-
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;
210
-
211
-
public function boot()
212
-
{
213
-
ServerAnalytics::addPostHook(
214
-
function (RequestDetails $requestDetails, Analytics $analytics) {
215
-
$analytics->addMeta('foo', 'bar');
216
-
}
217
-
);
218
-
}
152
+
ServerAnalytics::addRelationHook(function (RequestDetails $requestDetails) use ($post) {
153
+
return [
154
+
'model' => $post,
155
+
'reason' => 'Post that was deleted.'
156
+
];
157
+
});
219
158
```
220
159
221
-
There's also a helper method available if you want to attach metadata without checking the request, response, or analytics record:
160
+
There's also helper methods available which call the above hooks for you:
222
161
223
162
```php
224
-
// Controller
225
-
226
-
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
With the above example, the `method` variable for every request will be set to "TEST".
188
+
There's a config value, `request_details_class`, which you can set to the FQN of your request details class. We will attempt to resolve that class out of the container when logging requests.
0 commit comments