]> _ Git - cubist_gtag.git/commitdiff
wip #2609 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 26 Feb 2019 16:33:50 +0000 (17:33 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 26 Feb 2019 16:33:50 +0000 (17:33 +0100)
resources/config/config.php
src/GoogleAnalytics.php
src/GtagFrontProvider.php
src/ScriptViewCreator.php

index 3b2bff4504f93dabcfc94a736202f4d4daf6396e..a0c3110f4a55ce634d7345866aff54578e981ba9 100644 (file)
@@ -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,
 ];
index 4356b18ebbd2651b0ac566aac7e17890a67d7079..dd4c5dca0ece4a54fceb3c93504146200729fe09 100644 (file)
@@ -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;
+    }
 }
index d8f4e1392671ec3661dc3293c64b49a6eb379902..294f09562263ad30bc4ce41d6ac55d17b07bd8d2 100644 (file)
@@ -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');
index b0a64c6c99a21e8eaa9ddee510ee2ccf1a34c293..9a71ff43e719429a429a904c281a55c71477005d 100644 (file)
@@ -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());
+    }
 }