theiconic / php-ga-measurement-protocol

Send data to Google Analytics from the server using PHP. Implements GA measurement protocol.
MIT License
655 stars 136 forks source link

Batch request implemented #83

Closed alberto-bottarini closed 3 years ago

alberto-bottarini commented 4 years ago

This PR allows you to batching multiple hits in a single request using /batch endpoint.

It adds some new methods:

Here's an example:

$analytics = new Analytics(false, false);

$analytics
    ->setProtocolVersion('1')
    ->setTrackingId('UA-xxxxxx-x')
    ->setClientId('xxxxxx.xxxxxx');    

foreach(range(0, 19) as $i) {
    $analytics = $analytics
        ->setDocumentPath("/mypage$i")
        ->enqueuePageview(); //enqueue url without pushing
}

$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request

$analytics->emptyQueue();

foreach(range(20, 39) as $i) {
    $analytics = $analytics
        ->setDocumentPath("/mypage$i")
        ->enqueuePageview(); //enqueue url without pushing
}

$analytics->sendEnqueuedHits(); //push 20 pageviews in a single request

I updated tests and upgraded some dependencies

alberto-bottarini commented 4 years ago

@jorgeborges have you taken a look?

jorgeborges commented 4 years ago

I hope to review this one in the upcoming weeks @alberto-bottarini, but definitely this month.

alberto-bottarini commented 4 years ago

I was thinking... maybe I could add emptyQueue inside sendEnqueuedHits to automatically empty the queue after GA request

jorgeborges commented 4 years ago

@alberto-bottarini I think that's a good idea. That would make the feature more comfortable to use.

I would also prefer if after adding 20 events to the queue, it automatically sends them (and empties the queue), instead of throwing an Exception. Another approach would be to remove the first event in the queue and add the latest one, keeping it a 20.

Also, just noticed something missing from the PR is updating the README, can you please update it with a description of batch request feature and examples?

alberto-bottarini commented 4 years ago

Ok. I followed this "exception approach" to be more coherent with the actual code: you throw, for example, an exception if a mandatory parameter is missing.

But it's just philosophy. I prefer a clear api instead of magic behaviour like pushing data without a specific method invocation or removing item without notification.

Tell me what you like in order to implement it :)

About readme, you are right. I will update it as soon we define the new methods.

jorgeborges commented 4 years ago

OK @alberto-bottarini, I will take a look, thoroughly review the PR, and tell you how I think we should proceed in the upcoming weeks. Apologies for taking so long but I'm busy with the office work right now.

alberto-bottarini commented 4 years ago

Hi @jorgeborges, did you take a look?

alberto-bottarini commented 4 years ago

@jorgeborges any news?

jorgeborges commented 3 years ago

Terribly sorry about this one @alberto-bottarini, I have requested vacations from 21~25 September in about one week from now, I'll use some of those days to review and make any changes to this PR as needed.