Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from rokde/master
Browse files Browse the repository at this point in the history
Measurement API supported
  • Loading branch information
Robert Kummer committed May 9, 2014
2 parents 7df68de + 38449c5 commit 6064c98
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 1 deletion.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

`<img src="{{ Analytics::trackMeasurementUrl('metric1', '1', new Event, new Campaign, md5($user)) }}" width="1" height="1" style="background-color: transparent; border: 0 none;" />`

3. That's it

## API Documentation

Expand Down Expand Up @@ -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());
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ipunkt/laravel-analytics",
"version": "1.0.3",
"version": "1.0.4",
"type": "library",
"description": "Analytics tracking for laravel 4.x",
"keywords": ["analytics", "google analytics", "laravel", "statistics", "javascript", "php"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
namespace Ipunkt\LaravelAnalytics\Contracts;


use Ipunkt\LaravelAnalytics\Data\Campaign;
use Ipunkt\LaravelAnalytics\Data\Event;

interface AnalyticsProviderInterface {

/**
Expand Down Expand Up @@ -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());
}
108 changes: 108 additions & 0 deletions src/Ipunkt/LaravelAnalytics/Data/Campaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* laravel-analytics
*
* @author rok
* @since 09.05.14
*/

namespace Ipunkt\LaravelAnalytics\Data;


class Campaign {

/**
* campaign source
*
* @var string
*/
private $source = 'newsletter';

/**
* campaign medium
*
* @var string
*/
private $medium = 'email';

/**
* campaign name
*
* @var string
*/
private $name;

/**
* @param string $name
*/
public function __construct($name = '')
{
$this->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;
}
}
129 changes: 129 additions & 0 deletions src/Ipunkt/LaravelAnalytics/Data/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* laravel-analytics
*
* @author rok
* @since 09.05.14
*/

namespace Ipunkt\LaravelAnalytics\Data;


class Event {

/**
* event category
*
* @var string
*/
private $category = 'email';

/**
* event action
*
* @var string
*/
private $action = 'open';

/**
* event label
*
* @var string
*/
private $label;

/**
* hit type
*
* @var string
*/
private $hitType = 'event';

/**
* set action
*
* @param string $action
*
* @return Event
*/
public function setAction($action)
{
$this->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;
}
}
65 changes: 65 additions & 0 deletions src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -221,4 +223,67 @@ protected function _getJavascriptTemplateBlockEnd()
{
return '</script>';
}

/**
* 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);
}
}
Loading

0 comments on commit 6064c98

Please sign in to comment.