Skip to content

Commit

Permalink
Add support for custom domain using GTM JS client
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Jan 25, 2024
1 parent c3b232b commit 13b84b6
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 10 deletions.
18 changes: 18 additions & 0 deletions Block/DataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ public function addJsInHead($store_id = null)
{
return (int) $this->_gtmHelper->addJsInHead($store_id);
}

/**
* @param null $store_id
* @return bool
*/
public function isAdvancedSettingsEnabled($store_id = null)
{
return $this->_gtmHelper->isAdvancedSettingsEnabled($store_id);
}

/**
* @param null $store_id
* @return string
*/
public function getAdvancedSettingsJsCode($store_id = null)
{
return $this->_gtmHelper->getAdvancedSettingsJsCode($store_id);
}
}
18 changes: 18 additions & 0 deletions Block/GtmCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ protected function _toHtml()

return parent::_toHtml();
}

/**
* @param null $store_id
* @return bool
*/
public function isAdvancedSettingsEnabled($store_id = null)
{
return $this->_gtmHelper->isAdvancedSettingsEnabled($store_id);
}

/**
* @param null $store_id
* @return string
*/
public function getAdvancedSettingsIframeCode($store_id = null)
{
return $this->_gtmHelper->getAdvancedSettingsIframeCode($store_id);
}
}
39 changes: 39 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,45 @@ public function isCategoryLayerEnabled($store_id = null)
);
}

/**
* @param null $store_id
* @return bool
*/
public function isAdvancedSettingsEnabled($store_id = null)
{
return $this->scopeConfig->isSetFlag(
'googletagmanager/advanced_settings/enabled',
ScopeInterface::SCOPE_STORE,
$store_id
);
}

/**
* @param null $store_id
* @return string
*/
public function getAdvancedSettingsJsCode($store_id = null)
{
return $this->scopeConfig->getValue(
'googletagmanager/advanced_settings/js_code',
ScopeInterface::SCOPE_STORE,
$store_id
);
}

/**
* @param null $store_id
* @return string
*/
public function getAdvancedSettingsIframeCode($store_id = null)
{
return $this->scopeConfig->getValue(
'googletagmanager/advanced_settings/iframe_code',
ScopeInterface::SCOPE_STORE,
$store_id
);
}

/**
* @param ProductInterface $product
* @param array $viewItem
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"magepal/magento2-google-analytics4": "Prepare for the future with Google Analytics 4. Learn more at https://www.magepal.com/google-analytics-4-for-google-tag-manager.html"
},
"type": "magento2-module",
"version": "2.7.4",
"version": "2.7.5",
"autoload": {
"files": [
"registration.php"
Expand Down
42 changes: 35 additions & 7 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
that make it very similar, yet more powerful than Enhanced Ecommerce.
Gain access to GA4 new approach to privacy-first tracking, channel measurement, and AI based predictive data with our new Google Analytics 4 extension.
</div>
<div id="upgrade-to-datalayer">
Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager?
Learn how simple and easy it is to integrate any third party service with our new <a href="http://bit.ly/MPE-DLGTM">DataLayer extension</a>.
</div>
<div id="upgrade-to-enhanced-ecommerce">
Want to learn more about your customers? Gain valuable insight on your customers shopping behavior, sales performance and more.
Upgrade to our new <a href="http://bit.ly/MPE-EEGTM">Enhanced E-commerce</a> today,
to take full advantage of Google Analytics most valuable features and reports.
</div>
<div id="upgrade-to-datalayer">
Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager?
Learn how simple and easy it is to integrate any third party service with our new <a href="http://bit.ly/MPE-DLGTM">DataLayer extension</a>.
</div>
<hr class="magepal-hr" />
]]>
</comment>
Expand Down Expand Up @@ -99,9 +99,7 @@
<depends>
<field id="*/*/enabled">0</field>
</depends>
<comment>
Note: Data Layer event firing Order may change
</comment>
<comment>Note: Data Layer events firing sequence may change.</comment>
</field>
<field id="option" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Options</label>
Expand All @@ -128,6 +126,36 @@
</depends>
</field>
</group>
<group id="advanced_settings" translate="label" type="text" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Advanced Settings</label>
<comment><![CDATA[Utilize advanced settings to set up the most sophisticated Google Tag Manager configuration for Multiple Environments & Custom Domain.]]></comment>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Advanced Settings</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>

<field id="js_code" translate="label" type="textarea" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google Tag Manager JavaScript Code</label>
<comment>
<![CDATA[Copy and Paste the entire Google Tag Manager JavaScript Code.]]>
</comment>
<validate>required-entry</validate>
<depends>
<field id="*/*/enabled">1</field>
</depends>
</field>

<field id="iframe_code" translate="label" type="textarea" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Google Tag Manager iFrame Code</label>
<comment>
<![CDATA[Copy and Paste the entire Google Tag Manager iFrame HTML Code.]]>
</comment>
<validate>required-entry</validate>
<depends>
<field id="*/*/enabled">1</field>
</depends>
</field>
</group>
<group id="gtm_container" translate="label" type="text" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">
<label>GTM Container</label>
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
4 changes: 4 additions & 0 deletions view/frontend/templates/iframe.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
?>

<!-- Google Tag Manager by MagePal -->
<?php if (!$block->isAdvancedSettingsEnabled()): ?>
<noscript><iframe src="<?= '/' . '/' ?>www.googletagmanager.com/ns.html?id=<?= /* @noEscape */ $block->getEmbeddedAccountId() ?>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<?php else: ?>
<?= /* @noEscape */ $block->getAdvancedSettingsIframeCode() ?>
<?php endif; ?>
<!-- End Google Tag Manager by MagePal -->
8 changes: 6 additions & 2 deletions view/frontend/templates/js.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $containerCode = $block->getEmbeddedCode();
<script type="text/javascript">
window.<?= /* @noEscape */ $dataLayerName ?> = window.<?= /* @noEscape */ $dataLayerName ?> || [];

<?php if (!$block->isGdprEnabled() && $block->addJsInHead()): ?>
<?php if (!$block->isGdprEnabled() && $block->addJsInHead() && !$block->isAdvancedSettingsEnabled()): ?>
<?= /* @noEscape */ $block->getDataLayerJs() ?>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
Expand All @@ -25,7 +25,11 @@ $containerCode = $block->getEmbeddedCode();
<?php endif; ?>
</script>

<?php if ($block->isGdprEnabled() || !$block->addJsInHead()): ?>
<?php if ($block->isAdvancedSettingsEnabled()): ?>
<?= /* @noEscape */ $block->getAdvancedSettingsJsCode() ?>
<?php endif; ?>

<?php if (($block->isGdprEnabled() || !$block->addJsInHead()) && !$block->isAdvancedSettingsEnabled()) : ?>
<script type="text/x-magento-init">
{
"*": {
Expand Down

0 comments on commit 13b84b6

Please sign in to comment.