-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
|
||
namespace Fractas\GoogleAnalytics; | ||
|
||
use SilverStripe\Admin\AdminRootController; | ||
use SilverStripe\Control\Director; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\Middleware\HTTPMiddleware; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Core\Config\Configurable; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\View\ArrayData; | ||
use SilverStripe\View\HTML; | ||
|
||
class GoogleTagManagerMiddleware implements HTTPMiddleware | ||
{ | ||
use Injectable; | ||
use Configurable; | ||
|
||
/** | ||
* @var string Google Tag Manager ID | ||
*/ | ||
private $gtm_id = null; | ||
|
||
/** | ||
* @var string Google Tag Manager domain | ||
*/ | ||
private $gtm_domain = null; | ||
|
||
/** | ||
* @var string Google Analytics domain | ||
*/ | ||
private $ga_domain = null; | ||
|
||
/** | ||
* Process request. | ||
* | ||
* @param HTTPRequest $request | ||
* @param callable $delegate | ||
* | ||
* @return $response | ||
*/ | ||
public function process(HTTPRequest $request, callable $delegate) | ||
{ | ||
$response = $delegate($request); | ||
|
||
if (true === $this->getIsAdmin($request)) { | ||
return $response; | ||
} | ||
|
||
if ($this->getIsEnabled()) { | ||
$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'); | ||
|
||
$this->addBodyTag($response); | ||
$this->addHeadTag($response); | ||
$this->addPrefetchGA($response); | ||
$this->addPrefetchGTM($response); | ||
$this->addPreconnectGA($response); | ||
$this->addPreconnectGTM($response); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
private function addBodyTag(&$response) | ||
{ | ||
$data = ArrayData::create(['GTMID' => $this->gtm_id]); | ||
$tag = $data->renderWith('GoogleTagManagerNoscriptCode'); | ||
$body = $response->getBody(); | ||
$pattern = '/\<body.*\>/'; | ||
$replace = '${0}'."\n".$tag; | ||
$newBody = preg_replace($pattern, $replace, $body); | ||
$response->setBody($newBody); | ||
} | ||
|
||
private function addHeadTag(&$response) | ||
{ | ||
$data = ArrayData::create(['GTMID' => $this->gtm_id]); | ||
$tag = $data->renderWith('GoogleTagManagerCode'); | ||
$body = $response->getBody(); | ||
$body = str_replace('<head>', '<head>'.$tag, $body); | ||
$response->setBody($body); | ||
} | ||
|
||
private function addPreconnectGA(&$response) | ||
{ | ||
$atts = [ | ||
'rel' => 'preconnect', | ||
'href' => $this->ga_domain, | ||
]; | ||
$pfTag = "\n".HTML::createTag('link', $atts)."\n"; | ||
$body = $response->getBody(); | ||
$body = str_replace('<head>', '<head>'.$pfTag, $body); | ||
$response->setBody($body); | ||
} | ||
|
||
private function addPreconnectGTM(&$response) | ||
{ | ||
$atts = [ | ||
'rel' => 'preconnect', | ||
'href' => $this->gtm_domain, | ||
]; | ||
$pfTag = "\n".HTML::createTag('link', $atts)."\n"; | ||
$body = $response->getBody(); | ||
$body = str_replace('<head>', '<head>'.$pfTag, $body); | ||
$response->setBody($body); | ||
} | ||
|
||
private function addPrefetchGA(&$response) | ||
{ | ||
$atts = [ | ||
'rel' => 'dns-prefetch', | ||
'href' => $this->ga_domain, | ||
]; | ||
$pfTag = "\n".HTML::createTag('link', $atts)."\n"; | ||
$body = $response->getBody(); | ||
$body = str_replace('<head>', '<head>'.$pfTag, $body); | ||
$response->setBody($body); | ||
} | ||
|
||
private function addPrefetchGTM(&$response) | ||
{ | ||
$atts = [ | ||
'rel' => 'dns-prefetch', | ||
'href' => $this->gtm_domain, | ||
]; | ||
$pfTag = "\n".HTML::createTag('link', $atts)."\n"; | ||
$body = $response->getBody(); | ||
$body = str_replace('<head>', '<head>'.$pfTag, $body); | ||
$response->setBody($body); | ||
} | ||
|
||
/** | ||
* Determine whether the website is being viewed from an admin protected area or not | ||
* (shamelessly stolen from https://github.com/silverstripe/silverstripe-subsites). | ||
* | ||
* @param HTTPRequest $request | ||
* | ||
* @return bool | ||
*/ | ||
private function getIsAdmin(HTTPRequest $request) | ||
{ | ||
$adminPaths = static::config()->get('admin_url_paths'); | ||
$adminPaths[] = AdminRootController::config()->get('url_base').'/'; | ||
$adminPaths[] = 'dev/'; | ||
$currentPath = rtrim($request->getURL(), '/').'/'; | ||
foreach ($adminPaths as $adminPath) { | ||
if (substr($currentPath, 0, \strlen($adminPath)) === $adminPath) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return bool Google Analytics is enabled by default if is in "live" mode | ||
*/ | ||
private function getIsEnabled() | ||
{ | ||
if (Director::isLive()) { | ||
return true; | ||
} elseif (Director::isDev() && Config::inst()->get(GoogleAnalyticsController::class, 'enable_in_dev')) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- Google Tag Manager --> | ||
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','dataLayer','$GTMID');</script> | ||
<!-- End Google Tag Manager --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- Google Tag Manager (noscript) --> | ||
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={$GTMID}" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | ||
<!-- End Google Tag Manager (noscript) --> |