Skip to content

Commit d35571d

Browse files
committed
Refactored getting public ID
1 parent 0587b9a commit d35571d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Controller/Adminhtml/Container/Generate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function execute()
124124
'rm' => true
125125
];
126126
$this->fileFactory->create(
127-
sprintf($this->config->getPublicId() . '_%s.json', $this->dateTime->date('Y-m-d_H-i-s')),
127+
sprintf('GTM' . '_%s.json', $this->dateTime->date('Y-m-d_H-i-s')),
128128
$fileContent,
129129
DirectoryList::MEDIA,
130130
'application/json'

Model/Config.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ public function __construct(
7272
*/
7373
public function isEnabled(string $storeId = null): bool
7474
{
75-
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId) && (
76-
($this->getPublicId($storeId) && 'use_public_id' === $this->getInstallGtm($storeId))
77-
|| ($this->getGtmScript($storeId) && 'use_head_and_body_script' === $this->getInstallGtm($storeId))
78-
);
75+
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId);
7976
}
8077

8178
/**
@@ -117,7 +114,21 @@ public function getContainerId(string $storeId = null): string
117114
*/
118115
public function getPublicId(string $storeId = null): string
119116
{
120-
return trim((string)$this->getConfig(self::XML_PATH_WEB_PUBLIC_ID, $storeId));
117+
$result = trim((string)$this->getConfig(self::XML_PATH_WEB_PUBLIC_ID, $storeId));
118+
119+
if (!$result) {
120+
if ($gtmScript = $this->getGtmScript($storeId)) {
121+
$pattern = '/GTM-[A-Z0-9]+/';
122+
$matches = [];
123+
if (preg_match($pattern, $gtmScript, $matches)) {
124+
if (isset($matches[0])) {
125+
return (string)$matches[0];
126+
}
127+
}
128+
}
129+
}
130+
131+
return $result;
121132
}
122133

123134
/**

0 commit comments

Comments
 (0)