diff --git a/src/Ipunkt/LaravelAnalytics/AnalyticsFacade.php b/src/Ipunkt/LaravelAnalytics/AnalyticsFacade.php index b4c590e..b338fbc 100644 --- a/src/Ipunkt/LaravelAnalytics/AnalyticsFacade.php +++ b/src/Ipunkt/LaravelAnalytics/AnalyticsFacade.php @@ -1,20 +1,23 @@ -isLaravel4()) { - $this->package('ipunkt/laravel-analytics'); - - return; - } - - $config = realpath(__DIR__ . '/../../config/analytics.php'); - - $this->mergeConfigFrom($config, 'analytics'); - - $this->publishes([ - $config => config_path('analytics.php'), - ]); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $packageNamespace = $this->isLaravel4() ? 'laravel-analytics::' : ''; - - $this->app->bind('analytics', function () use ($packageNamespace) { - - // get analytics provider name - $provider = Config::get($packageNamespace . 'analytics.provider'); - - // make it a class - $providerClass = 'Ipunkt\LaravelAnalytics\Providers\\' . $provider; - - // getting the config - $providerConfig = []; - if (Config::has($packageNamespace . 'analytics.configurations.' . $provider)) { - $providerConfig = Config::get($packageNamespace . 'analytics.configurations.' . $provider); - } - - // return an instance - return new $providerClass($providerConfig); - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array(); - } - - /** - * are we on laravel 4 - * - * @return bool - */ - private function isLaravel4() - { - return version_compare(\Illuminate\Foundation\Application::VERSION, '5', '<'); - } + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + if ($this->isLaravel4()) { + $this->package('ipunkt/laravel-analytics'); + + return; + } + + $config = realpath(__DIR__ . '/../../config/analytics.php'); + + $this->mergeConfigFrom($config, 'analytics'); + + $this->publishes([ + $config => config_path('analytics.php'), + ]); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $packageNamespace = $this->isLaravel4() ? 'laravel-analytics::' : ''; + + $this->app->bind('analytics', function () use ($packageNamespace) { + + // get analytics provider name + $provider = Config::get($packageNamespace . 'analytics.provider'); + + // make it a class + $providerClass = 'Ipunkt\LaravelAnalytics\Providers\\' . $provider; + + // getting the config + $providerConfig = []; + if (Config::has($packageNamespace . 'analytics.configurations.' . $provider)) { + $providerConfig = Config::get($packageNamespace . 'analytics.configurations.' . $provider); + } + + // return an instance + return new $providerClass($providerConfig); + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return []; + } + + /** + * are we on laravel 4 + * + * @return bool + */ + private function isLaravel4() + { + return version_compare(\Illuminate\Foundation\Application::VERSION, '5', '<'); + } } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php b/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php index e1e2255..04883d4 100644 --- a/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php +++ b/src/Ipunkt/LaravelAnalytics/Contracts/AnalyticsProviderInterface.php @@ -1,4 +1,6 @@ -nonInteraction(true)->render(); + * getting: if ($this->nonInteraction()) echo 'non-interaction set'; + * + * @param boolean|null $value + * + * @return bool|AnalyticsProviderInterface + */ + public function nonInteraction($value = null); - /** - * 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()); + /** + * sets an user id for user tracking + * + * @param string $userId + * + * @return AnalyticsProviderInterface + * + * @see https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id + */ + public function setUserId($userId); - /** - * sets or gets nonInteraction - * - * setting: $this->nonInteraction(true)->render(); - * getting: if ($this->nonInteraction()) echo 'non-interaction set'; - * - * @param boolean|null $value - * - * @return bool|AnalyticsProviderInterface - */ - public function nonInteraction($value = null); + /** + * unsets a possible given user id + * + * @return AnalyticsProviderInterface + */ + public function unsetUserId(); } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Contracts/TrackingBagInterface.php b/src/Ipunkt/LaravelAnalytics/Contracts/TrackingBagInterface.php index f765332..1bc2377 100644 --- a/src/Ipunkt/LaravelAnalytics/Contracts/TrackingBagInterface.php +++ b/src/Ipunkt/LaravelAnalytics/Contracts/TrackingBagInterface.php @@ -1,24 +1,27 @@ -name = $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; + /** + * set medium + * + * @param string $medium + * + * @return Campaign + */ + public function setMedium($medium) + { + $this->medium = $medium; - return $this; - } + return $this; + } - /** - * @return string - */ - public function getMedium() - { - return $this->medium; - } + /** + * @return string + */ + public function getMedium() + { + return $this->medium; + } - /** - * set name - * - * @param string $name - * - * @return Campaign - */ - public function setName($name) - { - $this->name = $name; + /** + * set name + * + * @param string $name + * + * @return Campaign + */ + public function setName($name) + { + $this->name = $name; - return $this; - } + return $this; + } - /** - * @return string - */ - public function getName() - { - return $this->name; - } + /** + * @return string + */ + public function getName() + { + return $this->name; + } - /** - * set source - * - * @param string $source - * - * @return Campaign - */ - public function setSource($source) - { - $this->source = $source; + /** + * set source + * + * @param string $source + * + * @return Campaign + */ + public function setSource($source) + { + $this->source = $source; - return $this; - } + return $this; + } - /** - * @return string - */ - public function getSource() - { - return $this->source; - } + /** + * @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 index f89bdce..bb48401 100644 --- a/src/Ipunkt/LaravelAnalytics/Data/Event.php +++ b/src/Ipunkt/LaravelAnalytics/Data/Event.php @@ -1,124 +1,135 @@ -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; - } + /** + * 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; + } + + /** + * returns action + * + * @return string + */ + public function getAction() + { + return $this->action; + } + + /** + * set category + * + * @param string $category + * + * @return Event + */ + public function setCategory($category) + { + $this->category = $category; + + return $this; + } + + /** + * returns category + * + * @return string + */ + public function getCategory() + { + return $this->category; + } + + /** + * sets hit type + * + * @param string $hitType + * + * @return Event + */ + public function setHitType($hitType) + { + $this->hitType = $hitType; + + return $this; + } + + /** + * returns hit type + * + * @return string + */ + public function getHitType() + { + return $this->hitType; + } + + /** + * sets label + * + * @param string $label + * + * @return Event + */ + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + /** + * returns label + * + * @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 06dacf0..a2f4459 100644 --- a/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php +++ b/src/Ipunkt/LaravelAnalytics/Providers/GoogleAnalytics.php @@ -1,4 +1,6 @@ -trackingId = array_get($options, 'tracking_id'); - $this->trackingDomain = array_get($options, 'tracking_domain', 'auto'); - $this->displayFeatures = array_get($options, 'display_features', false); - $this->anonymizeIp = array_get($options, 'anonymize_ip', false); - $this->autoTrack = array_get($options, 'auto_track', false); - $this->debug = array_get($options, 'debug', false); - - if ($this->trackingId === null) { - throw new InvalidArgumentException('Argument tracking_id can not be null'); - } - - $this->trackingBag = new TrackingBag; - } - - /** - * track an page view - * - * @param null|string $page - * @param null|string $title - * @param null|string $hittype - * - * @return void - */ - public function trackPage($page = null, $title = null, $hittype = null) - { - $allowedHitTypes = ['pageview', 'appview', 'event', 'transaction', 'item', 'social', 'exception', 'timing']; - if ($hittype === null) { - $hittype = $allowedHitTypes[0]; - } - - if ( ! in_array($hittype, $allowedHitTypes)) { - return; - } - - $trackingCode = "ga('send', 'pageview');"; - - if ($page !== null || $title !== null || $hittype !== null) { - $page = ($page === null) ? "window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search" : "'{$page}'"; - $title = ($title === null) ? "document.title" : "'{$title}'"; - - $trackingCode = "ga('send', {'hitType': '{$hittype}', 'page': {$page}, 'title': '{$title}'});"; - } - - $this->trackingBag->add($trackingCode); - } - - /** - * track an event - * - * @param string $category - * @param string $action - * @param null|string $label - * @param null|int $value - */ - public function trackEvent($category, $action, $label = null, $value = null) - { - $command = ''; - if ($label !== null) { - $command .= ", '{$label}'"; - if ($value !== null && is_numeric($value)) { - $command .= ", {$value}"; - } - } - - $trackingCode = "ga('send', 'event', '{$category}', '{$action}'$command);"; - - $this->trackingBag->add($trackingCode); - } - - /** - * track any custom code - * - * @param string $customCode - * - * @return void - */ - public function trackCustom($customCode) - { - $this->trackingBag->add($customCode); - } - - /** - * enable display features - * - * @return GoogleAnalytics - */ - public function enableDisplayFeatures() - { - $this->displayFeatures = true; - - return $this; - } - - /** - * disable display features - * - * @return GoogleAnalytics - */ - public function disableDisplayFeatures() - { - $this->displayFeatures = false; - - return $this; - } - - /** - * enable auto tracking - * - * @return GoogleAnalytics - */ - public function enableAutoTracking() - { - $this->autoTrack = true; - - return $this; - } - - /** - * disable auto tracking - * - * @return GoogleAnalytics - */ - public function disableAutoTracking() - { - $this->autoTrack = false; - - return $this; - } - - /** - * returns the javascript embedding code - * - * @return string - */ - public function render() - { - $script[] = $this->_getJavascriptTemplateBlockBegin(); - - if ($this->debug || App::environment('local')) { - $script[] = "ga('create', '{$this->trackingId}', { 'cookieDomain': 'none' });"; - } else { - $script[] = "ga('create', '{$this->trackingId}', '{$this->trackingDomain}');"; - } - - if ($this->displayFeatures) { - $script[] = "ga('require', 'displayfeatures');"; - } - - if ($this->anonymizeIp) { - $script[] = "ga('set', 'anonymizeIp', true);"; - } - - if ($this->nonInteraction) { - $script[] = "ga('set', 'nonInteraction', true);"; - } - - $trackingStack = $this->trackingBag->get(); - if (count($trackingStack)) { - $script[] = implode("\n", $trackingStack); - } - - if ($this->autoTrack) { - $script[] = "ga('send', 'pageview');"; - } - $script[] = $this->_getJavascriptTemplateBlockEnd(); - - return implode('', $script); - } - - /** - * sets or gets nonInteraction - * - * setting: $this->nonInteraction(true)->render(); - * getting: if ($this->nonInteraction()) echo 'non-interaction set'; - * - * @param boolean|null $value - * - * @return bool|$this - */ - public function nonInteraction($value = null) - { - if (null === $value) { - return $this->nonInteraction; - } - - $this->nonInteraction = ($value === true); - - return $this; - } - - /** - * returns start block - * - * @return string - */ - protected function _getJavascriptTemplateBlockBegin() - { - $appendix = $this->debug ? '_debug' : ''; - - return "'; - } - - /** - * make the tracking measurement url unsecure - * - * @return $this - */ - public function unsecureMeasurementUrl() - { - $this->secureTrackingUrl = false; - - return $this; - } - - /** - * use the secured version of the tracking measurement url - * - * @return $this - */ - public function secureMeasurementUrl() - { - $this->secureTrackingUrl = false; - - return $this; - } - - /** - * 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')); - } - - $protocol = $this->secureTrackingUrl ? 'https' : 'http'; - - $defaults = [ - 'url' => $protocol . '://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); - } + /** + * tracking id + * + * @var string + */ + private $trackingId; + + /** + * tracking domain + * + * @var string + */ + private $trackingDomain; + + /** + * display features plugin enabled or disabled + * + * @var bool + */ + private $displayFeatures = false; + + /** + * anonymize users ip + * + * @var bool + */ + private $anonymizeIp = false; + + /** + * auto tracking the page view + * + * @var bool + */ + private $autoTrack = false; + + /** + * debug mode + * + * @var bool + */ + private $debug = false; + + /** + * for event tracking it can mark track as non-interactive so the bounce-rate calculation ignores that tracking + * + * @var bool + */ + private $nonInteraction = false; + + /** + * session tracking bag + * + * @var TrackingBag + */ + private $trackingBag; + + /** + * use https for the tracking measurement url + * + * @var bool + */ + private $secureTrackingUrl = true; + + /** + * a user id for tracking + * + * @var string|null + */ + private $userId = null; + + /** + * setting options via constructor + * + * @param array $options + * + * @throws InvalidArgumentException when tracking id not set + */ + public function __construct(array $options = []) + { + $this->trackingId = array_get($options, 'tracking_id'); + $this->trackingDomain = array_get($options, 'tracking_domain', 'auto'); + $this->displayFeatures = array_get($options, 'display_features', false); + $this->anonymizeIp = array_get($options, 'anonymize_ip', false); + $this->autoTrack = array_get($options, 'auto_track', false); + $this->debug = array_get($options, 'debug', false); + + if ($this->trackingId === null) { + throw new InvalidArgumentException('Argument tracking_id can not be null'); + } + + $this->trackingBag = new TrackingBag; + } + + /** + * track an page view + * + * @param null|string $page + * @param null|string $title + * @param null|string $hittype + * + * @return void + */ + public function trackPage($page = null, $title = null, $hittype = null) + { + $allowedHitTypes = ['pageview', 'appview', 'event', 'transaction', 'item', 'social', 'exception', 'timing']; + if ($hittype === null) { + $hittype = $allowedHitTypes[0]; + } + + if ( ! in_array($hittype, $allowedHitTypes)) { + return; + } + + $trackingCode = "ga('send', 'pageview');"; + + if ($page !== null || $title !== null || $hittype !== null) { + $page = ($page === null) ? "window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search" : "'{$page}'"; + $title = ($title === null) ? "document.title" : "'{$title}'"; + + $trackingCode = "ga('send', {'hitType': '{$hittype}', 'page': {$page}, 'title': '{$title}'});"; + } + + $this->trackingBag->add($trackingCode); + } + + /** + * track an event + * + * @param string $category + * @param string $action + * @param null|string $label + * @param null|int $value + */ + public function trackEvent($category, $action, $label = null, $value = null) + { + $command = ''; + if ($label !== null) { + $command .= ", '{$label}'"; + if ($value !== null && is_numeric($value)) { + $command .= ", {$value}"; + } + } + + $trackingCode = "ga('send', 'event', '{$category}', '{$action}'$command);"; + + $this->trackingBag->add($trackingCode); + } + + /** + * track any custom code + * + * @param string $customCode + * + * @return void + */ + public function trackCustom($customCode) + { + $this->trackingBag->add($customCode); + } + + /** + * enable display features + * + * @return GoogleAnalytics + */ + public function enableDisplayFeatures() + { + $this->displayFeatures = true; + + return $this; + } + + /** + * disable display features + * + * @return GoogleAnalytics + */ + public function disableDisplayFeatures() + { + $this->displayFeatures = false; + + return $this; + } + + /** + * enable auto tracking + * + * @return GoogleAnalytics + */ + public function enableAutoTracking() + { + $this->autoTrack = true; + + return $this; + } + + /** + * disable auto tracking + * + * @return GoogleAnalytics + */ + public function disableAutoTracking() + { + $this->autoTrack = false; + + return $this; + } + + /** + * returns the javascript embedding code + * + * @return string + */ + public function render() + { + $script[] = $this->_getJavascriptTemplateBlockBegin(); + + $trackingUserId = (null === $this->userId) + ? '' + : sprintf(", {'userId': '%s'}", $this->userId); + + if ($this->debug || App::environment('local')) { + $script[] = "ga('create', '{$this->trackingId}', { 'cookieDomain': 'none' }{$trackingUserId});"; + } else { + $script[] = "ga('create', '{$this->trackingId}', '{$this->trackingDomain}'{$trackingUserId});"; + } + + if ($this->displayFeatures) { + $script[] = "ga('require', 'displayfeatures');"; + } + + if ($this->anonymizeIp) { + $script[] = "ga('set', 'anonymizeIp', true);"; + } + + if ($this->nonInteraction) { + $script[] = "ga('set', 'nonInteraction', true);"; + } + + $trackingStack = $this->trackingBag->get(); + if (count($trackingStack)) { + $script[] = implode("\n", $trackingStack); + } + + if ($this->autoTrack) { + $script[] = "ga('send', 'pageview');"; + } + $script[] = $this->_getJavascriptTemplateBlockEnd(); + + return implode('', $script); + } + + /** + * sets or gets nonInteraction + * + * setting: $this->nonInteraction(true)->render(); + * getting: if ($this->nonInteraction()) echo 'non-interaction set'; + * + * @param boolean|null $value + * + * @return bool|$this + */ + public function nonInteraction($value = null) + { + if (null === $value) { + return $this->nonInteraction; + } + + $this->nonInteraction = ($value === true); + + return $this; + } + + /** + * returns start block + * + * @return string + */ + protected function _getJavascriptTemplateBlockBegin() + { + $appendix = $this->debug ? '_debug' : ''; + + return "'; + } + + /** + * make the tracking measurement url unsecure + * + * @return $this + */ + public function unsecureMeasurementUrl() + { + $this->secureTrackingUrl = false; + + return $this; + } + + /** + * use the secured version of the tracking measurement url + * + * @return $this + */ + public function secureMeasurementUrl() + { + $this->secureTrackingUrl = false; + + return $this; + } + + /** + * 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 = [] + ) { + $uniqueId = ($clientId !== null) ? $clientId : uniqid('track_'); + + if ($event->getLabel() === '') { + $event->setLabel($uniqueId); + } + + if ($campaign->getName() === '') { + $campaign->setName('Campaign ' . date('Y-m-d')); + } + + $protocol = $this->secureTrackingUrl ? 'https' : 'http'; + + $defaults = [ + 'url' => $protocol . '://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); + } + + /** + * sets an user id for user tracking + * + * @param string $userId + * + * @return AnalyticsProviderInterface + * + * @see https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id + */ + public function setUserId($userId) + { + $this->userId = $userId; + + return $this; + } + + /** + * unsets a possible given user id + * + * @return AnalyticsProviderInterface + */ + public function unsetUserId() + { + return $this->setUserId(null); + } } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php b/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php index c055a59..140aaf0 100644 --- a/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php +++ b/src/Ipunkt/LaravelAnalytics/Providers/NoAnalytics.php @@ -1,4 +1,6 @@ -nonInteraction(true)->render(); + * getting: if ($this->nonInteraction()) echo 'non-interaction set'; + * + * @param boolean|null $value + * + * @return bool|AnalyticsProviderInterface + */ + public function nonInteraction($value = null) + { + if (null === $value) { + return false; + } - /** - * 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 ''; - } + return $this; + } - /** - * sets or gets nonInteraction - * - * setting: $this->nonInteraction(true)->render(); - * getting: if ($this->nonInteraction()) echo 'non-interaction set'; - * - * @param boolean|null $value - * - * @return bool|AnalyticsProviderInterface - */ - public function nonInteraction($value = null) - { - if (null === $value) - return false; + /** + * sets an user id for user tracking + * + * @param string $userId + * + * @return AnalyticsProviderInterface + * + * @see https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id + */ + public function setUserId($userId) + { + return $this; + } - return $this; - } + /** + * unsets a possible given user id + * + * @return AnalyticsProviderInterface + */ + public function unsetUserId() + { + return $this; + } } \ No newline at end of file diff --git a/src/Ipunkt/LaravelAnalytics/TrackingBag.php b/src/Ipunkt/LaravelAnalytics/TrackingBag.php index eddd4ba..cb5c5d1 100644 --- a/src/Ipunkt/LaravelAnalytics/TrackingBag.php +++ b/src/Ipunkt/LaravelAnalytics/TrackingBag.php @@ -1,49 +1,56 @@ -sessionIdentifier)) { - $sessionTracks = Session::get($this->sessionIdentifier); - } - - // prevent duplicates in session - $trackingKey = md5($tracking); - $sessionTracks[$trackingKey] = $tracking; - - Session::flash($this->sessionIdentifier, $sessionTracks); - } - - /** - * returns all trackings - * - * @return array - */ - public function get() - { - $trackings = []; - if (Session::has($this->sessionIdentifier)) { - $trackings = Session::get($this->sessionIdentifier); - Session::forget($this->sessionIdentifier); - } - - return $trackings; - } + /** + * session identifier + * + * @var string + */ + private $sessionIdentifier = 'analytics.tracking'; + + /** + * adds a tracking + * + * @param string $tracking + */ + public function add($tracking) + { + $sessionTracks = []; + if (Session::has($this->sessionIdentifier)) { + $sessionTracks = Session::get($this->sessionIdentifier); + } + + // prevent duplicates in session + $trackingKey = md5($tracking); + $sessionTracks[$trackingKey] = $tracking; + + Session::flash($this->sessionIdentifier, $sessionTracks); + } + + /** + * returns all trackings + * + * @return array + */ + public function get() + { + $trackings = []; + if (Session::has($this->sessionIdentifier)) { + $trackings = Session::get($this->sessionIdentifier); + Session::forget($this->sessionIdentifier); + } + + return $trackings; + } } diff --git a/src/helpers.php b/src/helpers.php index 01dad9a..e0ba2d1 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,6 +5,7 @@ * taken from Illuminate/Foundation/helpers.php from laravel 5. */ +use Illuminate\Support\Str; if (! function_exists('env')) { /**