* 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,
];
*/
protected $enabled;
+ /**
+ * @var bool
+ */
+ protected $observe_dnt;
+
/**
* @param string $id
*/
{
$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;
+ }
}
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');
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());
+ }
}