Skip to content

Commit

Permalink
feat: add ability to disable GTM
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jan 29, 2024
1 parent 9dcf8c5 commit bd7bb28
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/GoogleTagManagerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Heyday\Analytics;

use SilverStripe\Control\Controller;
use SilverStripe\Core\Environment;

/**
* Class GoogleTagManagerProvider
Expand All @@ -22,6 +23,10 @@ class GoogleTagManagerProvider extends AnalyticsProvider
*/
public function getAnalyticsCode(): string
{
if (self::isTagManagerDisabled()) {
return '';
}

$id = $this->getAnalyticsID();

if (!$id) {
Expand Down Expand Up @@ -59,6 +64,10 @@ public function getAnalyticsCode(): string
*/
public function getTagManagerNoScript(): string
{
if (self::isTagManagerDisabled()) {
return '';
}

$id = $this->getAnalyticsID();

if (!$id) {
Expand Down Expand Up @@ -99,6 +108,10 @@ public static function insertDataLayer($key, $value)
*/
public static function getDataLayer()
{
if (self::isTagManagerDisabled()) {
return '';
}

// support nonce on scripts
$controller = Controller::curr();
$scriptTag = 'script';
Expand Down Expand Up @@ -140,4 +153,19 @@ private static function buildDataLayer()

return implode(',', $data);
}



public static function isTagManagerDisabled()
{
if (Environment::getEnv('GOOGLE_TAG_MANAGER_DISABLED')) {
return true;
}

if (Controller::curr() && Controller::curr()->getRequest()->getVar('disable_gtm')) {
return true;
}

return;
}
}

0 comments on commit bd7bb28

Please sign in to comment.