Skip to content

Commit

Permalink
Merge pull request #3 from jinjie/fix-googlesitemap
Browse files Browse the repository at this point in the history
FIX #2
  • Loading branch information
jelicanin authored Nov 3, 2023
2 parents f483584 + 290a814 commit f535022
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions _config/mygaconfig.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 22 additions & 1 deletion src/GoogleTagManagerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f535022

Please sign in to comment.