Permalink
Browse files

Merge pull request #24 from antimattr/feature/analytics-disable-name-…

…prefix-on-tracker

Feature/analytics disable name prefix on tracker
  • Loading branch information...
2 parents c2c9800 + 7485df7 commit e94eaa781266e864a9df5ae3907bf9442b39cd7b @matthewfitz matthewfitz committed Jun 20, 2012
Showing with 123 additions and 49 deletions.
  1. +76 −32 Analytics.php
  2. +8 −0 Helper/AnalyticsHelper.php
  3. +17 −17 Resources/views/Analytics/async.html.twig
  4. +22 −0 Tests/AnalyticsWebTest.php
View
@@ -33,7 +33,7 @@ public function __construct(ContainerInterface $container,
$this->whitelist = $whitelist;
$this->api_key = isset($dashboard['api_key']) ? $dashboard['api_key'] : '';
$this->client_id = isset($dashboard['client_id']) ? $dashboard['client_id'] : '';
- $this->table_id = isset($dashboard['table_id']) ? $dashboard['table_id'] : '';
+ $this->table_id = isset($dashboard['table_id']) ? $dashboard['table_id'] : '';
}
public function excludeBaseUrl()
@@ -46,32 +46,51 @@ public function includeBaseUrl()
$this->pageViewsWithBaseUrl = true;
}
+ private function isValidConfigKey($trackerKey)
+ {
+ if (!array_key_exists($trackerKey, $this->trackers)) {
+ throw new \InvalidArgumentException(sprintf('There is no tracker configuration assigned with the key "%s".', $trackerKey));
+ }
+ return true;
+ }
+
+ private function setTrackerProperty($tracker, $property, $value)
+ {
+ if ($this->isValidConfigKey($tracker)) {
+ $this->trackers[$tracker][$property] = $value;
+ }
+ }
+
+ private function getTrackerProperty($tracker, $property)
+ {
+ if (!$this->isValidConfigKey($tracker)) {
+ return;
+ }
+
+ if (array_key_exists($property, $this->trackers[$tracker])) {
+ return $this->trackers[$tracker][$property];
+ }
+ }
+
/**
* @param string $trackerKey
* @param boolean $allowHash
*/
public function setAllowHash($trackerKey, $allowHash)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['allowHash'] = $allowHash;
+ $this->setTrackerProperty($trackerKey, 'allowHash', $allowHash);
}
/**
* @param string $trackerKey
- * @return boolean $allowHash
+ * @return boolean $allowHash (default:false)
*/
public function getAllowHash($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return false;
- }
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('allowHash', $trackerConfig)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'allowHash'))) {
return false;
}
- return $trackerConfig['allowHash'];
+ return $property;
}
/**
@@ -80,26 +99,58 @@ public function getAllowHash($trackerKey)
*/
public function setAllowLinker($trackerKey, $allowLinker)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['allowLinker'] = $allowLinker;
+ $this->setTrackerProperty($trackerKey, 'allowLinker', $allowLinker);
}
/**
* @param string $trackerKey
- * @return boolean $allowLinker
+ * @return boolean $allowLinker (default:true)
*/
public function getAllowLinker($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'allowLinker'))) {
return true;
}
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('allowLinker', $trackerConfig)) {
+ return $property;
+ }
+
+ /**
+ * @param string $trackerKey
+ * @param boolean $includeNamePrefix
+ */
+ public function setIncludeNamePrefix($trackerKey, $includeNamePrefix)
+ {
+ $this->setTrackerProperty($trackerKey, 'includeNamePrefix', $includeNamePrefix);
+ }
+
+ /**
+ * @param string $trackerKey
+ * @return boolean $includeNamePrefix (default:true)
+ */
+ public function getIncludeNamePrefix($trackerKey)
+ {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'includeNamePrefix'))) {
return true;
}
- return $trackerConfig['allowLinker'];
+ return $property;
+ }
+
+ /**
+ * @param string $trackerKey
+ * @param boolean $name
+ */
+ public function setTrackerName($trackerKey, $name)
+ {
+ $this->setTrackerProperty($trackerKey, 'name', $name);
+ }
+
+ /**
+ * @param string $trackerKey
+ * @return string $name
+ */
+ public function getTrackerName($trackerKey)
+ {
+ return $this->getTrackerProperty($trackerKey, 'name');
}
/**
@@ -108,26 +159,19 @@ public function getAllowLinker($trackerKey)
*/
public function setTrackPageLoadTime($trackerKey, $trackPageLoadTime)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return;
- }
- $this->trackers[$trackerKey]['trackPageLoadTime'] = $trackPageLoadTime;
+ $this->setTrackerProperty($trackerKey, 'trackPageLoadTime', $trackPageLoadTime);
}
/**
* @param string $trackerKey
- * @return boolean $trackPageLoadTime
+ * @return boolean $trackPageLoadTime (default:false)
*/
public function getTrackPageLoadTime($trackerKey)
{
- if (!array_key_exists($trackerKey, $this->trackers)) {
- return false;
- }
- $trackerConfig = $this->trackers[$trackerKey];
- if (!array_key_exists('trackPageLoadTime', $trackerConfig)) {
+ if (null === ($property = $this->getTrackerProperty($trackerKey, 'trackPageLoadTime'))) {
return false;
}
- return $trackerConfig['trackPageLoadTime'];
+ return $property;
}
/**
@@ -31,6 +31,14 @@ public function getAllowLinker($trackerKey)
return $this->analytics->getAllowLinker($trackerKey);
}
+ public function getTrackerName($trackerKey)
+ {
+ if ($this->analytics->getIncludeNamePrefix($trackerKey)) {
+ return $this->analytics->getTrackerName($trackerKey).'.';
+ }
+ return "";
+ }
+
public function getTrackPageLoadTime($trackerKey)
{
return $this->analytics->getTrackPageLoadTime($trackerKey);
@@ -14,7 +14,7 @@ AntiMattr.GoogleAnalytics.onGatLoad = function(callback) {
};
AntiMattr.GoogleAnalytics.notifyEvent = function(tracker, category, action, label, value) {
- var event = [tracker + '._trackEvent', category, action];
+ var event = [tracker + '_trackEvent', category, action];
if (typeof label != 'undefined') {
event.push(label);
if (typeof value != 'undefined') {
@@ -27,53 +27,53 @@ AntiMattr.GoogleAnalytics.notifyEvent = function(tracker, category, action, labe
if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
{% for key, tracker in google_analytics.trackers %}
- _gaq.push(['{{ tracker.name }}._setAccount', '{{ tracker.accountId }}']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setAccount', '{{ tracker.accountId }}']);
{% if tracker.domain %}
- _gaq.push(['{{ tracker.name }}._setDomainName', '{{ tracker.domain }}']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setDomainName', '{{ tracker.domain }}']);
{% if google_analytics.getAllowLinker(key) %}
- _gaq.push(['{{ tracker.name }}._setAllowLinker', true ]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setAllowLinker', true ]);
{% else %}
- _gaq.push(['{{ tracker.name }}._setAllowLinker', false ]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setAllowLinker', false ]);
{% endif %}
{% if google_analytics.getAllowHash(key) %}
- _gaq.push(['{{ tracker.name }}._setAllowHash', true ]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setAllowHash', true ]);
{% else %}
- _gaq.push(['{{ tracker.name }}._setAllowHash', false ]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setAllowHash', false ]);
{% endif %}
{% endif %}
{% if google_analytics.hasCustomVariables %}
{% for customVariable in google_analytics.customVariables %}
- _gaq.push(['{{ tracker.name }}._setCustomVar', {{ customVariable.index }}, '{{ customVariable.name }}', '{{ customVariable.value }}', {{ customVariable.scope }}]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_setCustomVar', {{ customVariable.index }}, '{{ customVariable.name }}', '{{ customVariable.value }}', {{ customVariable.scope }}]);
{% endfor %}
{% endif %}
{% if google_analytics.hasPageViewQueue %}
{% for pageView in google_analytics.pageViewQueue %}
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ pageView }}']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackPageview', '{{ pageView }}']);
{% endfor %}
{% endif %}
{% if google_analytics.hasCustomPageView %}
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getCustomPageView() | raw }}']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackPageview', '{{ google_analytics.getCustomPageView() | raw }}']);
{% else %}
if (window.location.hash) {
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() | raw }}'+window.location.hash]);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackPageview', '{{ google_analytics.getRequestUri() | raw }}'+window.location.hash]);
} else {
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() | raw }}']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackPageview', '{{ google_analytics.getRequestUri() | raw }}']);
}
{% endif %}
{% if google_analytics.getTrackPageLoadTime(key) %}
- _gaq.push(['{{ tracker.name }}._trackPageLoadTime']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackPageLoadTime']);
{% endif %}
{% if google_analytics.isTransactionValid %}
{% set transaction = google_analytics.transaction %}
- _gaq.push(['{{ tracker.name }}._addTrans',
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_addTrans',
'{{ transaction.orderNumber | escape('js') }}', // order number required
'{{ transaction.affiliation | escape('js') }}', // affiliation optional
'{{ transaction.total | escape('js') }}', // total optional
@@ -86,7 +86,7 @@ if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
{% if google_analytics.hasItems %}
{% for item in google_analytics.items %}
- _gaq.push(['{{ tracker.name }}._addItem',
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_addItem',
'{{ item.orderNumber | escape('js') }}', // order number required
'{{ item.sku | escape('js') }}', // sku required
'{{ item.name | escape('js') }}', // name optional
@@ -97,14 +97,14 @@ if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
{% endfor %}
{% endif %}
- _gaq.push(['{{ tracker.name }}._trackTrans']);
+ _gaq.push(['{{ google_analytics.trackerName(key) }}_trackTrans']);
{% endif %}
{% if google_analytics.hasEventQueue %}
AntiMattr.GoogleAnalytics.onGatLoad(function() {
{% for event in google_analytics.eventQueue %}
- AntiMattr.GoogleAnalytics.notifyEvent('{{ tracker.name }}', '{{ event.category }}','{{ event.action }}','{{ event.label }}','{{ event.value }}');
+ AntiMattr.GoogleAnalytics.notifyEvent('{{ google_analytics.trackerName(key) }}', '{{ event.category }}','{{ event.action }}','{{ event.label }}','{{ event.value }}');
{% endfor %}
});
{% endif %}
View
@@ -38,6 +38,16 @@ public function testConstructor()
$this->assertEquals(1, count($this->analytics->getTrackers()));
$this->assertTrue($this->analytics->getAllowLinker('default'));
$this->assertFalse($this->analytics->getAllowHash('default'));
+ $this->assertTrue($this->analytics->getIncludeNamePrefix('default'));
+ $this->assertTrue(0 < strlen($this->analytics->getTrackerName('default')));
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testExpectedInvalidArgumentException()
+ {
+ $this->analytics->getAllowLinker('not-a-tracker');
}
public function testSetGetCustomPageView()
@@ -184,6 +194,18 @@ public function testSetAllowLinker()
$this->assertFalse($this->analytics->getAllowLinker('default'));
}
+ public function testSetIncludeNamePrefix()
+ {
+ $this->analytics->setIncludeNamePrefix('default', false);
+ $this->assertFalse($this->analytics->getIncludeNamePrefix('default'));
+ }
+
+ public function testSetTrackerName()
+ {
+ $this->analytics->setTrackerName('default', 'a-different-name');
+ $this->assertEquals('a-different-name', $this->analytics->getTrackerName('default'));
+ }
+
public function testSetTrackPageLoadTime()
{
$this->analytics->setTrackPageLoadTime('default', true);

0 comments on commit e94eaa7

Please sign in to comment.