From be4ee7619465d67200a9f175a2a9486aa9825d07 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 26 Feb 2019 17:33:50 +0100 Subject: [PATCH] wip #2609 @0.25 --- resources/config/config.php | 5 ++++ src/GoogleAnalytics.php | 21 +++++++++++++++++ src/GtagFrontProvider.php | 1 + src/ScriptViewCreator.php | 47 ++++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 22 deletions(-) diff --git a/resources/config/config.php b/resources/config/config.php index 3b2bff4..a0c3110 100644 --- a/resources/config/config.php +++ b/resources/config/config.php @@ -11,4 +11,9 @@ return [ * Enable or disable script rendering. Useful for local development. */ 'enabled' => env('GOOGLE_ANALYTICS_ID', '') != '', + + /* + * Observe Do Not Track header. Disable google analytics tracking when enabled on the user agent + */ + 'observe_dnt' => true, ]; diff --git a/src/GoogleAnalytics.php b/src/GoogleAnalytics.php index 4356b18..dd4c5dc 100644 --- a/src/GoogleAnalytics.php +++ b/src/GoogleAnalytics.php @@ -16,6 +16,11 @@ class GoogleAnalytics */ protected $enabled; + /** + * @var bool + */ + protected $observe_dnt; + /** * @param string $id */ @@ -60,4 +65,20 @@ class GoogleAnalytics { $this->enabled = false; } + + /** + * @return bool + */ + public function isObserveDnt() + { + return $this->observe_dnt; + } + + /** + * @param bool $observe_dnt + */ + public function setObserveDnt($observe_dnt) + { + $this->observe_dnt = $observe_dnt; + } } diff --git a/src/GtagFrontProvider.php b/src/GtagFrontProvider.php index d8f4e13..294f095 100644 --- a/src/GtagFrontProvider.php +++ b/src/GtagFrontProvider.php @@ -42,6 +42,7 @@ class GtagServiceProvider extends ServiceProvider if (config('gtag.enabled') === false) { $googleAnalytics->disable(); } + $googleAnalytics->setObserveDnt(config('gtag.observe_dnt')); $this->app->instance('Cubist\Gtag\GoogleAnalytics', $googleAnalytics); $this->app->alias('Cubist\Gtag\GoogleAnalytics', 'gtag'); diff --git a/src/ScriptViewCreator.php b/src/ScriptViewCreator.php index b0a64c6..9a71ff4 100644 --- a/src/ScriptViewCreator.php +++ b/src/ScriptViewCreator.php @@ -6,30 +6,33 @@ use Illuminate\View\View; class ScriptViewCreator { - /** - * @var GoogleAnalytics - */ - protected $googleAnalytics; + /** + * @var GoogleAnalytics + */ + protected $googleAnalytics; - public function __construct(GoogleAnalytics $googleAnalytics) - { - $this->googleAnalytics = $googleAnalytics; - } + public function __construct(GoogleAnalytics $googleAnalytics) + { + $this->googleAnalytics = $googleAnalytics; + } - /** - * Bind data to the view. - * - * @param View $view - */ - public function create(View $view) - { - if ($this->googleAnalytics->isEnabled() && empty($this->googleAnalytics->id())) { - return; - } + /** + * Bind data to the view. + * + * @param View $view + */ + public function create(View $view) + { + if ($this->googleAnalytics->isObserveDnt() && isset($_SERVER['HTTP_DNT']) && $_SERVER['HTTP_DNT'] === 1) { + $this->googleAnalytics->disable(); + } + if ($this->googleAnalytics->isEnabled() && empty($this->googleAnalytics->id())) { + return; + } - $view - ->with('enabled', $this->googleAnalytics->isEnabled()) - ->with('id', $this->googleAnalytics->id()); - } + $view + ->with('enabled', $this->googleAnalytics->isEnabled()) + ->with('id', $this->googleAnalytics->id()); + } } -- 2.39.5