Skip to content

Commit ba71e6d

Browse files
author
Stanislav Idolov
authored
ENGCOM-1229: [Forwardport] Moved Google Analytics block code to head tag #8837 #14513
2 parents 1ecfd23 + 5deee2f commit ba71e6d

File tree

3 files changed

+103
-4
lines changed
  • app/code/Magento/GoogleAnalytics
  • dev/tests/integration/testsuite/Magento/GoogleAnalytics/Block

3 files changed

+103
-4
lines changed

app/code/Magento/GoogleAnalytics/etc/adminhtml/system.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
<field id="*/*/active">1</field>
2424
</depends>
2525
</field>
26-
27-
<field id="anonymize" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
26+
<field id="anonymize" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
2827
<label>Anonymize IP</label>
2928
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
30-
<depends>
29+
<depends>
3130
<field id="*/*/active">1</field>
3231
</depends>
3332
</field>

app/code/Magento/GoogleAnalytics/view/frontend/layout/default.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-->
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
10-
<referenceContainer name="after.body.start">
10+
<referenceContainer name="head.additional">
1111
<block class="Magento\GoogleAnalytics\Block\Ga" name="google_analytics" as="google_analytics" template="Magento_GoogleAnalytics::ga.phtml"/>
1212
</referenceContainer>
1313
</body>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\GoogleAnalytics\Block;
10+
11+
use Magento\TestFramework\TestCase\AbstractController;
12+
13+
class GaTest extends AbstractController
14+
{
15+
/**
16+
* Layout instance
17+
*
18+
* @var \Magento\Framework\View\LayoutInterface
19+
*/
20+
private $layout;
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
$this->dispatch('/');
29+
$this->layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
30+
}
31+
32+
/**
33+
* Check for correct position of GA block
34+
*/
35+
public function testBlockPresentInHead()
36+
{
37+
$this->assertNotNull(
38+
$this->getGaBlockFromNode('head.additional')
39+
);
40+
}
41+
42+
/**
43+
* Test that block has been successfully moved
44+
* from body to head tag.
45+
*/
46+
public function testBlockIsAbsentInBody()
47+
{
48+
$this->assertFalse(
49+
$this->getGaBlockFromNode('after.body.start')
50+
);
51+
}
52+
53+
/**
54+
* Test null output when GA is disabled
55+
*
56+
* @magentoAppArea frontend
57+
* @magentoConfigFixture current_store google/analytics/active 0
58+
* @magentoConfigFixture current_store google/analytics/account XXXXXXX
59+
*/
60+
public function testBlockOutputIsEmptyWhenGaIsDisabled()
61+
{
62+
$this->assertEquals(
63+
"",
64+
$this->getGaBlockFromNode('head.additional')->toHtml()
65+
);
66+
}
67+
68+
/**
69+
* Check, that block successfully gets rendered when configuration is
70+
* active.
71+
*
72+
* @magentoAppArea frontend
73+
* @magentoConfigFixture current_store google/analytics/active 1
74+
* @magentoConfigFixture current_store google/analytics/account XXXXXXX
75+
*/
76+
public function testBlockOutputExistsWhenGaIsEnabled()
77+
{
78+
$this->assertNotEquals(
79+
"",
80+
$this->getGaBlockFromNode('head.additional')->toHtml()
81+
);
82+
}
83+
84+
/**
85+
* Get GA block
86+
*
87+
* @param string $nodeName
88+
* @return \Magento\Framework\View\Element\AbstractBlock|false
89+
*/
90+
private function getGaBlockFromNode($nodeName = 'head.additional')
91+
{
92+
$childBlocks = $this->layout->getChildBlocks($nodeName);
93+
foreach ($childBlocks as $block) {
94+
if (strpos($block->getNameInLayout(), 'google_analytics') !== false) {
95+
return $block;
96+
}
97+
}
98+
return false;
99+
}
100+
}

0 commit comments

Comments
 (0)