Skip to content

Commit 58d453c

Browse files
committed
Improvements based on static tests
1 parent 2383f88 commit 58d453c

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

app/code/Magento/GoogleGtag/Block/Ga.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ public function getPageName(): ?string
8383
return $this->_getData('page_name');
8484
}
8585

86-
/**
87-
* Render GA tracking scripts
88-
*
89-
* @return string
90-
*/
91-
protected function _toHtml()
92-
{
93-
if (!$this->googleGtagConfig->isGoogleAnalyticsAvailable()) {
94-
return '';
95-
}
96-
97-
return parent::_toHtml();
98-
}
99-
10086
/**
10187
* Return cookie restriction mode value.
10288
*
@@ -206,7 +192,8 @@ public function getAnalyticsData()
206192
'currentWebsite' => $this->getCurrentWebsiteId(),
207193
'cookieName' => Cookie::IS_USER_ALLOWED_SAVE_COOKIE,
208194
'pageTrackingData' => $this->getPageTrackingData($this->googleGtagConfig->getMeasurementId()),
209-
'ordersTrackingData' => $this->getOrdersTrackingData()
195+
'ordersTrackingData' => $this->getOrdersTrackingData(),
196+
'googleAnalyticsAvailable' => $this->googleGtagConfig->isGoogleAnalyticsAvailable()
210197
];
211198
return $this->serializer->serialize($analyticData);
212199
}

app/code/Magento/GoogleGtag/view/frontend/templates/ga.phtml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
/** @var $block \Magento\GoogleGtag\Block\Ga */
8+
/** @var \Magento\Framework\Escaper $escaper */
9+
10+
$analyticsData = $block->getAnalyticsData();
611
?>
7-
<?php /** @var $block \Magento\GoogleGtag\Block\Ga */ ?>
8-
<?php /** @var \Magento\Framework\Escaper $escaper */ ?>
9-
<!-- BEGIN GOOGLE ANALYTICS 4 CODE -->
10-
<script type="text/x-magento-init">
11-
{
12-
"*": {
13-
"Magento_GoogleGtag/js/google-analytics": <?= /* @noEscape */ $block->getAnalyticsData() ?>
12+
13+
<?php if ($analyticsData['googleAnalyticsAvailable']): ?>
14+
<!-- BEGIN GOOGLE ANALYTICS 4 CODE -->
15+
<script type="text/x-magento-init">
16+
{
17+
"*": {
18+
"Magento_GoogleGtag/js/google-analytics": <?= /* @noEscape */ $block->getAnalyticsData() ?>
19+
}
1420
}
15-
}
16-
</script>
17-
<!-- END GOOGLE ANALYTICS 4 CODE -->
21+
</script>
22+
<!-- END GOOGLE ANALYTICS 4 CODE -->
23+
<?php endif; ?>

0 commit comments

Comments
 (0)