diff --git a/README.md b/README.md index edeceb1..8e7d60f 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ The master branch of this module is currently aiming for SilverStripe 4.x compat newTracker1: UA-xxxxxxxx-x1 #you can add multiple Google Analytics ID properties newTracker2: UA-xxxxxxxx-x2 newTrackerX: UA-xxxxxxxx-xX + excluded_controllers: + # Controllers that should be excluded from including GA codes. + - Wilr\GoogleSitemaps\Control\GoogleSitemapController ``` - Run flush=all in your browser diff --git a/_config/mygaconfig.yml.example b/_config/mygaconfig.yml.example index cb6ef90..c9c2acc 100755 --- a/_config/mygaconfig.yml.example +++ b/_config/mygaconfig.yml.example @@ -11,3 +11,6 @@ Fractas\GoogleAnalytics\GoogleAnalyticsController: newTracker1: UA-xxxxxxxx-x1 #you can add multiple Google Analytics ID properties newTracker2: UA-xxxxxxxx-x2 newTrackerX: UA-xxxxxxxx-xX + excluded_controllers: + # Controllers that should be excluded from including GA codes. + - Wilr\GoogleSitemaps\Control\GoogleSitemapController diff --git a/src/GoogleTagManagerMiddleware.php b/src/GoogleTagManagerMiddleware.php index c3bd700..331222a 100644 --- a/src/GoogleTagManagerMiddleware.php +++ b/src/GoogleTagManagerMiddleware.php @@ -32,6 +32,11 @@ class GoogleTagManagerMiddleware implements HTTPMiddleware */ private $ga_domain = null; + /** + * @var array List of controllers to be excluded + */ + private $excluded_controllers = []; + /** * Process request. * @@ -48,7 +53,9 @@ public function process(HTTPRequest $request, callable $delegate) return $response; } - if ($this->getIsEnabled()) { + $this->excluded_controllers = Config::inst()->get(GoogleAnalyticsController::class, 'excluded_controllers'); + + if ($this->getIsEnabled() && !$this->getIsExcluded($request)) { $this->gtm_id = Config::inst()->get(GoogleAnalyticsController::class, 'gtm_id'); $this->gtm_domain = Config::inst()->get(GoogleAnalyticsController::class, 'gtm_domain'); $this->ga_domain = Config::inst()->get(GoogleAnalyticsController::class, 'ga_domain'); @@ -168,4 +175,18 @@ private function getIsEnabled() return false; } + + /** + * @return bool Check if controller should be excluded from including GA codes + */ + public function getIsExcluded(HTTPRequest $request) + { + $routeParams = $request->routeParams(); + + if (array_key_exists('Controller', $routeParams)) { + return in_array($routeParams['Controller'], $this->excluded_controllers); + } + + return false; + } }