Permalink
Browse files

Event tracking support

  • Loading branch information...
1 parent 17ffac4 commit 371b1584c015f4dd54ba7a22797c49080a5a39ea @matthewfitz matthewfitz committed Oct 25, 2011
View
@@ -12,14 +12,14 @@ class Event
private $label;
private $value;
- public function __contruct($category, $action, $label = null, $value = null)
+ public function __construct($category, $action, $label = null, $value = null)
{
$this->action = $action;
$this->category = $category;
$this->label = $label;
- $this->value = $value;
+ $this->value = $value;
}
-
+
/**
* @return string $action
*/
View
@@ -50,16 +50,6 @@ public function getCustomVariables()
return $this->analytics->getCustomVariables();
}
- public function getEventFunctionName($eventName)
- {
- return 'trackEvent'.ucfirst($eventName);
- }
-
- public function addEvent($category, $action, $label = null, $value = null)
- {
- $this->analytics->enqueueEvent(new Event($category, $action, $label, $value));
- }
-
public function hasEventQueue()
{
return $this->analytics->hasEventQueue();
@@ -1,101 +1,103 @@
<script type="text/javascript">
- if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
+if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
- {% for key, tracker in google_analytics.trackers %}
+{% for key, tracker in google_analytics.trackers %}
_gaq.push(function() {
var {{ tracker.name }} = _gaq._createAsyncTracker('{{ tracker.accountId }}','{{ tracker.name }}');
});
+
{% if tracker.domain %}
- _gaq.push(['{{ tracker.name }}._setDomainName', '{{ tracker.domain }}']);
+ _gaq.push(['{{ tracker.name }}._setDomainName', '{{ tracker.domain }}']);
- {% if google_analytics.getAllowLinker(key) %}
- _gaq.push(['{{ tracker.name }}._setAllowLinker', true ]);
- {% else %}
- _gaq.push(['{{ tracker.name }}._setAllowLinker', false ]);
- {% endif %}
+ {% if google_analytics.getAllowLinker(key) %}
+ _gaq.push(['{{ tracker.name }}._setAllowLinker', true ]);
+ {% else %}
+ _gaq.push(['{{ tracker.name }}._setAllowLinker', false ]);
+ {% endif %}
- {% if google_analytics.getAllowHash(key) %}
- _gaq.push(['{{ tracker.name }}._setAllowHash', true ]);
- {% else %}
- _gaq.push(['{{ tracker.name }}._setAllowHash', false ]);
- {% endif %}
+ {% if google_analytics.getAllowHash(key) %}
+ _gaq.push(['{{ tracker.name }}._setAllowHash', true ]);
+ {% else %}
+ _gaq.push(['{{ tracker.name }}._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 }}]);
- {% endfor %}
+ {% for customVariable in google_analytics.customVariables %}
+ _gaq.push(['{{ tracker.name }}._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 }}']);
- {% endfor %}
+ {% for pageView in google_analytics.pageViewQueue %}
+ _gaq.push(['{{ tracker.name }}._trackPageview', '{{ pageView }}']);
+ {% endfor %}
{% endif %}
{% if google_analytics.hasCustomPageView %}
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getCustomPageView() }}']);
+ _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getCustomPageView() }}']);
{% else %}
- if (window.location.hash) {
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() }}'+window.location.hash]);
- } else {
- _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() }}']);
- }
+ if (window.location.hash) {
+ _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() }}'+window.location.hash]);
+ } else {
+ _gaq.push(['{{ tracker.name }}._trackPageview', '{{ google_analytics.getRequestUri() }}']);
+ }
{% endif %}
{% if google_analytics.getTrackPageLoadTime(key) %}
- _gaq.push(['{{ tracker.name }}._trackPageLoadTime']);
+ _gaq.push(['{{ tracker.name }}._trackPageLoadTime']);
{% endif %}
{% if google_analytics.isTransactionValid %}
- {% set transaction = google_analytics.transaction %}
- _gaq.push(['{{ tracker.name }}._addTrans',
- '{{ transaction.orderNumber | escape('js') }}', // order number required
- '{{ transaction.affiliation | escape('js') }}', // affiliation optional
- '{{ transaction.total | escape('js') }}', // total optional
- '{{ transaction.tax | escape('js') }}', // tax optional
- '{{ transaction.shipping | escape('js') }}', // shipping optional
- '{{ transaction.city | escape('js') }}', // city optional
- '{{ transaction.state | escape('js') }}', // state optional
- '{{ transaction.country | escape('js') }}' // country optional
- ]);
+ {% set transaction = google_analytics.transaction %}
+ _gaq.push(['{{ tracker.name }}._addTrans',
+ '{{ transaction.orderNumber | escape('js') }}', // order number required
+ '{{ transaction.affiliation | escape('js') }}', // affiliation optional
+ '{{ transaction.total | escape('js') }}', // total optional
+ '{{ transaction.tax | escape('js') }}', // tax optional
+ '{{ transaction.shipping | escape('js') }}', // shipping optional
+ '{{ transaction.city | escape('js') }}', // city optional
+ '{{ transaction.state | escape('js') }}', // state optional
+ '{{ transaction.country | escape('js') }}' // country optional
+ ]);
- {% if google_analytics.hasItems %}
- {% for item in google_analytics.items %}
- _gaq.push(['{{ tracker.name }}._addItem',
- '{{ item.orderNumber | escape('js') }}', // order number required
- '{{ item.sku | escape('js') }}', // sku required
- '{{ item.name | escape('js') }}', // name optional
- '{{ item.category | escape('js') }}', // category optional
- '{{ item.price | escape('js') }}', // price required
- '{{ item.quantity | escape('js') }}' // quantity required
- ]);
- {% endfor %}
- {% endif %}
+ {% if google_analytics.hasItems %}
+ {% for item in google_analytics.items %}
+ _gaq.push(['{{ tracker.name }}._addItem',
+ '{{ item.orderNumber | escape('js') }}', // order number required
+ '{{ item.sku | escape('js') }}', // sku required
+ '{{ item.name | escape('js') }}', // name optional
+ '{{ item.category | escape('js') }}', // category optional
+ '{{ item.price | escape('js') }}', // price required
+ '{{ item.quantity | escape('js') }}' // quantity required
+ ]);
+ {% endfor %}
+ {% endif %}
- _gaq.push(['{{ tracker.name }}._trackTrans']);
+ _gaq.push(['{{ tracker.name }}._trackTrans']);
{% endif %}
- {% endfor %}
{% if google_analytics.hasEventQueue %}
- {% for event in google_analytics.eventQueue %}
- _gaq.push(['{{ tracker.name }}._trackEvent'
- ,'{{ event.category }}' // category required
- ,'{{ event.action }}' // action required
- {% if event.label %}
- ,'{{ event.label }}' // label optional
- {% endif %}
- {% if event.label and event.value %}
- ,'{{ event.value }}' // value optional but dependent on label
- {% endif %}
- ]);
- {% endfor %}
+ {% for event in google_analytics.eventQueue %}
+ _gaq.push(['{{ tracker.name }}._trackEvent'
+ ,'{{ event.category }}' // category required
+ ,'{{ event.action }}' // action required
+ {% if event.label %}
+ ,'{{ event.label }}' // label optional
+ {% endif %}
+ {% if event.label and event.value %}
+ ,'{{ event.value }}' // value optional but dependent on label
+ {% endif %}
+ ]);
+ {% endfor %}
{% endif %}
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
+{% endfor %}
+
+(function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+})();
</script>
@@ -1,18 +0,0 @@
-{#
- # @param string eventName
- #}
-{% set eventFunctionName = google_analytics.getEventFunctionName(eventName) %}
-<script type="text/javascript">
-if ( _gaq == null || typeof(_gaq) != 'array') { var _gaq = window._gaq = []; }
-
-{{ eventFunctionName }} = function(category, action, label, value) {
- {% for key, tracker in google_analytics.trackers %}
- _gaq.push(['{{ tracker.name }}._trackEvent'
- , category // category required
- , action // action required
- , label // label optional
- , value // value optional but dependent on label
- ]);
- {% endfor %}
-}
-</script>
@@ -0,0 +1,44 @@
+<?php
+
+namespace AntiMattr\GoogleBundle\Tests\Analytics;
+
+use AntiMattr\GoogleBundle\Analytics\Event;
+
+class EventTest extends \PHPUnit_Framework_TestCase
+{
+ private $event;
+
+ public function setUp()
+ {
+ parent::setup();
+ $this->category = 'Test category';
+ $this->action = 'Test action';
+ $this->label = 'Test label';
+ $this->value = 'Test value';
+ $this->event = new Event($this->category, $this->action);
+ }
+
+ public function tearDown()
+ {
+ $this->event = null;
+ $this->value = null;
+ $this->label = null;
+ $this->action = null;
+ $this->category = null;
+ parent::tearDown();
+ }
+
+ public function testConstructor()
+ {
+ $this->assertEquals($this->category, $this->event->getCategory());
+ $this->assertEquals($this->action, $this->event->getAction());
+ $this->assertNull($this->event->getLabel());
+ $this->assertNull($this->event->getValue());
+
+ $label = 'Test label';
+ $value = 'Test value';
+ $event = new Event($this->category, $this->action, $label, $value);
+ $this->assertEquals($label, $event->getLabel());
+ $this->assertEquals($value, $event->getValue());
+ }
+}
View
@@ -6,6 +6,7 @@
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use AntiMattr\GoogleBundle\Analytics;
+use AntiMattr\GoogleBundle\Analytics\Event;
use AntiMattr\GoogleBundle\Analytics\Item;
use AntiMattr\GoogleBundle\Analytics\Transaction;
@@ -78,6 +79,41 @@ public function providePageViewsForQueue()
);
}
+ /**
+ * @dataProvider provideEventsForQueue
+ */
+ public function testEnqueueEvent($eventData, $count)
+ {
+ foreach ($eventData as $data) {
+ $event = new Event($data['category'], $data['action']);
+ $this->analytics->enqueueEvent($event);
+ }
+
+ $this->assertTrue($this->analytics->hasEventQueue());
+ $this->assertEquals($count, count($this->analytics->getEventQueue()));
+ }
+
+ public function provideEventsForQueue()
+ {
+ return array(
+ array(
+ array(
+ array('category' => 'Category A', 'action' => 'Action A'),
+ array('category' => 'Category B', 'action' => 'Action B'),
+ array('category' => 'Category C', 'action' => 'Action C')
+ ),
+ 3
+ ),
+ array(
+ array(
+ array('category' => 'Category D', 'action' => 'Action D'),
+ array('category' => 'Category E', 'action' => 'Action E'),
+ ),
+ 2
+ )
+ );
+ }
+
public function testSetGetTransaction()
{
$this->assertFalse($this->analytics->isTransactionValid());

0 comments on commit 371b158

Please sign in to comment.