Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 6be10221e29c240c91939fc6b10c34e463382a8d @felixge committed Jul 14, 2010
Showing with 80 additions and 0 deletions.
  1. +32 −0 controllers/components/mixpanel.php
  2. +48 −0 views/helpers/mixpanel.php
@@ -0,0 +1,32 @@
+<?php
+class MixpanelComponent extends Object{
+ public $Controller;
+
+ public $settings = array(
+ 'token' => null,
+ 'properties' => array()
+ );
+
+ public $events = array();
+
+ function startup(&$Controller, $settings = array()) {
+ $this->Controller = &$Controller;
+ $this->settings = array_merge($this->settings, $settings);
+ }
+
+ public function beforeRender() {
+ Configure::write('Mixpanel.events', $this->events);
+ Configure::write('Mixpanel.settings', $this->settings);
+ $this->Controller->helpers[] = 'Mixpanel.Mixpanel';
+ }
+
+ function track($event, $properties = array()) {
+ $this->events[] = compact('event', 'properties');
+ }
+
+ function trackInternal($event, $properties = array()) {
+ $this->events[] = compact('event', 'properties');
+ }
+}
+
+?>
@@ -0,0 +1,48 @@
+<?php
+class MixpanelHelper extends AppHelper{
+ public function embed($inline = true) {
+ $include = <<<JS
+<script type='text/javascript'>
+var mp_protocol = (('https:' == document.location.protocol) ? 'https://' : 'http://');
+document.write(unescape('%3Cscript src="' + mp_protocol + 'api.mixpanel.com/site_media/js/api/mixpanel.js" type="text/javascript"%3E%3C/script%3E'));
+</script>
+<script type='text/javascript'>
+try {
+ var mpmetrics = new MixpanelLib(TOKEN);
+} catch(err) {
+ null_fn = function () {};
+ var mpmetrics = { track: null_fn, track_funnel: null_fn, register: null_fn, register_once: null_fn, register_funnel: null_fn };
+}
+TRACKERS
+</script>
+JS;
+
+ $settings = Configure::read('Mixpanel.settings');
+ $events = Configure::read('Mixpanel.events');
+
+ if (empty($events)) {
+ return '';
+ }
+
+ $trackers = array();
+ foreach ($events as $event) {
+ $properties = $event['properties'];
+ $properties = array_merge($settings['properties'], $properties);
+
+ $trackers[] = sprintf(
+ 'mpmetrics.track(%s, %s);',
+ json_encode($event['event']),
+ (!empty($properties))
+ ? json_encode($properties)
+ : '{}'
+ );
+ }
+
+ return str_replace(
+ array('TOKEN', 'TRACKERS'),
+ array(json_encode($settings['token']), join("\n", $trackers)),
+ $include
+ );
+ }
+}
+?>

0 comments on commit 6be1022

Please sign in to comment.