From a8fe4915ad3829abd7818fa86faaa468b6b42539 Mon Sep 17 00:00:00 2001 From: rokde Date: Fri, 9 May 2014 15:07:33 +0200 Subject: [PATCH] Adding measurement api support Adding measurement api to enable tracking without having javascript - use case: track newsletter openings with a tracking image url --- README.md | 29 ++++ composer.json | 2 +- .../Contracts/AnalyticsProviderInterface.php | 18 +++ src/Ipunkt/LaravelAnalytics/Data/Campaign.php | 108 +++++++++++++++ src/Ipunkt/LaravelAnalytics/Data/Event.php | 129 ++++++++++++++++++ .../Providers/GoogleAnalytics.php | 65 +++++++++ .../Providers/NoAnalytics.php | 21 +++ 7 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 src/Ipunkt/LaravelAnalytics/Data/Campaign.php create mode 100644 src/Ipunkt/LaravelAnalytics/Data/Event.php diff --git a/README.md b/README.md index a8ef8fa..934d859 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,17 @@ There is a builtin provider called `NoAnalytics`. This is for testing environmen not have to rewrite your code, simple select this `provider` in `analytics` configuration for your special environment configurations. +### Track a measurement without having javacsript + +1. Log in to Google Analytics and create custom definition. There you create a custom metrics. + For example: Email opens, Integer type, min: 0 and max: 1 + This will be available as `metric1`. + +2. Within your mail template (or page template) you have to create a tracking image + + `` + +3. That's it ## API Documentation @@ -145,3 +156,21 @@ Disabling the auto tracking, overriding the configuration setting `auto_track`. */ public function disableAutoTracking(); +### Analytics::trackMeasurementUrl() + +Sometimes you have to track measurements, e.g. opening an email newsletter. There you have no javascript at all. + + /** + * assembles an url for tracking measurement without javascript + * + * e.g. for tracking email open events within a newsletter + * + * @param string $metricName + * @param mixed $metricValue + * @param \Ipunkt\LaravelAnalytics\Data\Event $event + * @param \Ipunkt\LaravelAnalytics\Data\Campaign $campaign + * @param string|null $clientId + * @param array $params + * @return string + */ + public function trackMeasurementUrl($metricName, $metricValue, Event $event, Campaign $campaign, $clientId = null, array $params = array()); diff --git a/composer.json b/composer.json index 2a72543..8c725fb 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "ipunkt/laravel-analytics", - "version": "1.0.0", + "version": "1.0.1", "type": "library", "description": "Analytics tracking for laravel 4.x", "keywords": ["analytics", "google analytics", "laravel", "statistics", "javascript", "php"], diff --git a/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php b/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php index 1c28489..e7fa8de 100644 --- a/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php +++ b/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php @@ -9,6 +9,9 @@ namespace Ipunkt\LaravelAnalytics\Contracts; +use Ipunkt\LaravelAnalytics\Data\Campaign; +use Ipunkt\LaravelAnalytics\Data\Event; + interface AnalyticsProviderInterface { /** @@ -60,4 +63,19 @@ public function enableAutoTracking(); * @return void */ public function disableAutoTracking(); + + /** + * assembles an url for tracking measurement without javascript + * + * e.g. for tracking email open events within a newsletter + * + * @param string $metricName + * @param mixed $metricValue + * @param \Ipunkt\LaravelAnalytics\Data\Event $event + * @param \Ipunkt\LaravelAnalytics\Data\Campaign $campaign + * @param string|null $clientId + * @param array $params + * @return string + */ + public function trackMeasurementUrl($metricName, $metricValue, Event $event, Campaign $campaign, $clientId = null, array $params = array()); } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Data/Campaign.php b/src/Ipunkt/LaravelAnalytics/Data/Campaign.php new file mode 100644 index 0000000..5508d96 --- /dev/null +++ b/src/Ipunkt/LaravelAnalytics/Data/Campaign.php @@ -0,0 +1,108 @@ +name = $name; + } + + /** + * set medium + * + * @param string $medium + * + * @return Campaign + */ + public function setMedium($medium) + { + $this->medium = $medium; + + return $this; + } + + /** + * @return string + */ + public function getMedium() + { + return $this->medium; + } + + /** + * set name + * + * @param string $name + * + * @return Campaign + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * set source + * + * @param string $source + * + * @return Campaign + */ + public function setSource($source) + { + $this->source = $source; + + return $this; + } + + /** + * @return string + */ + public function getSource() + { + return $this->source; + } +} \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Data/Event.php b/src/Ipunkt/LaravelAnalytics/Data/Event.php new file mode 100644 index 0000000..35b3566 --- /dev/null +++ b/src/Ipunkt/LaravelAnalytics/Data/Event.php @@ -0,0 +1,129 @@ +action = $action; + + return $this; + } + + /** + * @return string + */ + public function getAction() + { + return $this->action; + } + + /** + * set category + * + * @param string $category + * + * @return Event + */ + public function setCategory($category) + { + $this->category = $category; + + return $this; + } + + /** + * @return string + */ + public function getCategory() + { + return $this->category; + } + + /** + * set hitType + * + * @param string $hitType + * + * @return Event + */ + public function setHitType($hitType) + { + $this->hitType = $hitType; + + return $this; + } + + /** + * @return string + */ + public function getHitType() + { + return $this->hitType; + } + + /** + * set label + * + * @param string $label + * + * @return Event + */ + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + /** + * @return string + */ + public function getLabel() + { + return $this->label; + } +} \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php b/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php index a1c4fad..cefc0c2 100644 --- a/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php +++ b/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php @@ -10,6 +10,8 @@ use Ipunkt\LaravelAnalytics\Contracts\AnalyticsProviderInterface; +use Ipunkt\LaravelAnalytics\Data\Campaign; +use Ipunkt\LaravelAnalytics\Data\Event; use Ipunkt\LaravelAnalytics\TrackingBag; use App; @@ -221,4 +223,67 @@ protected function _getJavascriptTemplateBlockEnd() { return ''; } + + /** + * assembles an url for tracking measurement without javascript + * + * e.g. for tracking email open events within a newsletter + * + * @param string $metricName + * @param mixed $metricValue + * @param \Ipunkt\LaravelAnalytics\Data\Event $event + * @param \Ipunkt\LaravelAnalytics\Data\Campaign $campaign + * @param string|null $clientId + * @param array $params + * @return string + * + * @experimental + */ + public function trackMeasurementUrl($metricName, $metricValue, Event $event, Campaign $campaign, $clientId = null, array $params = array()) + { + $uniqueId = ($clientId !== null) ? $clientId : uniqid('track_'); + + if ($event->getLabel() === '') + { + $event->setLabel($uniqueId); + } + + if ($campaign->getName() === '') + { + $campaign->setName('Campaign ' . date('Y-m-d')); + } + + $defaults = [ + 'url' => 'http://www.google-analytics.com/collect?', + 'params' => [ + 'v' => 1, // protocol version + 'tid' => $this->trackingId, // tracking id + 'cid' => $uniqueId, // client id + 't' => $event->getHitType(), + 'ec' => $event->getCategory(), + 'ea' => $event->getAction(), + 'el' => $event->getLabel(), + 'cs' => $campaign->getSource(), + 'cm' => $campaign->getMedium(), + 'cn' => $campaign->getName(), + $metricName => $metricValue, // metric data + ], + ]; + + $url = isset($params['url']) ? $params['url'] : $defaults['url']; + $url = rtrim($url, '?') . '?'; + + if (isset($params['url'])) + unset($params['url']); + + $params = array_merge($defaults['params'], $params); + $queryParams = []; + foreach ($params as $key => $value) + { + if (!empty($value)) + $queryParams[] = sprintf('%s=%s', $key, $value); + } + + return $url . implode('&', $queryParams); + } } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php b/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php index 6e40d79..4884b7f 100644 --- a/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php +++ b/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php @@ -10,6 +10,8 @@ use Ipunkt\LaravelAnalytics\Contracts\AnalyticsProviderInterface; +use Ipunkt\LaravelAnalytics\Data\Campaign; +use Ipunkt\LaravelAnalytics\Data\Event; class NoAnalytics implements AnalyticsProviderInterface { @@ -81,4 +83,23 @@ public function disableAutoTracking() } + /** + * assembles an url for tracking measurement without javascript + * + * e.g. for tracking email open events within a newsletter + * + * @param string $metricName + * @param mixed $metricValue + * @param \Ipunkt\LaravelAnalytics\Data\Event $event + * @param \Ipunkt\LaravelAnalytics\Data\Campaign $campaign + * @param string|null $clientId + * @param array $params + * @return string + */ + public function trackMeasurementUrl($metricName, $metricValue, Event $event, Campaign $campaign, $clientId = null, array $params = array()) + { + return ''; + } + + } \ No newline at end of file