Browse files

[ANALYTICS] Support GET parameter whitelist

  • Loading branch information...
1 parent 3960426 commit 24946dadc808e4c87bad50539e831c6f1de6fc19 @matthewfitz matthewfitz committed Oct 29, 2011
Showing with 27 additions and 6 deletions.
  1. +22 −6 Analytics.php
  2. +3 −0 DependencyInjection/GoogleExtension.php
  3. +2 −0 Resources/config/analytics.xml
View
28 Analytics.php
@@ -20,11 +20,13 @@ class Analytics
private $customVariables = array();
private $pageViewsWithBaseUrl = true;
private $trackers;
+ private $whitelist;
- public function __construct(ContainerInterface $container, array $trackers = array())
+ public function __construct(ContainerInterface $container, array $trackers = array(), array $whitelist = array())
{
$this->container = $container;
$this->trackers = $trackers;
+ $this->whitelist = $whitelist;
}
public function excludeBaseUrl()
@@ -273,21 +275,35 @@ public function getRequest()
}
/**
- * If Custom Page view not set,
- * Then requestUri is used as an alternative
+ * Check and apply base url configuration
+ * If a GET param whitelist is declared,
+ * Then only allow the whitelist
*
* @return string $requestUri
*/
public function getRequestUri()
{
$request = $this->getRequest();
- $requestUri = $request->getRequestUri();
+ $path = $request->getPathInfo();
+
if (!$this->pageViewsWithBaseUrl) {
$baseUrl = $request->getBaseUrl();
if ($baseUrl != '/') {
- return str_replace($baseUrl, '', $requestUri);
+ $uri = str_replace($baseUrl, '', $path);
}
- return $requestUri;
+ }
+
+ $params = $request->query->all();
+ if (!empty($this->whitelist) && !empty($params)) {
+ $whitelist = array_flip($this->whitelist);
+ $params = array_intersect_key($params, $whitelist);
+ }
+
+ $requestUri = $path;
+ $query = http_build_query($params);
+
+ if (isset($query) && '' != trim($query)) {
+ $requestUri .= '?'. $query;
}
return $requestUri;
}
View
3 DependencyInjection/GoogleExtension.php
@@ -65,6 +65,9 @@ private function analyticsLoad(array $configs, ContainerBuilder $container)
if (isset($config['trackers'])) {
$container->setParameter('google.analytics.trackers', $config['trackers']);
}
+ if (isset($config['whitelist'])) {
+ $container->setParameter('google.analytics.whitelist', $config['whitelist']);
+ }
}
}
View
2 Resources/config/analytics.xml
@@ -4,12 +4,14 @@
<parameters>
<parameter key="google.analytics.trackers" type="collection" />
+ <parameter key="google.analytics.whitelist" type="collection" />
</parameters>
<services>
<service id="google.analytics" class="AntiMattr\GoogleBundle\Analytics">
<argument type="service" id="service_container" />
<argument>%google.analytics.trackers%</argument>
+ <argument>%google.analytics.whitelist%</argument>
</service>
<service id="templating.helper.google_analytics" class="AntiMattr\GoogleBundle\Helper\AnalyticsHelper">

0 comments on commit 24946da

Please sign in to comment.