Skip to content

Instantly share code, notes, and snippets.

@tobiasdalhof
Last active May 9, 2022 17:01
Show Gist options
  • Save tobiasdalhof/0c8ea28f1e0007ad3698cca2f2949240 to your computer and use it in GitHub Desktop.
Save tobiasdalhof/0c8ea28f1e0007ad3698cca2f2949240 to your computer and use it in GitHub Desktop.
Magento Fooman Google Analytics GDPR
<div>
<input type="checkbox" name="toggleGoogleAnalytics" id="toggleGoogleAnalytics">
<label for="toggleGoogleAnalytics">Allow Google Analytics to track my visit.</label>
</div>
<script>
var gaProperty = 'YOUR-ANALYTICS-PROPERTY';
var gaDisableCookieName = 'ga-disable-' + gaProperty;
var gaIsAllowed = document.cookie.indexOf(gaDisableCookieName + '=true') === -1
var gaCheckbox = document.getElementById('toggleGoogleAnalytics');
gaCheckbox.checked = gaIsAllowed;
var gaOptout = function () {
document.cookie = gaDisableCookieName + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
}
var gaOptin = function () {
document.cookie = gaDisableCookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/';
}
gaCheckbox.addEventListener('change', function () {
if (this.checked === true) {
gaOptin();
} else {
gaOptout();
}
});
</script>
<?php /* @var $this Fooman_GoogleAnalyticsPlus_Block_Universal */?>
<?php if ($this->shouldInclude()): ?>
<?php $altUniversal = $this->getAlternativeUniversalAccount(); ?>
<script>
var gaProperty = '<?= $this->getUniversalAccount() ?>';
var gaDisableCookieName = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(gaDisableCookieName + '=true') === -1 ) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','<?php echo $this->getAnalyticsLocation(); ?>','ga');
ga('create', '<?php echo $this->getUniversalAccount(); ?>',<?php echo $this->getUniversalParams(); ?>);
ga('require', 'GTM-KLLL4TF');
<?php if ($altUniversal): ?>
ga('create', '<?php echo $altUniversal; ?>',<?php echo $this->getUniversalParams(true); ?>);
<?php endif; ?>
<?php if($this->getUniversalAnonymise()): ?>
ga('set', 'anonymizeIp', true);
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.set', 'anonymizeIp', true);
<?php endif; ?>
<?php endif; ?>
<?php if($this->getUniversalDisplayAdvertising()): ?>
ga('require', 'displayfeatures'<?php if($this->getUniversalDisplayAdvertisingCookieName()): ?>, undefined, { cookieName: '<?php echo $this->getUniversalDisplayAdvertisingCookieName(); ?>' }<?php endif; ?>);
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.require', 'displayfeatures'<?php if($this->getUniversalDisplayAdvertisingCookieName()): ?>, undefined, { cookieName: '<?php echo $this->getUniversalDisplayAdvertisingCookieName(); ?>' }<?php endif; ?>);
<?php endif; ?>
<?php endif; ?>
ga('send', 'pageview', '<?php echo $this->getPageName(); ?>');
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.send', 'pageview', '<?php echo $this->getPageName(); ?>');
<?php endif; ?>
}
</script>
<?php if($this->isSuccessPage()): ?>
<?php $order = $this->_getOrder(); ?>
<script>
/* <![CDATA[ */
if (document.cookie.indexOf(gaDisableCookieName + '=true') === -1 ) {
ga('require', 'ecommerce', 'ecommerce.js');
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.require', 'ecommerce', 'ecommerce.js');
<?php endif; ?>
<?php
$shipping = Mage::helper('googleanalyticsplus')->convert($order, 'shipping_amount');
$tax = Mage::helper('googleanalyticsplus')->convert($order, 'tax_amount');
$revenue = Mage::helper('googleanalyticsplus')->convert($order, 'grand_total');
$revenue -= (($this->shouldExcludeShipping())? $shipping : 0);
$revenue -= (($this->shouldExcludeTax())? $tax : 0);
$transactionDetails = "{
'id': '". $this->jsQuoteEscape($order->getIncrementId()) ."',
'affiliation': '". $this->jsQuoteEscape(Mage::app()->getStore()->getName()) ."',
'revenue': '". $revenue ."',
'shipping': '". $shipping ."',
'tax': '". $tax ."',
'currency': '". Mage::helper('googleanalyticsplus')->getTrackingCurrency($order) ."'
}";
?>
ga('ecommerce:addTransaction', <?php echo $transactionDetails?>);
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.ecommerce:addTransaction', <?php echo $transactionDetails?>);
<?php endif; ?>
<?php foreach ($order->getAllVisibleItems() as $item): ?>
<?php $itemDetails = "{
'id': '".$this->jsQuoteEscape($order->getIncrementId()) ."',
'name': '". $this->jsQuoteEscape($item->getName()) ."',
'sku': '". $this->jsQuoteEscape($item->getSku()) ."',
'price': '". Mage::helper('googleanalyticsplus')->convert($item, 'price', $order->getOrderCurrencyCode()) ."',
'quantity': '". (int)$item->getQtyOrdered() ."',
'category': '". $this->jsQuoteEscape($this->getCategory($item)) ."'
}";
?>
ga('ecommerce:addItem', <?php echo $itemDetails?>);
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.ecommerce:addItem', <?php echo $itemDetails?>);
<?php endif; ?>
<?php endforeach; ?>
ga('ecommerce:send');
<?php if ($altUniversal): ?>
ga('<?php echo Fooman_GoogleAnalyticsPlus_Block_Universal::TRACKER_TWO_NAME?>.ecommerce:send');
<?php endif; ?>
}
/* ]]> */
</script>
<?php endif; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment