@@ -7,13 +7,13 @@ |
||
7 | 7 | use SilverStripe\Forms\FieldList; |
8 | 8 | |
9 | 9 | /** |
10 | - * Description of GTagConfig |
|
11 | - * |
|
12 | - * @author tim |
|
13 | - */ |
|
10 | + * Description of GTagConfig |
|
11 | + * |
|
12 | + * @author tim |
|
13 | + */ |
|
14 | 14 | class GTagConfig extends DataExtension { |
15 | 15 | |
16 | - private static $db = [ |
|
16 | + private static $db = [ |
|
17 | 17 | 'GTMID' => 'Varchar(255)' |
18 | 18 | ]; |
19 | 19 |
@@ -14,95 +14,95 @@ |
||
14 | 14 | |
15 | 15 | class GTagMiddleware implements HTTPMiddleware { |
16 | 16 | |
17 | - use Injectable; |
|
17 | + use Injectable; |
|
18 | 18 | |
19 | 19 | use Configurable; |
20 | 20 | |
21 | - /** |
|
22 | - * @config |
|
23 | - * |
|
24 | - * Domain name of GTM |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private static $gtm_domain = 'www.googletagmanager.com'; |
|
28 | - |
|
29 | - /** |
|
30 | - * Holds the GTM ID |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - private $gtm_id = null; |
|
34 | - |
|
35 | - /** |
|
36 | - * Process the request |
|
37 | - * @param HTTPRequest $request |
|
38 | - * @param $delegate |
|
39 | - * @return |
|
40 | - */ |
|
41 | - public function process(HTTPRequest $request, callable $delegate) { |
|
42 | - |
|
43 | - $response = $delegate($request); |
|
44 | - |
|
45 | - if ($this->getIsAdmin($request) === true) { |
|
46 | - return $response; |
|
47 | - } |
|
48 | - |
|
49 | - $config = SiteConfig::current_site_config(); |
|
50 | - $this->gtm_id = $config->GTMID; |
|
51 | - if ($this->gtm_id != "") { |
|
52 | - $this->addBodyTag($response); |
|
53 | - $this->addHeadTag($response); |
|
54 | - $this->addPrefetch($response); |
|
55 | - } |
|
56 | - return $response; |
|
57 | - } |
|
58 | - |
|
59 | - private function addBodyTag(&$response) { |
|
60 | - $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
61 | - $tag = $data->renderWith('BodySnippet'); |
|
62 | - $body = $response->getBody(); |
|
63 | - $pattern = '/\<body.*\>/'; |
|
64 | - $replace = '${0}' . "\n" . $tag; |
|
65 | - $newBody = preg_replace($pattern, $replace, $body); |
|
66 | - $response->setBody($newBody); |
|
67 | - } |
|
68 | - |
|
69 | - private function addHeadTag(&$response) { |
|
70 | - $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
71 | - $tag = $data->renderWith('HeadSnippet'); |
|
72 | - $body = $response->getBody(); |
|
73 | - $body = str_replace('<head>', "<head>" . $tag, $body); |
|
74 | - $response->setBody($body); |
|
75 | - } |
|
76 | - |
|
77 | - private function addPrefetch(&$response) { |
|
78 | - $atts = [ |
|
79 | - 'rel' => 'dns-prefetch', |
|
80 | - 'href' => $this->config()->get('gtm_domain') |
|
81 | - ]; |
|
82 | - $pfTag = "\n" . HTML::createTag('link', $atts) . "\n"; |
|
83 | - $body = $response->getBody(); |
|
84 | - $body = str_replace('<head>', "<head>" . $pfTag, $body); |
|
85 | - $response->setBody($body); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Determine whether the website is being viewed from an admin protected area or not |
|
90 | - * (shamelessly stolen from https://github.com/silverstripe/silverstripe-subsites) |
|
91 | - * |
|
92 | - * @param HTTPRequest $request |
|
93 | - * @return bool |
|
94 | - */ |
|
95 | - private function getIsAdmin(HTTPRequest $request) { |
|
96 | - $adminPaths = static::config()->get('admin_url_paths'); |
|
97 | - $adminPaths[] = AdminRootController::config()->get('url_base') . '/'; |
|
98 | - $adminPaths[] = 'dev/'; |
|
99 | - $currentPath = rtrim($request->getURL(), '/') . '/'; |
|
100 | - foreach ($adminPaths as $adminPath) { |
|
101 | - if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { |
|
102 | - return true; |
|
103 | - } |
|
104 | - } |
|
105 | - return false; |
|
106 | - } |
|
21 | + /** |
|
22 | + * @config |
|
23 | + * |
|
24 | + * Domain name of GTM |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + private static $gtm_domain = 'www.googletagmanager.com'; |
|
28 | + |
|
29 | + /** |
|
30 | + * Holds the GTM ID |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + private $gtm_id = null; |
|
34 | + |
|
35 | + /** |
|
36 | + * Process the request |
|
37 | + * @param HTTPRequest $request |
|
38 | + * @param $delegate |
|
39 | + * @return |
|
40 | + */ |
|
41 | + public function process(HTTPRequest $request, callable $delegate) { |
|
42 | + |
|
43 | + $response = $delegate($request); |
|
44 | + |
|
45 | + if ($this->getIsAdmin($request) === true) { |
|
46 | + return $response; |
|
47 | + } |
|
48 | + |
|
49 | + $config = SiteConfig::current_site_config(); |
|
50 | + $this->gtm_id = $config->GTMID; |
|
51 | + if ($this->gtm_id != "") { |
|
52 | + $this->addBodyTag($response); |
|
53 | + $this->addHeadTag($response); |
|
54 | + $this->addPrefetch($response); |
|
55 | + } |
|
56 | + return $response; |
|
57 | + } |
|
58 | + |
|
59 | + private function addBodyTag(&$response) { |
|
60 | + $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
61 | + $tag = $data->renderWith('BodySnippet'); |
|
62 | + $body = $response->getBody(); |
|
63 | + $pattern = '/\<body.*\>/'; |
|
64 | + $replace = '${0}' . "\n" . $tag; |
|
65 | + $newBody = preg_replace($pattern, $replace, $body); |
|
66 | + $response->setBody($newBody); |
|
67 | + } |
|
68 | + |
|
69 | + private function addHeadTag(&$response) { |
|
70 | + $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
71 | + $tag = $data->renderWith('HeadSnippet'); |
|
72 | + $body = $response->getBody(); |
|
73 | + $body = str_replace('<head>', "<head>" . $tag, $body); |
|
74 | + $response->setBody($body); |
|
75 | + } |
|
76 | + |
|
77 | + private function addPrefetch(&$response) { |
|
78 | + $atts = [ |
|
79 | + 'rel' => 'dns-prefetch', |
|
80 | + 'href' => $this->config()->get('gtm_domain') |
|
81 | + ]; |
|
82 | + $pfTag = "\n" . HTML::createTag('link', $atts) . "\n"; |
|
83 | + $body = $response->getBody(); |
|
84 | + $body = str_replace('<head>', "<head>" . $pfTag, $body); |
|
85 | + $response->setBody($body); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Determine whether the website is being viewed from an admin protected area or not |
|
90 | + * (shamelessly stolen from https://github.com/silverstripe/silverstripe-subsites) |
|
91 | + * |
|
92 | + * @param HTTPRequest $request |
|
93 | + * @return bool |
|
94 | + */ |
|
95 | + private function getIsAdmin(HTTPRequest $request) { |
|
96 | + $adminPaths = static::config()->get('admin_url_paths'); |
|
97 | + $adminPaths[] = AdminRootController::config()->get('url_base') . '/'; |
|
98 | + $adminPaths[] = 'dev/'; |
|
99 | + $currentPath = rtrim($request->getURL(), '/') . '/'; |
|
100 | + foreach ($adminPaths as $adminPath) { |
|
101 | + if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { |
|
102 | + return true; |
|
103 | + } |
|
104 | + } |
|
105 | + return false; |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | use SilverStripe\SiteConfig\SiteConfig; |
13 | 13 | use SilverStripe\View\ArrayData; |
14 | 14 | |
15 | -class GTagMiddleware implements HTTPMiddleware { |
|
15 | +class GTagMiddleware implements HTTPMiddleware { |
|
16 | 16 | |
17 | 17 | use Injectable; |
18 | 18 | |
@@ -24,13 +24,13 @@ discard block |
||
24 | 24 | * Domain name of GTM |
25 | 25 | * @var string |
26 | 26 | */ |
27 | - private static $gtm_domain = 'www.googletagmanager.com'; |
|
27 | + private static $gtm_domain = 'www.googletagmanager.com'; |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Holds the GTM ID |
31 | 31 | * @var string |
32 | 32 | */ |
33 | - private $gtm_id = null; |
|
33 | + private $gtm_id = null; |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * Process the request |
@@ -38,17 +38,17 @@ discard block |
||
38 | 38 | * @param $delegate |
39 | 39 | * @return |
40 | 40 | */ |
41 | - public function process(HTTPRequest $request, callable $delegate) { |
|
41 | + public function process(HTTPRequest $request, callable $delegate) { |
|
42 | 42 | |
43 | - $response = $delegate($request); |
|
43 | + $response = $delegate($request); |
|
44 | 44 | |
45 | - if ($this->getIsAdmin($request) === true) { |
|
45 | + if ($this->getIsAdmin($request) === true) { |
|
46 | 46 | return $response; |
47 | 47 | } |
48 | 48 | |
49 | - $config = SiteConfig::current_site_config(); |
|
50 | - $this->gtm_id = $config->GTMID; |
|
51 | - if ($this->gtm_id != "") { |
|
49 | + $config = SiteConfig::current_site_config(); |
|
50 | + $this->gtm_id = $config->GTMID; |
|
51 | + if ($this->gtm_id != "") { |
|
52 | 52 | $this->addBodyTag($response); |
53 | 53 | $this->addHeadTag($response); |
54 | 54 | $this->addPrefetch($response); |
@@ -56,32 +56,32 @@ discard block |
||
56 | 56 | return $response; |
57 | 57 | } |
58 | 58 | |
59 | - private function addBodyTag(&$response) { |
|
60 | - $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
61 | - $tag = $data->renderWith('BodySnippet'); |
|
62 | - $body = $response->getBody(); |
|
63 | - $pattern = '/\<body.*\>/'; |
|
64 | - $replace = '${0}' . "\n" . $tag; |
|
65 | - $newBody = preg_replace($pattern, $replace, $body); |
|
59 | + private function addBodyTag(&$response) { |
|
60 | + $data = ArrayData::create([ 'GTMID' => $this->gtm_id ]); |
|
61 | + $tag = $data->renderWith('BodySnippet'); |
|
62 | + $body = $response->getBody(); |
|
63 | + $pattern = '/\<body.*\>/'; |
|
64 | + $replace = '${0}' . "\n" . $tag; |
|
65 | + $newBody = preg_replace($pattern, $replace, $body); |
|
66 | 66 | $response->setBody($newBody); |
67 | 67 | } |
68 | 68 | |
69 | - private function addHeadTag(&$response) { |
|
70 | - $data = ArrayData::create(['GTMID' => $this->gtm_id]); |
|
71 | - $tag = $data->renderWith('HeadSnippet'); |
|
72 | - $body = $response->getBody(); |
|
73 | - $body = str_replace('<head>', "<head>" . $tag, $body); |
|
69 | + private function addHeadTag(&$response) { |
|
70 | + $data = ArrayData::create([ 'GTMID' => $this->gtm_id ]); |
|
71 | + $tag = $data->renderWith('HeadSnippet'); |
|
72 | + $body = $response->getBody(); |
|
73 | + $body = str_replace('<head>', "<head>" . $tag, $body); |
|
74 | 74 | $response->setBody($body); |
75 | 75 | } |
76 | 76 | |
77 | - private function addPrefetch(&$response) { |
|
78 | - $atts = [ |
|
77 | + private function addPrefetch(&$response) { |
|
78 | + $atts = [ |
|
79 | 79 | 'rel' => 'dns-prefetch', |
80 | 80 | 'href' => $this->config()->get('gtm_domain') |
81 | 81 | ]; |
82 | - $pfTag = "\n" . HTML::createTag('link', $atts) . "\n"; |
|
83 | - $body = $response->getBody(); |
|
84 | - $body = str_replace('<head>', "<head>" . $pfTag, $body); |
|
82 | + $pfTag = "\n" . HTML::createTag('link', $atts) . "\n"; |
|
83 | + $body = $response->getBody(); |
|
84 | + $body = str_replace('<head>', "<head>" . $pfTag, $body); |
|
85 | 85 | $response->setBody($body); |
86 | 86 | } |
87 | 87 | |
@@ -92,13 +92,13 @@ discard block |
||
92 | 92 | * @param HTTPRequest $request |
93 | 93 | * @return bool |
94 | 94 | */ |
95 | - private function getIsAdmin(HTTPRequest $request) { |
|
96 | - $adminPaths = static::config()->get('admin_url_paths'); |
|
97 | - $adminPaths[] = AdminRootController::config()->get('url_base') . '/'; |
|
98 | - $adminPaths[] = 'dev/'; |
|
99 | - $currentPath = rtrim($request->getURL(), '/') . '/'; |
|
100 | - foreach ($adminPaths as $adminPath) { |
|
101 | - if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { |
|
95 | + private function getIsAdmin(HTTPRequest $request) { |
|
96 | + $adminPaths = static::config()->get('admin_url_paths'); |
|
97 | + $adminPaths[ ] = AdminRootController::config()->get('url_base') . '/'; |
|
98 | + $adminPaths[ ] = 'dev/'; |
|
99 | + $currentPath = rtrim($request->getURL(), '/') . '/'; |
|
100 | + foreach ($adminPaths as $adminPath) { |
|
101 | + if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { |
|
102 | 102 | return true; |
103 | 103 | } |
104 | 104 | } |