Skip to content

Commit b238b01

Browse files
author
Magento CICD
authored
MAGETWO-64661: [GitHub] [PR] add anonymize ip option for google analytics #8417
2 parents eb10a64 + d51c7d1 commit b238b01

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

app/code/Magento/GoogleAnalytics/Block/Ga.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ public function getPageTrackingCode($accountId)
8080
$optPageURL = ", '" . $this->escapeHtmlAttr($pageName, false) . "'";
8181
}
8282

83+
$anonymizeIp = "";
84+
if ($this->_googleAnalyticsData->isAnonymizedIpActive()) {
85+
$anonymizeIp = "\nga('set', 'anonymizeIp', true);";
86+
}
87+
8388
return "\nga('create', '" . $this->escapeHtmlAttr($accountId, false)
84-
. ", 'auto');\nga('send', 'pageview'{$optPageURL});\n";
89+
. ", 'auto');{$anonymizeIp}\nga('send', 'pageview'{$optPageURL});\n";
8590
}
8691

8792
/**
@@ -105,6 +110,7 @@ public function getOrdersTrackingCode()
105110
$result = [];
106111

107112
$result[] = "ga('require', 'ec', 'ec.js');";
113+
108114
foreach ($collection as $order) {
109115
if ($order->getIsVirtual()) {
110116
$address = $order->getBillingAddress();

app/code/Magento/GoogleAnalytics/Helper/Data.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
2323

2424
const XML_PATH_ACCOUNT = 'google/analytics/account';
2525

26+
const XML_PATH_ANONYMIZE = 'google/analytics/anonymize';
27+
2628
/**
2729
* Whether GA is ready to use
2830
*
@@ -34,4 +36,16 @@ public function isGoogleAnalyticsAvailable($store = null)
3436
$accountId = $this->scopeConfig->getValue(self::XML_PATH_ACCOUNT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
3537
return $accountId && $this->scopeConfig->isSetFlag(self::XML_PATH_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
3638
}
39+
40+
/**
41+
* Whether anonymized IPs are active
42+
*
43+
* @param null|string|bool|int|Store $store
44+
* @return bool
45+
*/
46+
public function isAnonymizedIpActive($store = null) {
47+
$anonymize = $this->scopeConfig->getValue(self::XML_PATH_ANONYMIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
48+
return $anonymize;
49+
}
50+
3751
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
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">
28+
<label>Anonymize IP</label>
29+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
30+
<depends>
31+
<field id="*/*/active">1</field>
32+
</depends>
33+
</field>
2634
</group>
2735
</section>
2836
</system>

0 commit comments

Comments
 (0)