Skip to content

Commit

Permalink
-change request format
Browse files Browse the repository at this point in the history
  • Loading branch information
arcticfalcon authored and jfalcondb committed Oct 28, 2014
1 parent 3e96626 commit 8c3f15d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
95 changes: 66 additions & 29 deletions src/ArcticFalcon/LaravelAnalytics/Data/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,27 @@ class Hit
* event category
* @var string
*/
private $category = 'email';
protected $category = null;

/**
* event action
* @var string
*/
private $action = 'open';
protected $action = null;

/**
* event label
* @var string
*/
private $label = null;
protected $label = null;

protected $title = null;

/**
* hit type
* @var string
*/
private $hitType = 'event';
protected $hitType = null;

protected $page = null;

Expand All @@ -53,13 +55,22 @@ class Hit

protected $hitCallback = null;

function __construct($hitType, $category, $action, $label = null, $value = null)
function __construct($hitType, $categoryOrPage = null, $actionOrTitle = null, $label = null, $value = null)
{
$this->setHitType($hitType);
$this->setCategory($category);
$this->setAction($action);
$this->setLabel($label);
$this->setValue($value);
if($this->hitType == static::PageView)
{
$this->setPage($categoryOrPage)
->setTitle($actionOrTitle);
}
if($this->hitType == static::Event)
{
$this->setCategory($categoryOrPage)
->setAction($actionOrTitle);
}

$this->setLabel($label)
->setValue($value);

return $this;
}
Expand All @@ -68,7 +79,7 @@ function __construct($hitType, $category, $action, $label = null, $value = null)
/**
* set action
* @param string $action
* @return Event
* @return Hit
*/
public function setAction($action)
{
Expand All @@ -90,7 +101,7 @@ public function getAction()
*
* @param string $category
*
* @return Event
* @return Hit
*/
public function setCategory($category)
{
Expand All @@ -112,7 +123,7 @@ public function getCategory()
*
* @param string $hitType
*
* @return Event
* @return Hit
*/
public function setHitType($hitType)
{
Expand All @@ -138,7 +149,7 @@ public function getHitType()
*
* @param string $label
*
* @return Event
* @return Hit
*/
public function setLabel($label)
{
Expand Down Expand Up @@ -194,6 +205,26 @@ public function setPage($page)
return $this;
}

/**
* @return null
*/
public function getTitle()
{
return $this->title;
}

/**
* @param null $title
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}




/**
* @return boolean
Expand Down Expand Up @@ -263,33 +294,39 @@ public function setHitCallback($hitCallback)

public function render()
{
$command = '';
$field = [];
if($this->nonInteraction == true)
{
$field['nonInteraction'] = 1;
}
if($this->page !== null)
{
$field['page'] = $this->page;
}
if($this->category !== null)
{
$field['eventCategory'] = $this->category;
}
if($this->action !== null)
{
$field['eventAction'] = $this->action;
}
if ($this->label !== null)
{
$command .= ", '{$this->label}'";
if ($this->value !== null)
{
$command .= ", {$this->value}";
}
$field['eventLabel'] = $this->label;
}

$field = [];
if($this->nonInteraction)
if ($this->value !== null)
{
$field['nonInteraction'] = 1;
$field['eventValue'] = $this->value;
}
foreach($this->customDimensions as $index => $value)
{
$field['dimension'.$index] = $value;
}
if($this->page)
{
$field['page'] = $this->page;
}

$field = json_encode($field, JSON_FORCE_OBJECT + JSON_UNESCAPED_SLASHES);

if($this->hitCallback)
if($this->hitCallback !== null)
{
$field = rtrim($field, '}');
if($field != '{')
Expand All @@ -308,7 +345,7 @@ public function render()
$field = '';
}

return "ga('send', '{$this->hitType}', '{$this->category}', '{$this->action}'{$command}{$field});";
return "ga('send', '{$this->hitType}'{$field});";
}

}
6 changes: 4 additions & 2 deletions tests/GoogleAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public function testHit()
{
$ga = new GoogleAnalytics($this->options, new TrackingBagMock);
$ga->trackHit(
(new Hit('event', 'mycat', 'myact'))
(new Hit(Hit::PageView))
->setPage('/mypage')
);

$render = $ga->render();

$this->assertContains("ga('send', 'event', 'mycat', 'myact', {\"page\":\"/mypage\"}", $render);
$this->assertContains("ga('send', 'pageview',", $render);
$this->assertContains("\"page\":\"/mypage\"", $render);

}
}

11 changes: 5 additions & 6 deletions tests/HitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ public function testConstructor()
$hit = new Hit(Hit::Event, 'mycat', 'myact', 'mylabel', 321);
$render = $hit->render();

$this->assertContains("'event',", $render);
$this->assertContains("'mycat',", $render);
$this->assertContains("'myact',", $render);
$this->assertContains("'mylabel',", $render);
$this->assertContains("321", $render);

$this->assertContains('event', $render);
$this->assertContains('eventCategory":"mycat"', $render);
$this->assertContains('eventAction":"myact"', $render);
$this->assertContains('eventLabel":"mylabel"', $render);
$this->assertContains('eventValue":321', $render);
}

public function testSetters()
Expand Down

0 comments on commit 8c3f15d

Please sign in to comment.