Skip to content

Commit 8eb0752

Browse files
AC-2574 moved inline js in separate file
1 parent cc250a7 commit 8eb0752

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,25 @@
88
/** @var \Magento\Framework\View\Element\Template $block */
99
/** @var \Magento\Framework\Escaper $escaper */
1010
/** @var \Magento\GoogleGtag\ViewModel\Adwords $adsViewModel */
11+
1112
$adsViewModel = $block->getViewModel();
1213
?>
1314

1415
<?php if ($adsViewModel->isGoogleAdwordsConfigurable() && $adsViewModel->isGoogleAdwordsActive()): ?>
15-
<?php $conversionId = $adsViewModel->getConversionId(); ?>
16-
<?php $conversionLabel = $adsViewModel->getConversionLabel(); ?>
17-
<script>
18-
gtag(
19-
'event',
20-
'conversion',
21-
{'send_to': '<?= $block->escapeHtml($conversionId) ?>' + '/'
22-
+ '<?= $block->escapeHtml($conversionLabel) ?>'}
23-
);
16+
<?php $conversionId = $block->escapeHtml($adsViewModel->getConversionId()); ?>
17+
<?php $conversionLabel = $block->escapeHtml($adsViewModel->getConversionLabel()); ?>
18+
<?php $gtagSiteSrc = $adsViewModel->getConversionGtagGlobalSiteTagSrc(); ?>
19+
<!-- BEGIN GOOGLE ADWORDS CODE -->
20+
<script type="text/x-magento-init">
21+
{
22+
"*": {
23+
"Magento_GoogleGtag/js/google-adwords": {
24+
"conversionId": "<?= $block->escapeHtml($conversionId); ?>",
25+
"gtagSiteSrc": "<?= $block->escapeHtml($gtagSiteSrc); ?>",
26+
"conversionLabel": "<?= $block->escapeHtml($conversionLabel); ?>"
27+
}
28+
}
29+
}
2430
</script>
31+
<!-- END GOOGLE ADWORDS CODE -->
2532
<?php endif ?>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$analyticsData = $block->getAnalyticsData();
1111
?>
1212

13-
<?php if ($analyticsData['googleAnalyticsAvailable']): ?>
13+
<?php if ($analyticsData): ?>
1414
<!-- BEGIN GOOGLE ANALYTICS 4 CODE -->
1515
<script type="text/x-magento-init">
1616
{

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,16 @@ $adsViewModel = $block->getViewModel();
1414
<?php if ($adsViewModel->isGoogleAdwordsConfigurable() && $adsViewModel->isGoogleAdwordsActive()): ?>
1515
<?php $conversionId = $adsViewModel->getConversionId(); ?>
1616
<?php $gtagSiteSrc = $adsViewModel->getConversionGtagGlobalSiteTagSrc(); ?>
17-
<script>
18-
if (!window.gtag) {
19-
// Inject Global Site Tag
20-
var gtagScript = document.createElement('script');
21-
gtagScript.type = 'text/javascript';
22-
gtagScript.async = true;
23-
gtagScript.src = '<?= $block->escapeHtml($gtagSiteSrc) ?>';
24-
document.head.appendChild(gtagScript);
25-
26-
window.dataLayer = window.dataLayer || [];
27-
28-
function gtag(){dataLayer.push(arguments);}
29-
gtag('js', new Date());
30-
gtag('set', 'developer_id.dYjhlMD', true);
31-
} else {
32-
gtag('config', '<?= $block->escapeHtml($conversionId) ?>');
17+
<!-- BEGIN GOOGLE ADWORDS CODE -->
18+
<script type="text/x-magento-init">
19+
{
20+
"*": {
21+
"Magento_GoogleGtag/js/google-adwords": {
22+
"conversionId": "<?= $block->escapeHtml($conversionId); ?>",
23+
"gtagSiteSrc": "<?= $block->escapeHtml($gtagSiteSrc); ?>"
24+
}
3325
}
34-
</script>
26+
}
27+
</script>
28+
<!-- END GOOGLE ADWORDS CODE -->
3529
<?php endif ?>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
/* jscs:disable */
6+
/* eslint-disable */
7+
define([
8+
'jquery'
9+
], function ($) {
10+
'use strict';
11+
12+
/**
13+
* @param {Object} config
14+
*/
15+
return function (config) {
16+
if (!window.gtag) {
17+
// Inject Global Site Tag
18+
var gtagScript = document.createElement('script');
19+
gtagScript.type = 'text/javascript';
20+
gtagScript.async = true;
21+
gtagScript.src = config.gtagSiteSrc;
22+
document.head.appendChild(gtagScript);
23+
24+
window.dataLayer = window.dataLayer || [];
25+
26+
function gtag(){dataLayer.push(arguments);}
27+
gtag('js', new Date());
28+
gtag('set', 'developer_id.dYjhlMD', true);
29+
if (config.conversionLabel) {
30+
gtag(
31+
'event',
32+
'conversion',
33+
{'send_to': config.conversionId + '/'
34+
+ config.conversionLabel}
35+
);
36+
}
37+
} else {
38+
gtag('config', config.conversionId);
39+
}
40+
}
41+
});

0 commit comments

Comments
 (0)