Skip to content

Commit af8bdc5

Browse files
committed
docs: add batch hit documentation to README
1 parent a6ebe08 commit af8bdc5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,58 @@ $analytics->setEventCategory('Checkout')
197197
->sendEvent();
198198
```
199199

200+
### Batch Hits
201+
202+
GA has an endpoint that you can hit to register multiple hits at once, with a limit of 20 hits. Hits to be send can be placed in a queue as you build up the Analytics object.
203+
204+
Here's an example that sends two hits, and then empties the queue.
205+
206+
```php
207+
$analytics = new Analytics(false, false);
208+
209+
$analytics
210+
->setProtocolVersion('1')
211+
->setTrackingId('UA-xxxxxx-x')
212+
->setClientId('xxxxxx.xxxxxx');
213+
214+
foreach(range(0, 19) as $i) {
215+
$analytics = $analytics
216+
->setDocumentPath("/mypage$i")
217+
->enqueuePageview(); //enqueue url without pushing
218+
}
219+
220+
$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request and empties the queue
221+
```
222+
223+
The queue is emptied when the hits are sent, but it can also be empty manually with `emptyQueue` method.
224+
225+
```php
226+
$analytics = new Analytics(false, false);
227+
228+
$analytics
229+
->setProtocolVersion('1')
230+
->setTrackingId('UA-xxxxxx-x')
231+
->setClientId('xxxxxx.xxxxxx');
232+
233+
foreach(range(0, 5) as $i) {
234+
$analytics = $analytics
235+
->setDocumentPath("/mypage$i")
236+
->enqueuePageview(); //enqueue url without pushing
237+
}
238+
239+
$analytics->emptyQueue(); // empty queue, allows to enqueue 20 hits again
240+
241+
foreach(range(1, 20) as $i) {
242+
$analytics = $analytics
243+
->setDocumentPath("/mypage$i")
244+
->enqueuePageview(); //enqueue url without pushing
245+
}
246+
247+
$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request and empties the queue
248+
```
249+
250+
If more than 20 hits are attempted to be enqueue, the library will throw a `EnqueueUrlsOverflowException`.
251+
200252
### Validating Hits
201253

202254
From Google Developer Guide:

0 commit comments

Comments
 (0)