Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically send batch request #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace TheIconic\Tracking\GoogleAnalytics;

use BadMethodCallException;
use TheIconic\Tracking\GoogleAnalytics\Exception\EnqueueUrlsOverflowException;
use TheIconic\Tracking\GoogleAnalytics\Exception\InvalidPayloadDataException;
use TheIconic\Tracking\GoogleAnalytics\Network\HttpClient;
use TheIconic\Tracking\GoogleAnalytics\Network\PrepareUrl;
Expand Down Expand Up @@ -327,7 +326,6 @@ class Analytics
* @var string
*/
protected $batchEndpoint = '://www.google-analytics.com/batch';


/**
* Indicates if the request is in debug mode(validating hits).
Expand Down Expand Up @@ -373,7 +371,7 @@ class Analytics
* @var array
*/
protected $options = [];

/**
* Initializes to a list of all the available parameters to be sent in a hit.
*
Expand Down Expand Up @@ -622,8 +620,8 @@ protected function sendHit($methodName)
protected function enqueueHit($methodName)
{

if(count($this->enqueuedUrls) == 20) {
throw new EnqueueUrlsOverflowException();
if (count($this->enqueuedUrls) == 20) {
$this->sendEnqueuedHits();
}

$hitType = strtoupper(substr($methodName, 7));
Expand All @@ -643,7 +641,7 @@ protected function enqueueHit($methodName)
*/
protected function setAndValidateHit($hitType)
{

$hitConstant = $this->getParameterClassConstant(
'TheIconic\Tracking\GoogleAnalytics\Parameters\Hit\HitType::HIT_TYPE_' . $hitType,
'Hit type ' . $hitType . ' is not defined, check spelling'
Expand Down Expand Up @@ -698,7 +696,7 @@ protected function getHttpClientOptions()
*/
public function getUrl($onlyQuery = false)
{
$prepareUrl = new PrepareUrl;
$prepareUrl = new PrepareUrl();

return $prepareUrl->build(
$this->getEndpoint(),
Expand Down Expand Up @@ -950,6 +948,16 @@ public function emptyQueue()
return $this;
}

/**
* Determine if enqueued urls are present
*
* @return bool
*/
public function hasEnqueuedUrls()
{
return count($this->enqueuedUrls) > 0;
}

/**
* Routes the method call to the adequate protected method.
*
Expand Down
36 changes: 32 additions & 4 deletions tests/TheIconic/Tracking/GoogleAnalytics/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,22 @@ public function testSendBatchHitsAfterEmpty()
$this->analytics->sendEnqueuedHits();
}

/**
* @expectedException \TheIconic\Tracking\GoogleAnalytics\Exception\EnqueueUrlsOverflowException
*/
public function testEnqueueOverflowException()

public function testEnqueueAutomaticBatchSent()
{
$httpClient = $this->getMock('TheIconic\Tracking\GoogleAnalytics\Network\HttpClient', ['batch']);
$this->analytics->setHttpClient($httpClient);

$httpClient->expects($this->once())
->method('batch')
->with(
'http://www.google-analytics.com/batch',
array_merge(
['v=1&tid=555&cid=666&dp=%5Cmypage&t=pageview'],
array_fill(0, 19, 'v=1&tid=555&cid=666&dp=%5Cmypage2&t=pageview')
)
);

$this->analytics
->setDebug(true)
->setProtocolVersion('1')
Expand Down Expand Up @@ -410,6 +421,23 @@ public function testEnqueueOverflowException()
->enqueuePageview();
}

public function testHasEnqueuedUrls()
{
$this->analytics
->setDebug(true)
->setProtocolVersion('1')
->setTrackingId('555')
->setClientId('666')
->setDocumentPath('\mypage')
->enqueuePageview();

$this->assertTrue($this->analytics->hasEnqueuedUrls());

$this->analytics->emptyQueue();

$this->assertFalse($this->analytics->hasEnqueuedUrls());
}

public function testEmptyBatchHits()
{
$httpClient = $this->getMock('TheIconic\Tracking\GoogleAnalytics\Network\HttpClient', ['batch']);
Expand Down