Skip to content

Commit

Permalink
Merge pull request #29 from snapshotpl/master
Browse files Browse the repository at this point in the history
analytics.js
  • Loading branch information
Jurian Sluiman committed Nov 6, 2013
2 parents fdef2ca + 1b4844f commit 2a1838d
Show file tree
Hide file tree
Showing 20 changed files with 1,075 additions and 389 deletions.
62 changes: 5 additions & 57 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,12 @@
use Zend\ModuleManager\Feature;
use Zend\Mvc\MvcEvent;

use SlmGoogleAnalytics\Analytics;
use SlmGoogleAnalytics\View\Helper;

class Module implements
Feature\AutoloaderProviderInterface,
Feature\ConfigProviderInterface,
Feature\ViewHelperProviderInterface,
Feature\ServiceProviderInterface,
Feature\BootstrapListenerInterface
{

public function getAutoloaderConfig()
{
return array(
Expand All @@ -73,55 +69,6 @@ public function getConfig()
return include __DIR__ . '/config/module.config.php';
}

public function getViewHelperConfig()
{
return array(
'factories' => array(
'googleAnalytics' => function($sm) {
$tracker = $sm->getServiceLocator()->get('google-analytics');
$helper = new Helper\GoogleAnalytics($tracker);

return $helper;
},
),
);
}

public function getServiceConfig()
{
return array(
'aliases' => array(
'google-analytics' => 'SlmGoogleAnalytics\Analytics\Tracker',
),
'factories' => array(
'SlmGoogleAnalytics\Analytics\Tracker' => function($sm) {
$config = $sm->get('config');
$config = $config['google_analytics'];

$tracker = new Analytics\Tracker($config['id']);

if (isset($config['domain_name'])) {
$tracker->setDomainName($config['domain_name']);
}

if (isset($config['allow_linker'])) {
$tracker->setAllowLinker($config['allow_linker']);
}

if (true === $config['anonymize_ip']) {
$tracker->setAnonymizeIp(true);
}

if (false === $config['enable']) {
$tracker->setEnableTracking(false);
}

return $tracker;
},
),
);
}

/**
* When the render event is triggered, we invoke the view helper to
* render the javascript code.
Expand All @@ -131,17 +78,18 @@ public function getServiceConfig()
public function onBootstrap(EventInterface $e)
{
$app = $e->getParam('application');
$sm = $app->getServiceManager();
$em = $app->getEventManager();

if (!$app->getRequest() instanceof HttpRequest) {
return;
}

$sm = $app->getServiceManager();
$em = $app->getEventManager();

$em->attach(MvcEvent::EVENT_RENDER, function(MvcEvent $e) use ($sm) {
$view = $sm->get('ViewHelperManager');
$plugin = $view->get('googleAnalytics');
$plugin();
$plugin->appendScript();
});
}
}
21 changes: 21 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,26 @@
'domain_name' => '',
'allow_linker' => false,
'anonymize_ip' => false,
'script' => 'google-analytics-ga',
),
'service_manager' => array(
'aliases' => array(
'google-analytics' => 'SlmGoogleAnalytics\Analytics\Tracker',
'google-analytics-universal' => 'SlmGoogleAnalytics\View\Helper\Script\Analyticsjs',
'google-analytics-ga' => 'SlmGoogleAnalytics\View\Helper\Script\Gajs',
),
'invokables' => array(
'SlmGoogleAnalytics\View\Helper\Script\Analyticsjs' => 'SlmGoogleAnalytics\View\Helper\Script\Analyticsjs',
'SlmGoogleAnalytics\View\Helper\Script\Gajs' => 'SlmGoogleAnalytics\View\Helper\Script\Gajs',
),
'factories' => array(
'SlmGoogleAnalytics\Analytics\Tracker' => 'SlmGoogleAnalytics\Service\TrackerFactory',
'SlmGoogleAnalytics\Service\ScriptFactory' => 'SlmGoogleAnalytics\Service\ScriptFactory',
),
),
'view_helpers' => array(
'factories' => array(
'googleAnalytics' => 'SlmGoogleAnalytics\Service\GoogleAnalyticsFactory',
),
)
);
39 changes: 17 additions & 22 deletions src/SlmGoogleAnalytics/Analytics/CustomVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@

class CustomVariable
{
const SCOPE_VISITOR = 1;
const SCOPE_SESSION = 2;
const SCOPE_VISITOR = 1;
const SCOPE_SESSION = 2;
const SCOPE_PAGE_LEVEL = 3;

protected $index;
protected $name;
protected $value;
protected $scope;

public function __construct ($index, $name, $value, $scope = self::SCOPE_PAGE_LEVEL)
public function __construct($index, $name, $value, $scope = self::SCOPE_PAGE_LEVEL)
{
$this->setIndex($index);
$this->setName($name);
Expand All @@ -60,17 +60,15 @@ public function __construct ($index, $name, $value, $scope = self::SCOPE_PAGE_LE
public function setIndex($index)
{
if (!is_int($index)) {
throw new InvalidArgumentException(
sprintf(
'Index must be of type integer, %s given',
gettype($index)
)
);
}

throw new InvalidArgumentException(sprintf(
'Index must be of type integer, %s given',
gettype($index)
));
}

$this->index = $index;
}

public function getIndex()
{
return $this->index;
Expand Down Expand Up @@ -103,22 +101,19 @@ public function setScope($scope)
self::SCOPE_SESSION,
self::SCOPE_PAGE_LEVEL
);

if (!in_array($scope, $allowed, true)) {
throw new InvalidArgumentException(
sprintf(
'Invalid value given for scope. Acceptable values are: %s.',
implode(', ', $allowed)
)
);
throw new InvalidArgumentException(sprintf(
'Invalid value given for scope. Acceptable values are: %s.',
implode(', ', $allowed)
));
}

$this->scope = $scope;
}

public function getScope()
{
return $this->scope;
}

}
32 changes: 13 additions & 19 deletions src/SlmGoogleAnalytics/Analytics/Ecommerce/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,67 +47,61 @@ class Item
protected $product;
protected $category;

public function __construct ($sku, $price, $quantity, $product = null, $category = null)
public function __construct($sku, $price, $quantity = null, $product = null, $category = null)
{
$this->setSku($sku);
$this->setPrice($price);
$this->setQuantity($quantity);

if (null !== $product) {
$this->setProduct($product);
}

if (null !== $category) {
$this->setCategory($category);
}
$this->setProduct($product);
$this->setCategory($category);
}

public function getSku ()
public function getSku()
{
return $this->sku;
}

public function setSku ($sku)
public function setSku($sku)
{
$this->sku = $sku;
}

public function getProduct ()
public function getProduct()
{
return $this->product;
}

public function setProduct ($product)
public function setProduct($product)
{
$this->product = $product;
}

public function getCategory ()
public function getCategory()
{
return $this->category;
}

public function setCategory ($category)
public function setCategory($category)
{
$this->category = $category;
}

public function getPrice ()
public function getPrice()
{
return $this->price;
}

public function setPrice ($price)
public function setPrice($price)
{
$this->price = $price;
}

public function getQuantity ()
public function getQuantity()
{
return $this->quantity;
}

public function setQuantity ($quantity)
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
Expand Down
Loading

0 comments on commit 2a1838d

Please sign in to comment.