Skip to content

Commit 733fb43

Browse files
Winflerostyslav-hymon
authored andcommitted
Moved Google Analytics block code to head tag #8837.
1 parent 5a7c3bd commit 733fb43

File tree

2 files changed

+96
-1
lines changed
  • app/code/Magento/GoogleAnalytics/view/frontend/layout
  • dev/tests/integration/testsuite/Magento/GoogleAnalytics/Block

2 files changed

+96
-1
lines changed

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

0 commit comments

Comments
 (0)