Skip to content

Commit

Permalink
Merge 5814773 into 6136c2f
Browse files Browse the repository at this point in the history
  • Loading branch information
RFreij committed Sep 30, 2020
2 parents 6136c2f + 5814773 commit 8994fbd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
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

0 comments on commit 8994fbd

Please sign in to comment.